Variables

Variables

Flash CS4 ActionScript 3.0 Tutorial

 

How to Assign Variable Names and Define the 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. For more information about variables, see topics, Defining a Variable, Interpreting a Number as a String, Interpreting a String as a Number and Requesting Variable Type.

Naming a Variable

When naming variables, try to observe these rules:

No spaces

Use numbers and letters

Avoid using any special characters

Character exceptions are $ or _ (underscore)

Don't use any reserved words (they appear in blue)

Never start a variable with a number

Assign a name that makes sense

Small Camel Case

A common practice when naming variables and instances is to use camel case. Small camel case variables start with small letters. Each new word starts with a cap. There are no spaces.

Blue Cylinder converted to small camel case becomes blueCylinder.

Big Camel Case

A common practice when naming classes and Library assets, file names and layer names is to use big camel case. Big camel case names start with capital letters. Each new word starts with a cap. There are no spaces.

Blue Cylinder converted to big camel case becomes BlueCylinder.

Numbers

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

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:

"You Won!!"

"Insert Another Token"

Boolean

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.

 

Step One

Option double-click (PC - Alt double-click) the first keyframe of a new blank document to open the ActionScript panel.

Step Two

Add the following code to establish a number variable named averageScore with a starting value of 21.25. Test your movie by pressing Cmd/Return (PC - Ctrl/Enter).

var averageScore:Number = 21.25;

trace(averageScore);

Step Three

This code creates a integer variable named rouletteChips with an initial value of 0. Test your movie by pressing Cmd/Return (PC - Ctrl/Enter).

var rouletteChips:int = 0;

trace(rouletteChips);

Step Four

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. Test your movie by pressing Cmd/Return (PC - Ctrl/Enter).

var totalVisitors:uint = 1;

trace(totalVisitors);

Step Five

This code creates a string variable named userInstructions with the initial value of Press Start to Play. Test your movie by pressing Cmd/Return (PC - Ctrl/Enter).

var userInstructions:String = "Press Start to Play";

trace(userInstructions);

Step Six

To establish a boolean (true or false) variable named overLimit with the value of false, you could use the following code. Test your movie by pressing Cmd/Return (PC - Ctrl/Enter).

var overLimit:Boolean = false;

trace(overLimit);

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.