Forum Topic: AS:Math - Simple

(4,286 views • 15 replies)

This topic is 1 page long.

<< < > >>
None

T-H

Reply To Post Reply & Quote

Posted at: 7/3/05 02:52 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

!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


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/3/05 02:53 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/3/05 02:54 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

T-H

Reply To Post Reply & Quote

Posted at: 7/3/05 02:55 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


None

Toast

Reply To Post Reply & Quote

Posted at: 7/3/05 02:59 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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


None

T-H

Reply To Post Reply & Quote

Posted at: 7/3/05 03:01 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/3/05 03:17 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948


None

Snubby

Reply To Post Reply & Quote

Posted at: 5/28/06 03:07 PM

Snubby EVIL LEVEL 20

Sign-Up: 12/04/04

Posts: 3,146

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...


None

Plumb

Reply To Post Reply & Quote

Posted at: 6/22/06 04:51 PM

Plumb NEUTRAL LEVEL 07

Sign-Up: 05/06/06

Posts: 132

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


None

Theromy

Reply To Post Reply & Quote

Posted at: 12/18/07 04:22 PM

Theromy NEUTRAL LEVEL 02

Sign-Up: 10/30/07

Posts: 22

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


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 12/18/07 04:24 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

None

Theromy

Reply To Post Reply & Quote

Posted at: 12/18/07 04:26 PM

Theromy NEUTRAL LEVEL 02

Sign-Up: 10/30/07

Posts: 22

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)


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 12/18/07 04:40 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

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

Resigned

Greensinge

Reply To Post Reply & Quote

Posted at: 5/9/08 07:38 AM

Greensinge LIGHT LEVEL 11

Sign-Up: 04/27/03

Posts: 483

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!


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 5/9/08 08:17 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

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;

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Greensinge

Reply To Post Reply & Quote

Posted at: 5/9/08 08:27 AM

Greensinge LIGHT LEVEL 11

Sign-Up: 04/27/03

Posts: 483

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!


All times are Eastern Standard Time (GMT -5) | Current Time: 01:48 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!