Defining a Number Variable
Flash CS4 ActionScript 3.0 Tutorial
How to Assign Variable Names and Define the Number Data Type with Flash ActionScript
Variables are similar to the variables we used in math class. They store a value and over time the variable can be assigned several different values. The value assigned to a variable can change based on the script and events on the stage. For more information about number variables, see topic, Interpreting a String as a Number.
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
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.
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;
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.