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.

No comments: