Variables in ActionScript3

A variable is a named location in memory where you can store values. Before you can use a variable, you must declare it using a good name and a type. You can also give it an initial value. For these examples, we will use int (integer values)

Declaration:

A variable dx is declared as shown below. Notice that a variable can be given an optional initial value when it is declared.

  var dx: int=0;
  var dy: int=0;

Scope

  • If you declare a variable inside a function, it is known only inside that function.
  • If you want to use a variable anywhere in the movie, declare it in frame 1, but not inside any function.
  • A variable that is declared outside of any functions is said to be global.
  • A variable that is declared inside a function is said to be local to that function.

Assignment Statements

A variable can be assigned a value with the assignment statement.
The format is: variable = newvalue;
Examples:
      The new value can be a constant: dx = 5;
      The new value can be another variable: dx = dy;
      The new value can be a property: dx = ball.y;
      The new value can be an expression: x = y/3;
      It can even use its current value in the expression: x = x + 1;
      The last statement is called incrementing and can also be written: x++;
      The last two statements assign a new value to x that is one more than its current value.

NEXT: Blast Off!