Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course
In this section, you will learn about:
- Declarations and Scope
- Data Types
- Loading external variables
- Sending variables out of Flash
Variables are used to store changing values, usually text values, such as names, or numerical values. Input and dynamic text fields can have variables assigned to them using the Property Inspector. For example, the screen below shows a movie with two input text fields: one for entering a first name and one for entering a last name. The first name text field (FirstName_txt) uses the variable varFirstName to store the user’s first name. When the text field is selected, you can see the variable in the var field of the Property Inspector:
To assign a variable to the last name field (LastName_txt):
- Select the input text field.
- In the Property Inspector, type the variable into the var field:
Values stored in variables assigned to input text fields can then be displayed using dynamic text fields. For example, to display the names the user enters into the fields above, you could add code to the button that advances the timeline to the next frame, which displays the whole name in a dynamic text field, along with a welcome message:
- The movie shown above has only one keyframe, which contains the input fields, their labels (static text), and the button. We’ve added a stop action to the first frame of the Actions layer to stop the timeline from moving out of the frame before the user clicks the button. We need to add code to the button to advance the movie to the next frame: Add an on(release) event handler and, between the curly braces, add the nextFrame action, as shown below.
You can either type nextFrame(); directly into the Script pane, or you can double-click the nextFrame action in the Timeline Control folder.
- Add a keyframe to the second frame of each layer and delete the text, fields, and button from it.
- In the second keyframe, on the Actions layer, add a stop action to prevent the movie from progressing out of the second frame.
- On the layer you’re using for your fields (also in the second keyframe), draw a text field using the Text tool. In the Property Inspector, select Dynamic as the text field type. Then set the text display properties and name the instance “welcome_txt”:
- You can display the variables’ contents either by adding code to the Actions panel, or by entering the variable you want to display into the var field of the Property Inspector. For example, type varFirstName into the var field and test the movie:
The dynamic text field displays the value of the variable varFirstName:
However, we want to display the entire name, along with a “welcome”, so delete the variable from the dynamic text field’s var field. Then select the second frame of the Actions layer and, before the stop action, type the following code:
this.welcome_txt.text = “Welcome, ” + varFirstName + ” ” + varLastName;
As you learn more about objects in ActionScript, you’ll recognize “.text” as a property of the TextField object, specifically responsible for the text that the object displays. In this case, the text field is displaying the word “Welcome” plus the contents of the variable varFirstName, plus a space, plus the contents of the variable varLastName. Text that you want to appear is enclosed in quotation marks, while variables are not.
In ActionScript, you can concatenate (join) variables and other text using the plus (+) sign (addition operator).
- Test the movie, entering a first and last name.
Declarations and Scope
You just learned about using the var field to assign variables to text fields. You can also use the var keyword to declare a variable in your code. For example:
var name = “Jane”;
This code assigns the string value “Jane” to the variable name. You can use the same method described above to display the contents of the variable name (“Jane”) in a dynamic text field (myText_txt.text = name;).
When you use the var statement to declare a variable, you limit its scope to the current location—whether that’s the main timeline, a movie clip timeline, or a function. Timeline variables are available only to subsequent frames in the timeline. For example, if you declare a variable in Frame 5 of the main timeline, script appearing before Frame 5 won’t be able to access it. Local variables—those declared within functions—are available only to those functions and the code calling them.
Like movie clips, you have to access a variable by targeting its location in the hierarchy. For example, the statement below assigns the value of the variable FirstName, which is located in the userwelcome_mc movie clip, to the variable user, located on the main timeline:
var user = _root.userwelcome_mc.FirstName;
If the variable FirstName were located on the main timeline, you could use:
var user = FirstName;
Or you could use:
var user = _root.FirstName;
For Flash Player 6 and later, you can use the “_global” keyword to declare a global variable, which is accessible from any object or timeline in the movie:
_global.FirstName = “Jane”;
To access the value of the global variable FirstName (“Jane”) from within a movie clip, you then need only use the variable itself:
welcome_txt.text = FirstName;
Most ActionScript programmers prefer to use local variables as much as possible to keep processing efficient.
ActionScript 2.0 uses strict data typing. This simply means that when you declare a variable, you can assign its data type, which may be a string, a number, a Boolean value, an object, or something else. You assign the data type by adding a colon : to the variable and then typing the variable’s data type:
var count:Number = 0;
The String Data Type: In the previous section, we assigned string variables to text fields. A string is a sequence of characters, which can include letters, numbers, and punctuation marks. String values are enclosed in quotation marks:
var FirstName:String = “Jane”;
You also learned that you can concatenate strings and other text using the plus (+) sign (addition operator):
var Welcome:String = “Welcome, ” + varFirstName + ” ” + varLastName;
Above, if the values of the variables varFirstName and varLastName were “Jane” and “Smith”, respectively, then the value of the variable Welcome would be: “Welcome, Jane Smith”.
Some ActionScript programmers use the “var” prefix to name a variable. This simply reminds them that the word is a variable, and not, for example, an instance of a symbol. Don’t confuse the “var” prefix, as in the variable varFirstName, with the var keyword used in a declaration. We could have named the Welcome variable varWelcome instead, in which case we would have declared it using:
var varWelcome:String = “Weclome…”;
Some characters can’t be represented in ActionScript without using what’s called a special “escape” sequence. For example, if you need to include a quotation mark in the value of a string variable, you’d need to precede it with a backslash (\); otherwise, the quotation mark would be interpreted as ending the string value. Other characters that need to be escaped are the single quotation mark, the backslash, the backspace, and the line-feed characters.
You can also use Flash’s escape function to automatically convert special characters like spaces to their URL-encoded equivalent:
var emailsubject:String = escape(“Support Request”);
This is necessary, for example, when you’re sending variables out of Flash using a query string.
The Number Data Type: The number data type uses a numerical value, which can be manipulated using the addition (+), subtraction (-), multiplication (*), division (/), modulo (%), increment (++), and decrement (–) operators, as well as the methods of the Math and Number classes.
For example, the following statement assigns a value of 1 to the number variable count:
var count:Number = 1;
To increment the value of the variable count, you’d use: count++; (which is equivalent to the statement: count = count + count;).
The Boolean Data Type: A Boolean is a true or false value.
The Object Data Type: You can use ActionScript to create custom objects in Flash. The code below creates a new object called user, which has properties called FirstName and LastName:
var user:Object = new Object();
user.FirstName = “Jane”;
user.LastName = “Smith”;
Other data types are null, undefined, and void. The null data type can be assigned when a variable doesn’t yet have a value. In addition, null can be returned by a method or function if no value has been assigned. The undefined data type is automatically assigned when a value hasn’t been specified for a variable. For example, in the movie we created earlier using input text fields to capture a user’s first name and last name, the dynamic text field in the second frame would have displayed something like the following if a last name wasn’t entered:
To avoid this effect, we would need to insert some code to tell Flash what to do if the user only completed one of the fields.
The void data type is used to indicate that a function doesn’t return a value.