The Math class in Flash consists of a very large set of operators, for calculating values while running the .swf
I'm going to go over some of the most simple operators in this topic, and leave the more advanced stuff to Inglor and others.
Basic Arithmatic Operators
These are the most basic artihmatic commands.
Adds two numeric values together
variableOne = 3 + 3;
This assigns the value of three plus three to the variable: variableOne
This will mean when the script is run, variableOne will be given the value of 6.
variableThree = variableOne + variableTwo;
Variables can be added together as well, this works if they have numeric or string values (by concatenation). I'm not going to go into string operators here.
Subtracts one numeric value from another. Cannot be used with anything but numeric values.
variableOne = 10 - 6;
variableOne will be given the value of 4 (10 minus 6) when the script is run.
Variables containing numeric values can also be subtracted from eachother.
Multiplies two numeric values together.
variableOne = 2*4;
variableOne will be given the value of 8.
Once again, this operator can be used on variables with numeric values.
variableOne * variableTwo
Divides two numeric values.
variableOne = 8 / 2;
variableOne will be given the value of 4.
This one is used less often. It returns the remainder after dividing two numeric values.
variableOne = 10%3;
variableOne will be given the value of 1. This is because:
10 divided by 3 gives the answer of 9, with a remainder of 1. The remainder is what the operator returns (in this case it is assigned to variableOne)
For division equations where there is no remainder (e.g 4 / 2), the value returned by Modulo (%) will be 0.
When performing multiple equations on the same code line. Operators will be calculated in a certain order, depending on their type.
Flash works in a similar way to standard BIDMAS precedence order, when calculating values using several operators
-Data in parenthasis (Brackets) is calculated first.
-Next, multiplication and division are calculated, they have equal precedence priority, so the leftmost equation on the line (out of the 2 operators) will be calculated first.
-Addition and subtraction are evaluated last. These two operators also have equal precedence, so whichever is further to the left hand side of the line is calulated first.
Math Class Methods
These are some basic methods of the Math Class. They perfom slightly more complicated tasks than the simple Arithmatic Operators.
This method changes whatever numeric value in between the brackets into a positive value.
variableOne = Maths.abs(-20);
variableOne will be given the positive value of 20. Equations can also be input between the brackets.
variableOne = Math.abs(20 - 50);
variableOne will be given the positive value of 30.
This method rounds the numeric value input between the brackets to the nearest integer (whole number).
If the number in the first decimal place of the input value is below 5, the value will be rounded down, if the first decimal place is equal to or greater then 5, the number will be rounded up.
-Will give the value of 3.
-Will give the value of 2.
This method does the same job as Math.round(), excpept it always round the input number UP to the next highest integer.
This method does the same job as Math.round(), excpept it always round the input number DOWN to the current integer without any decimal value.
2.4 is rounded down to 2
Finds the square root of the input value.
-Will return the value of 7.
This method calculates the value of x to the power ofy
Replace the x and y with numerical values, and seperate them with commas as shown in the subheader.
-Returns 8( 2 to the power of 3).
I'll finish up on this, a really useful method for a lot of games. The method returns a random value between 0 and 1.
The actual setup for the method is that it returns n, where 0 <= n < 1
So it could return the integer 0, but not return the integer 1.
-Could return any of the following:
We use Math.random() with different boundaries, here's how.
To calculate a random number value between 0 and 20 (0 <= n < 20), add a multiplication.
To calculate a value between 8 and 9 (8<= n <9), use an addition.
To calculate a random value between 10 and 20, use both, and add brackets too.
http://livedocs.macromedia.com...MX_2004&file=00001429.html - Math method table
http://actionscript-toolbox.com/mathobject.php - Simple arithmatic.
There are plenty of tutorials on this subject matter out there, Google for them if you are in furthur need.
Thats it for now! Hope this can help some of you guys out, and answer some simple questions. Keep an eye out for Inglor's AS:Math - Advanced, if he does one. Thanks