Understanding Scope

In Lesson 6, you learned about some of the things that make up ActionScript, such as variables, keywords, and data types. You also learned about how dot notation works and a bit about how dot notation is used to construct lines of ActionScript. Now you will take a look at where ActionScript lives in a FLA file.

One of the most important and sometimes confusing parts of ActionScript is getting a good grasp of how variable scope works and how it is used when you are writing code and working with Flash files. A scope is the area of your FLA file in which a variable can be referenced. That means if a variable exists in a certain place, such as within a function or on a particular Timeline, that is where its scope is. Understanding how scope works (and where your variables are) can take some practice and patience, and a certain amount of experience. As you test many different scenarios with your own personal projects, you quickly get a good grasp about how scope affects a Flash file. It might take some practice to get the hang of scope and see how it affects your code firsthand, so don't worry if it doesn't make sense right away.

In Lesson 6, you found out that variables are similar to containers that hold a piece of data, and you learned a bit about how to name them. A very important rule to remember is that two variables cannot have the same name. However, a variable can have the same name if they are in a different scope. This indicates how the code lives in different areas of your SWF files. There are three available scopes within Flash, as discussed in the following sections.

Local variables: These variables are available only when a function is called. Local variables are those inside the two curly brackets of a function, as you just saw in an earlier exercise. Outside of this function (when it's not being called), these variables do not exist.

Local variables are defined within a function using the var keyword and no longer exist when the function exits (when it's done). This means that the variables you use inside the function cannot be used in other places on the Timeline or in your code, which is a good thing most of the time because you won't have conflicts in other pieces of ActionScript with variables that might use the same name. Another benefit is that Flash uses fewer resources because it doesn't have to keep track of a large number of variables, which are no longer being used in your application. The local variables exist only for the life of the function and then disappear.

An example of a local variable is as follows:

function myVariable() {  var myNum:Number;  //myNum variable exists here } //myNum no longer exists. trace(myNum); //undefined

When you trace the myNum variable outside of the function, undefined is returned because the variable no longer exists outside of the myVariable function. Remember that the trace statement can be used to test your code and send messages to the Output panel when you test a document.


Although it is possible to use the same variable names in functions and in other scopes without naming conflicts, this isn't always advisable. You should avoid using the same names whenever you can because the practice can lead to confusion when editing your code at a future date if you have similarly named variables all throughout your FLA file in different scopes.

Timeline variables: These variables are available to any script only within the same Timeline. Remember that your SWF file can have more than one Timeline because a Timeline can exist on a different level or in a movie clip or component. Levels will be defined in the following section. If you have more than one Timeline, there can be different Timeline variables in each of these areasand all have the same name without running into conflicts.

When a variable is defined in a Timeline, that variable is available on frames after the variable has been defined. For example, if the following code were placed on Frame 10, it would create a variable named numUsers in the main Timeline. That variable exists throughout the Timeline after Frame 10. Before Frame 10 plays, that variable is not available in the SWF file.

var numUsers:Number = 5;

Global variables: These variables are available to any of the Timelines, scopes, or functions within the SWF file. Therefore, you can declare a global variable and then use the variables in other SWF files that are loaded into the main SWF file, and throughout the entire main SWF file without making any changes to your ActionScript or file structure. Global variables are slightly different from the first two scopes because they are not defined with the var keyword and are prepended with the keyword _global, as follows:

_global.numUsers = 5;

Because you cannot use the keyword var when defining global variables, you cannot use strict data typing with global variables. If you want to take advantage of code hinting with global variables, you must use the suffix method (appending _mc or _lv onto the end of the variable) or the comment method.

Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net