How to use Math in Flash Professional CS3 and Actionscript 3

The Math class is unlike any other in Action-script 3 and as daunting as it first may seem its actually much simpler to use than most other classes. The math class does not to be declared as a variable, a instance or a new class as all its functionality can be accessed directly as demonstrated further below. At first the Math of Actionscript may feel like an advanced thing to be dealing with to many new developers but i can assure you its as easy as it was in the class room. infact its just the same as it was in the class room at school. Actionscript is designed that way to be human like so its not nothing to be afraid of read on and you will see.

Your Ad Here

The normal way of writing a class  


Var  myInstance: MyClass = new MyClass();


Here we have declared the variable as myInstance then data typed the variable as MyClass then made that equal to a new instance of that class name only after that can you access all the functionalities of an instance.

 

With the math class all you need to do is type Math. in the actionscript 3 window as shown below and all the methods  of the Math  class will appear ready to be used.

                Math.

 

 

Basic math in AS3

Using the  trace();  function

here are a few examples

trace (1 + 2 + 3);  this code adds the numbers together and displays the answer in the trace window when after you press ctrl +Enter to test movie

 

trace (5 * 2); the * in this code is used as  multiply

 

trace (5 - 3);

 

trace (10  /  2); the / in this code represents the divide in mathematics

Because Actionscript reads and executes the code in order (unless coded otherwise) the answers will appear just as they would if you were working out the answers yourself.

 

Applying Math. methods

Trace is the best way to filter the results you are looking for so you know what your code will eventually produce in you final piece it is a key testing tool and comes into a lot of use when testing Maths methods. Try the following code

 

trace ( Math.random() ); this will produce random numbers under 1 with a lot of decimal places as shown below

 

 

If you carry on pressing  Enter on your key board as i have done in the image Actionscript will continue producing random numbers under 1

 

To get Actionscript 3 to produce numbers up to 10.9999999 for example you would edit the code as

trace ( Math.random() *11 );

This then will only  produce random numbers under 11 as shown below

pic 3

 

 

How do i round these numbers off ?

trace ( Math.floor( Math.random() * 10 )); this code uses the floor method of the Math class and rounds numbers down for example if the number were 4.4 it would round it down to 4. If the number were 4.9 it would still round the number down to 4.

 

 

trace ( Math.ceil( Math.random() * 10 ));  this code uses the ceil method of the Math class and rounds numbers up for example if the number were 4.7 it would round it up to to 5. If the number were 4.2 it would still round the number up to 5.

 

The easiest way to remember those two methods are up to the ceiling and down to the floor hence the names of the methods

 

 

 

trace ( Math.round( Math.random() * 10 )); this code uses the round method of the Math class and rounds numbers up and down for example if the number were 4.4 it would round it down to 4. If the number were 4.6 it would round the number up to 5.

 

 

 

 

 

If you enjoyed this post, make sure you subscribe to my RSS feed

!

Comments

One Response to “How to use Math in Flash Professional CS3 and Actionscript 3”

  1. roberto on October 26th, 2007 7:07 pm

    hi this is very informative and very cool ill have to try it… i had a question though… is it true that you can use this same kind of actionscript in fireworks or flash to get the same kinda design as coldfusions randrange function ? so basically instead of numbers randomizing you can get panels to change there positions on a web page ? so the same one wont always be on the top everytime the page is opened ?

Leave a Reply




  • Sites We Like


  • UK Life Insurance Quotes
    Life Quotes
    Illness Insurance
    Contents Insurance
    Web Site
    Your Ad Here
    Secured Loans

    Compare secured loans to find the perfect deal for you!

    www.accepted.co.uk

    Matched.co.uk