Printer Friendly Version

BellaOnline's Flash and Animation Editor

The Flash ActionScript Variable

The variable is the workhorse of any programming language. In Flash, it is one way that ActionScript can talk to the Flash program. Because ActionScript is based on the ECMAScript international standardized programming language for scripting, it makes sense that ActionScript will work in the same way as other scripting languages. So what is a variable and how does it work in ActionScript? You can think of a variable as a container or storage box for information. When you place information inside a variable, this is called assigning a value to the variable. The value, or information, will stay there until you change the value in some way by writing more ActionScript.

How do you create a variable in ActionScript? The easiest way is to use the variable in your program. Flash will recognize that it has never encountered this variable before and will make it "official". Although this method works and is perfectly acceptable, most programmers find it helpful to maintain more control. One way is by defining a variable using the var keyword. For example, let's create a variable that we will call "container" and assign to it an initial value to 20. Here is the code. Let's take a look at it and then break it down.

var container;
container = 20;

Notice that both lines of the code above end with a semicolon. The semicolon ends a line of program code much like a period ends a sentence. If you do not use the period at the end of your sentences, no one will be able to tell when one sentence ends and the next one begins. It's the same with programming. Don't forget those semicolons.

Let's take a look at the first line of code. It begins with the var keyword which is followed by the name we want to give to our variable. It is telling Flash that we are creating a new variable and we are naming it "container".

The second line of code assigns an initial value to the variable. This code might look familiar to those of you who have studied algebra. In algebra, x = 1 means that "x is equal to 1". The equal sign means "equal to" in algebra. However in ActionScript, the equal sign means "assign the value". Therefore, the second line is telling Flash to assign the value of 20 to the variable named "container".

The two lines of code above can be condensed into one line.
var container = 20;

Flash and Animation Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.

Website copyright © 2015 Minerva WebWorks LLC. All rights reserved.

BellaOnline Editor