2011-09-13

FLASH CS3 - Action Script part 6 (Math)

Let's Study Math, a Math is basic for all kind programing basic. Not only simple like multiply, divide or other.

You can calculate something complex like Physic or event Statistic This part not give you the script but is good if you read some of the basic math function we can found in Actionscript.

For complex math problem will be learn soon in another tutorial. For example how to found the size of circle (we need Pi for this.) We soon meet this kind of assigment in your program.
n = n + 1 ;
You can short it by type
n++;
if you have like this
n = n + 12;
you can type
n+=12;
For another type like substract we have like this
n = n - 5;
like above.. we can type like this
n-=5;
For divide
n /=2;
for multiply
n*=5;

for divide and multiply ... seem never used in real life. In order to use in the script.. you need declare the N variable too. like this
var n:Number = 5;
then you can use it in the script
full example
public function ExampleApplication(  ) {
     var n:Number = 5;
     n+=5;

}

Thanks for reading.. Now you can read the next issue equality and expresion.

TRUE OR FALSE

Now we will learn about TRUE and FLASH err FALSE

Knowing TRUE or FALSE make you able to control the program flow.. For example I want to enter the room, that's need a key.. I need a Key to pass it.. If I have the key.. I will pass it.. if don't I can't pass it

in Script we have

if(I_HAVE_KEY)
{
     trace('you can pass it');
}else{
   trace('you can't pass it');
}
This is only a simple expresion.. Next you will learn there perhaps there was soo much condition we must follow to make some action can be execute. In this example I give one.. but in real.. Perhaps not only a key I need.. I must have the keycode.. Or we can make another example..
I like Nasgor and I eat Nasgor.. in script I have
if( like == 'nasgor' && eatNow == 'nasgor' )

In Flash .. sometimes we forgot when we dealing string/char and number? like this example.. I have 6 Mellon (6 is number) with I have "6" Mellon .. (6 is string)... If we compirasion betwen 6 and "6".. the script will tell betwen two is TRUE (not False).. The reason.. flash convert the string into number first
if( 6 == '6') >> true
same as
if( 6 == 6) >> true
or
if( '6' == '6') >> true
Please to remember.. this Equality Operator isn't strict

other than comparison we have inequality operator ( != ), if we use in the script like this.. we can have
    if( != )       //false
    
if( != '6' )     //false
    
if( != )       //true
    
if( != '5' )     //true  

Just we can see here.. the  inequality operator isn't strict..
Let's focus on  equality.. We need  equality operator that's strict.. soo I don't have to worry about the comparison in the script.
    trace(=== 5);    // Displays: true
    
trace(=== "8");  // Displays: false
    
trace(!== 8);    // Displays: false
         
    trace(!== "5");  // Displays: true 
This script contain strict inequality too.. Other than this.. there's something you should take notice.. Sometimes you type like this
   trace(u 5);    // Displays: true
   
  

That code will return wrong!! Because the script mean variable U have value 5 not variable U same as 5!!

For your notice.. when you go depper into Flash and try to trace value of empty variable.. you get NaN !! NaN in Flash mean empty and NaN same as number 0!!
if you want to know what type of this variable.. try trace it like this
trace(typeof quantity);

Okay.. not only you can comparion using quality & enquality..
You can use < and > operator or combine with == like this


if( n < 56)
if( n < = 56)
etc..

This operator will give you different result like n <56 That make if n less than 56 , he have  to execute the function. But beware of this 
n<=45
It mean.. if n same as 46, the function still working. How to use this.. Next Tutorial

Tidak ada komentar: