
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Variable Scope
|
51
and then we check the value of day inside circle:
trace(day);
the Output window displays “Friday” (the timeline variable’s value), not “Monday”
(the global variable’s value). For more information on global variables, see
_global in
the Language Reference.
Though true global variables were not supported in Flash 5, it was possible to simu-
late them using the Object class or MovieClip class. To create a variable that is avail-
able on all timelines in Flash 5, use the following statement:
MovieClip.prototype.myGlobalVariable = myValue;
For example:
MovieClip.prototype.msg = "Hello world";
Alternatively, assign the variable as a property of Object, as follows:
Object.msg = "Hello world";
Then, from any other timeline, access msg like this:
trace(Object.msg);
We discuss this technique (and the reason it works) in “The end of the inheritance
chain,” in Chapter 12. In Flash MX, the
_global object is the preferred way to store
global variables.
Accessing Variables on Different Timelines
Even though variables on one timeline are not accessible directly to the scripts on
other timelines, they are accessible indirectly. To create, retrieve, or assign a variable
on a separate timeline, we use dot syntax, a standard notation common to object-ori-
ented programming languages such as Java, ...