Defining a Variable
Flash CS4 ActionScript 3.0 Tutorial
How to Assign Variable Names and Define Variable Data Type with Flash ActionScript
Variables are similar to the variables we used in math class. They store a value, series of letters or numbers or a true/false state. The value assigned to a variable can change based on the script and events on the stage.
Naming a Variable
When naming variables, try to observe these rules:
Use numbers and letters
Avoid using any special characters
Character exceptions are ($, _)
Don't use any reserved words (they appear in blue)
Never start a variable with a number
Assign a name that makes sense
A common practice when naming variables and instances is to use camel case. Camel case variables start with small letters. Each new word starts with a cap. There are no spaces.
Blue Cylinder converted to camel case becomes blueCylinder.
If a variable stores a value, it is generally a number (all numbers), int (integer), or uint (positive integer). The following are examples of each:
Number: 1.325, 5.6, 410
int (integer): 3, 5, 12, -10, -30
uint (positive integer): 3, 7, 13
Strings are single blocks of numbers, letters or symbols. A string can have one or multiple words. The string's value is framed with quotation marks. Here are some examples of strings:
"Insert Another Token"
A boolean variable is often used with If statements where you run a test to determine if it is true or false. The numbers 0 and 1 can be substituted for false and true, respectively.
Option double-click (PC - Alt double-click) the first keyframe of a new blank document to open the ActionScript panel.
Add the following code to establish a number variable named averageScore with a starting value of 21.25. To display output, press Cmd/Return (PC - Ctrl.Enter).
var averageScore:Number = 21.25;
This code creates a integer variable named rouletteChips with an initial value of 0.
var rouletteChips:int = 0;
If you wanted to create a variable called totalVisitors that would only allow positive whole values and is assigned the initial value of 1, you could use the following code.
var totalVisitors:uint = 1;
This code creates a string variable named userInstructions with the initial value of Press Start to Play.
var userInstructions:String = "Press Start to Play";
To establish a boolean (true or false) variable named overLimit with the value of false, you could use the following code.
var overLimit:Boolean = false;
Why the trace() Statement?
In each of these examples we also included the trace statement. The trace statement allows you to see the variable value in the Flash Output panel whenever you run the script.
Trace statements are helpful in troubleshooting because you can view a variable's value at a particular point in the script.
Where Does uint Get Its Name?
A uint is a positive integer. The appreviation, uint, stands for unsigned integer. In other words, since it has no sign, the integer is always positive.