Introduction to ActionScript 2.0/Math Class
Tools
General
Sister projects
In other projects
| Introduction to ActionScript 2.0 | ||
| Sounds | Math Class | Other Classes |
Key concepts:
Maths are very important in Flash applications. Let's learn how to use mathematical functions in ActionScript!
There are several functions of the Math class that can manipulate numbers for us:
Let's look at a quick example:
| Code | Result |
|---|---|
varmyNumber:Number=-12.3;trace(Math.abs(myNumber));trace(Math.ceil(myNumber));trace(Math.floor(myNumber));trace(Math.round(myNumber));trace(Math.max(Math.ceil(myNumber),Math.floor(myNumber)));trace(Math.min(Math.ceil(myNumber),Math.floor(myNumber)));trace(Math.floor(Math.random()*12)+1); |
|
Note the technique used in the last line to produce random numbers. Math.random() is multiplied by 12, rounded down (giving a random integer from 0 to 11) and then one is added (giving a random integer from 1 to 12).