00:00
00:00
Newgrounds Background Image Theme

Sh0rtiot just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS:Math - Simple

7,328 Views | 19 Replies
New Topic Respond to this Topic

AS:Math - Simple 2005-07-03 14:52:27


!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

Response to AS:Math - Simple 2005-07-03 14:53:17


is this the one I need to second? (Math-Intermediate)?

Response to AS:Math - Simple 2005-07-03 14:54:24


nevermind, read it, will do, but I'm making a math Intermediate before Math Advanced

Response to AS:Math - Simple 2005-07-03 14:55:19


At 7/3/05 02:54 PM, Inglor wrote: nevermind, read it, will do, but I'm making a math Intermediate before Math Advanced

Sure thing, thanks Inglor.

Response to AS:Math - Simple 2005-07-03 14:59:40


Bravo! The link to the AS: Main worked!You improved!


BBS Signature

Response to AS:Math - Simple 2005-07-03 15:01:09


At 7/3/05 02:59 PM, -Toast- wrote: Bravo! The link to the AS: Main worked!You improved!

YAY, I only used 3 nines this time ^^

hid=229808&page=999

Response to AS:Math - Simple 2005-07-03 15:17:34


Response to AS:Math - Simple 2006-05-28 15:07:50


At 7/3/05 02:52 PM, T-H wrote: Math.pow(x,y)

I don't think you should use x and y as variables like that when trying to explain something, it can be misleading...

Response to AS:Math - Simple 2006-06-22 16:51:53


Amazing tut, very well explained, and i learned alot from thism, i am currently making a game and i will definetly credit you, Thank you again

Response to AS:Math - Simple 2007-12-18 16:22:00


Sorry to post again since the thread's so old, but how do i get a random number between 260 and 380?

Response to AS:Math - Simple 2007-12-18 16:24:35


At 12/18/07 04:22 PM, Theromy wrote: Sorry to post again since the thread's so old, but how do i get a random number between 260 and 380?

how many numbers are between 260 and 380? 380-260 = 120.

so Math.random()*120+260

Response to AS:Math - Simple 2007-12-18 16:26:05


At 12/18/07 04:24 PM, dELtaluca wrote:
At 12/18/07 04:22 PM, Theromy wrote: Sorry to post again since the thread's so old, but how do i get a random number between 260 and 380?
how many numbers are between 260 and 380? 380-260 = 120.

so Math.random()*120+260

I was so close, this was mine:

k = (Math.ceil(Math.random()*260)+120)

Response to AS:Math - Simple 2007-12-18 16:40:34


At 12/18/07 04:24 PM, dELtaluca wrote:
At 12/18/07 04:22 PM, Theromy wrote: Sorry to post again since the thread's so old, but how do i get a random number between 260 and 380?
how many numbers are between 260 and 380? 380-260 = 120.

so Math.random()*120+260
function randomr(num1:Number, num2:Number):Number {
return (Math.random()*(num2-num1))+num1;
}

trace(randomr(260,380));

Lolol, mad a function of it!
Also threw in a little bit o' web twopointoh.


BBS Signature

Response to AS:Math - Simple 2008-05-09 07:38:50


I thought it best to post in here instead of creating a new thread:

I'm trying to make a simple game to teach myself more about AS. It's pretty much based on stats and numbers, like those Sim games there are a million of. For example:

Money: £100
Health: 100
Energy: 57
Hygiene: 21

etc. etc.

Basically, I know how to make certain buttons adjust the values of all the variables, but I can't quite figure out how to make the following happen:

Say 'Health' is at 95, and the maximum I want it to be able to go to is 100, but you do something like Eat, and it has a typical Health increase of 20. How do I make it so it doesn't become 115 but goes to 100 and stops? And the same goes for when an action removes Health, how do I make it so that if the reduction in health is more than the gap between 0 and the current value, it doesn't go into the minuses, but the player still dies etc.?

Thanks!

Response to AS:Math - Simple 2008-05-09 08:17:14


At 5/9/08 07:38 AM, Greensinge wrote: I thought it best to post in here instead of creating a new thread:

Create a new thread next time.

Say 'Health' is at 95, and the maximum I want it to be able to go to is 100, but you do something like Eat, and it has a typical Health increase of 20. How do I make it so it doesn't become 115 but goes to 100 and stops?
 if (health > 100) health = 100;

BBS Signature

Response to AS:Math - Simple 2008-05-09 08:27:40


At 5/9/08 08:17 AM, GustTheASGuy wrote: Create a new thread next time.

Can I just ask why? I didn't see the point in wasting a thread when you just answered my question in one post, that's all! Other people asked questions here too, I didn't think it was such a problem.

if (health > 100) health = 100;

Thanks for your help!

Response to AS:Math - Simple 2010-04-02 14:46:06


good stuff, AS main is the greatest thread I've discovered today/

Response to AS:Math - Simple 2010-04-02 15:26:31


At 4/2/10 02:46 PM, BeefJuice wrote: good stuff, AS main is the greatest thread I've discovered today/

cool story mate. next time you discover the greatest 5 year old thread ever, don't bump it.


BBS Signature

Response to AS:Math - Simple 2010-04-02 15:40:07


At 4/2/10 03:26 PM, Toast wrote:
At 4/2/10 02:46 PM, BeefJuice wrote: good stuff, AS main is the greatest thread I've discovered today/
cool story mate. next time you discover the greatest 5 year old thread ever, don't bump it.

HAI TOAST!


BBS Signature

Response to AS:Math - Simple 2010-04-02 18:02:04


Whoa man, 5 years old? I didn't even notice. Sorry.