Condition Statements
The if statement in ActionScript is the same as it is in many programming languages:
if (myValue == 1) {
doSomething();
}
The == comparison checks for general equality. You can also use >, <, >=, and <= respectively.
You can add else and else if to extend the if structure:
if (myValue == 1) {
doSomething();
} else if (myValue == 2) {
doSomethingElse();
} else {
doNothing();
}
You can also have more complex conditions with && and || . These represent the and and or comparison operators.
NOTE
You can no longer use keywords "and" and "or" as alternates to && and ||.
Loops
Loops is done with the for statement or the while statement. For statement has three parts:
initial statement, a condition, and a change statement.
for(var i:int=0;i<10;i++)>
doSomething();
}
Translation: The following code sets the variable i to zero, loops as long as it is less than ten, and increases the value of i each time through the loop.
February 2, 2008
February 1, 2008
ActionScript 3.0 Variable Syntax
Storing values in ActionScript 3.0 can be done with a simple assignment statement. However, you need to declare a variable first to use it.
VARIABLE
var myValue = 3;
declare variable first and use it later
var myValue;
NOTE
In ActionScript 2.0, the var statement is not required. In ActionScript 3.0, it is.
An int variable type can be any integer, positive or negative. A uint integer is only for positive integers. If you want to use fractional values, also known as floating-point numbers, you must use the Number type:
var myValue:Number = 7.8;
There are also String and Boolean types.
Strings hold text
Boolean values must be either true or false.
These are basic primitive types. However, you can also have arrays, movie clip and sprite references, and new types that match the code classes you create.
Operations on numeric variables:
var myNumber:Number = 7.8+2 (floating-point)
var myOtherNumber:int = 5-6; (subtraction)
var myOtherNumber:Number = myNumber*3; (multiplication)
var myNextNumber:Number = myNumber/myOtherNumber; (division)
Special Operators
The ++ will increment a variable by one
The -- will decrease it by one
Peform Operation on the original value
+=, -=, *-, and /=
myNumber += 7;
Use parenthesis to set the order of the operations
var myNumber:Number = (3+7)*2;
Strings can also be manipulated with the + operator and the += operator:
var myString:String = "Hello";
var myOtherString = myString+"World";
myString += "World";
Private variables cannot be accessed by code outside of the class. We must define them further as either private or public.
VARIABLE
var myValue = 3;
declare variable first and use it later
var myValue;
NOTE
In ActionScript 2.0, the var statement is not required. In ActionScript 3.0, it is.
An int variable type can be any integer, positive or negative. A uint integer is only for positive integers. If you want to use fractional values, also known as floating-point numbers, you must use the Number type:
var myValue:Number = 7.8;
There are also String and Boolean types.
Strings hold text
Boolean values must be either true or false.
These are basic primitive types. However, you can also have arrays, movie clip and sprite references, and new types that match the code classes you create.
Operations on numeric variables:
var myNumber:Number = 7.8+2 (floating-point)
var myOtherNumber:int = 5-6; (subtraction)
var myOtherNumber:Number = myNumber*3; (multiplication)
var myNextNumber:Number = myNumber/myOtherNumber; (division)
Special Operators
The ++ will increment a variable by one
The -- will decrease it by one
Peform Operation on the original value
+=, -=, *-, and /=
myNumber += 7;
Use parenthesis to set the order of the operations
var myNumber:Number = (3+7)*2;
Strings can also be manipulated with the + operator and the += operator:
var myString:String = "Hello";
var myOtherString = myString+"World";
myString += "World";
Private variables cannot be accessed by code outside of the class. We must define them further as either private or public.
Subscribe to:
Posts (Atom)