Math Functions

In addition to the math operators such as +, _ and *, ActionScript3 has functions to find square root; trigonometric functions such as sin and cosine, and many others. You can see a list of the functions by typing the following: trace(Math. As soon as you type the dot you will see a drop down list with all of the functions:
math functions
Finish typing the line as:


When you run the movie, this will display 4 in the output window. Functions receive values and return a result. In the example above Math.sqrt receives 16 and returns 4. That value is then displayed by the trace function. Try a few other functions that you are familiar with.

A few other functions that are useful are shown below. Use these with trace to test:

Function Purpose Example Return Value
abs absolute Math.abs(-5) 5
ceil ceiling: rounds up by moving right on timeline to a whole number Math.ceil(3.8) 4
    Math.ceil(-3.8) -3
floor floor: rounds down by moving left on timeline to a whole number Math.floor(3.8) 3
    Math.floor(-3.8) -4
round rounds to the nearest integer Math.round(3.8) 4
    Math.round(3.2) 3
max returns the largest of two integers Math.max(8,3) 8
min returns the smallest of two integers Math.min(8,3) 3
PI returns pi: Math.PI 3.141592653589793
random returns a random value >=0 and <1 Math.random() 0.9147554710507393
pow raises a number to a power Math.pow(2,3) 8 (2 to the 3rd power or 2*2*2)

There are also a number of trigonometry functions: acos (arc cosine), asin (arc sine), atan (arc tangent), atan2 (angle from x axis to a point), cos (cosine), sin (sine), tan (tangent).

NEXT: Arrow follows cursor