View comments | RSS feed

Variables and constants

Since programming mainly involves changing pieces of information in the computer's memory, there needs to be a way to represent a single piece of information in the program. A variable is a name that represents a value in the computer's memory. As you write statements to manipulate values, you write the variable's name in place of the value; any time the computer sees the variable name in your program, it looks in its memory and uses the value it finds there. For example, if you have two variables named value1 and value2, each containing a number, to add those two numbers you could write the statement:

value1 + value2

When it's actually carrying out the steps, the computer will look to see the values in each variable, and add them together.

In ActionScript 3.0, a variable actually consists of three different parts:

We've just discussed how the computer uses the name as a placeholder for the value. The data type is also important. When you create a variable in ActionScript, you specify the specific type of data that it will hold; from that point on, your program's instructions can store only that type of data in the variable, and you can manipulate the value using the particular characteristics associated with its data type. In ActionScript, to create a variable (known as declaring the variable), you use the var statement:

var value1:Number;

In this case, we've told the computer to create a variable named value1, which will hold only Number data ("Number" is a specific data type defined in ActionScript). You can also store a value in the variable right away:

var value2:Number = 17;

In the Adobe Flash CS3 Professional, there is another way to declare a variable. When you place a movie clip symbol, button symbol, or text field on the Stage, you can give it an instance name in the Property inspector. Behind the scenes, Flash creates a variable with the same name as the instance name, which you can use in your ActionScript code to refer to that Stage item. So, for example, if you have a movie clip symbol on the Stage and you give it the instance name rocketShip, whenever you use the variable rocketShip in your ActionScript code, you will in fact be manipulating that movie clip.


Flash CS3


Comments


SindiT said on May 30, 2007 at 5:10 PM :
The above suggests that ActionScript 3 requires that variables be 'strongly' typed. By this, I mean, that all variables must be explicitly declared and be given a type. This would be nice, particularly since variable names are case sensitive. If this is not the case, it would be nice to know if there is a way to force explicit variable declaration.
No screen name said on Jul 22, 2007 at 1:55 PM :
The title of this article is "Variables and constants" but no mention of constants in the text.
defanual said on Mar 3, 2008 at 5:52 AM :
According to the help docs (and after testing), you basically replace 'var' with 'const' to set up a constant.

So var value2:Number = 17; becomes const value2:Number = 17;

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000015.html