!Version 1.1
AS:Main
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.
Addition (+)
Adds two numeric values together
e.g
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.
ubtraction (-)
Subtracts one numeric value from another. Cannot be used with anything but numeric values.
e.g
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.
Multiplication (*)
Multiplies two numeric values together.
e.g
variableOne = 2*4;
variableOne will be given the value of 8.
Once again, this operator can be used on variables with numeric values.
e.g
variableOne * variableTwo
Division (/)
Divides two numeric values.
e.g
variableOne = 8 / 2;
variableOne will be given the value of 4.
Modulo (%)
This one is used less often. It returns the remainder after dividing two numeric values.
e.g
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.
Precedence
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.
Math.abs()
This method changes whatever numeric value in between the brackets into a positive value.
e.g
variableOne = Maths.abs(-20);
variableOne will be given the positive value of 20. Equations can also be input between the brackets.
e.g
variableOne = Math.abs(20 - 50);
variableOne will be given the positive value of 30.
Math.round()
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.
e.g
Math.round(2.5);
-Will give the value of 3.
Whereas:
Math.round(2.3);
-Will give the value of 2.
Math.ceil()
This method does the same job as Math.round(), excpept it always round the input number UP to the next highest integer.
Math.floor()
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.
e.g
2.4 is rounded down to 2
Math.sqrt()
Finds the square root of the input value.
e.g
Math.sqrt(49);
-Will return the value of 7.
Math.pow(x,y)
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.
e.g
Math.pow(2,3);
-Returns 8( 2 to the power of 3).
Math.random()
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.
e.g
Math.random();
-Could return any of the following:
0.2
0.3
0.2536
etc.
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.
Math.random()*20;
To calculate a value between 8 and 9 (8<= n <9), use an addition.
Math.random()+8;
To calculate a random value between 10 and 20, use both, and add brackets too.
(Math.random()*10)+10;
Links
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