How to target variables in a Flash Player 5 movie
Variables in ActionScript
One feature of ActionScript that is shared with other scripting languages is the ability to use variables. Variables are characters that can be created as placeholders for numerical and string data. They can be updated and accessed dynamically, which provides a great deal of flexibility and interactivity in a Flash movie.
Creating a variable is called a declaration. Variables can be set, accessed and updated anywhere in a Flash movie. They can be set any place a user can attach code on a frame, a button, or a movie clip using the action Set Variable or by values entered in a text field. Once set, the variable and its value exist in the timeline that they were set in. In other words, if a variable is set inside the main timeline, its name and value do not exist as such in other timelines, including movie clips and loaded movies. Variables set inside one timeline are not directly accessible to the scripts on other timelines, yet they are indirectly accessible. To create, retrieve, or assign a variable on a separate timeline we address the path where the variable located using dot syntax. Dot syntax is a standard notation common to object-oriented programming languages such as JavaScript. This path addressing is similar to using the With or Tell Target action for targeting objects in a Flash movie. Variables inside instances of movie clips are specified with the dot syntax instanceName.variableName, as explained on page 38 of the Flash 5 ActionScript Reference Guide.
A variable's name can depend on where it exists. To access a variable in another timeline, use an absolute or relative path. Absolute paths to variables begin with _root, indicating that the path starts from the main timeline. This is similar to accessing a root directory on a server. Relative paths can refer to timelines in the context of the current timeline using _parent. If the instance of a movie clip exists on the current timeline, a variable named x inside of it could be simply referred to as _parent.x, using its relative path name. The absolute path would be _root.mc.x.
Relative paths starting with a _parent indicate one level up from the current timeline. If a variable called x is set in the main timeline from a movie clip, it can be referred to as _parent.x (relative path) or _root.x (absolute path).
A variable can also be accessed in a loaded movie. In this case, the level that the movie has been loaded into must be identified first before identifying the variable. A variable named x in a movie loaded into level 1 can be accessed from the parent movie in level 0. The path would be _level1.x.
Using an absolute path versus a relative path
Absolute paths are constant. The path is the same, no matter where they are used in the movie. People who are new to variables and scripting may find absolute paths easier to work with. Relative paths are scalable. A movie clip in the library that has actions referring to variables with a relative path can be reused as objects. They will always refer to the timeline that they are nested in.
Things to watch out for:
- If you are comparing variables using an If statement, make sure you are using the appropriate operator for that kind of variable. For instance, the statement If (x = 5) is using an equals sign to evaluate a variable called x that has a numerical value. A statement such as If (x = "5") would be using the string operator to evaluate the variable with string data "5." It is important to note that the quotes around the 5 display a string value and not a numeric value.
- It is possible to make very complex movies with multiple timelines. Often, names of variables will be duplicated in different timelines and scripting can become quite complicated. There are ways to troubleshoot variables or check their values using Flash techniques.
- When viewing your movie in Test Movie mode, you can choose Debug> List Variables to see an inventory of all declared variables and values, in all timelines of your movie.
- To view the value of a variable when a specific action has occurred, use the Trace action statement. You can enter the name of the variable in the Trace dialog box. It will appear in an output window while in Test Movie mode.
- Putting a text field on the stage with the variable name may provide a visual guide to determining if and when a variable is defined. A text field can also be given an absolute or relative variable name in the Text Options panel.
Additional information
For more details on the differences between _root and _level0, please refer to Differences between _root and _level0 (TechNote 14686).
For information about variable paths in Flash 4, please refer toHow to target variables in a Flash movie (TechNote 14087).
This content requires Flash
To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.
Download the free Flash Player now!
