RSS link icon

.

Actionscript doing simple mathematics


We all either hate or love math, but one thing is for sure, if you want to be a programmer or just do some simple lines of code, you will need to do some math.

Doing math in actionscript is a bit more than just adding and subtracting, like any other programming languages it uses some reserved words to define different mathmathic methods.

math nerd - revenge of the nerds

 

I will use the Trace function in flash to output the result, you can always point to a textbox, text field later if you want.

Here is how to just add, subtract, divide and muliply (this is the simple part).

trace(1 + 1);
trace(2 - 1);
trace(2 * 2);
trace(4 / 2);

Note that we work with numbers so no double qoutes, like we do with text.

Now a very useful tool is to type Math. (something) this contains useful functions like ramdom, I will show a couple of examples, they all basically work the same way.

trace (Math.random());
//This outputs a random number less than 1.
//Now you might think this outputs a very long 0.00043348 number, here is how to round the number.
trace (Math.floor(Math.random() * 10)); 
//This will output a round number between 1-9
//And if you want two digi numbers do it like this.
trace (Math.floor(Math.random() * 100)); 

Now to combining the calculation and ramdom function.

b = 10 + Math.floor(Math.random()*6);
trace(b)

This will give you a random number between 10 and 15, as you see a great tool.

And is you want to do any math on angle calculations, calculate with circles and triangles etc, you will need these function.

trace (Math.cos(1));
trace (Math.tan(1));
trace (Math.sin(1));

I will do more on math with angles later in a bigger project I'm working on, where I will use it to make smaller flash games, this is where the math really gets fun and interesting.


krishna chaitanya sharma says: 2008-02-05

hi this is krishna chaitanya sharma learning animations in arena multimedia now in the flash ill be coming in scripting with in a couple of days so i wanna learn about calculator in flash this is very fine which u did .so please guide how 2 do calculator

Robert says: 2008-01-06

nice ! please add some more tutChr(34)s how to understand AS 2.0. i like the way you are explaining stuff.

Admin Bob says: 2008-01-07

Thanks Robert, IChr(34)m glad you like my tutorials, but in the future I will try to make some in AS 3 instead of 2.0, but I will write in the top descriptions what version my code is for.

Andrew says: 2007-12-29

Good tutorial, but really itChr(34)s still simple math.

Er says: 2007-12-28

Thanks, now I have to see if i can put it to good use!