
_global Object
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
540
|
ActionScript Language Reference
_global Object
container for global variables
Flash 6
_global.variableName
Description
The _global object enables the creation of global variables (i.e., variables that are available
from the timeline of any movie clip or level). Unlike in JavaScript, in ActionScript there is
no “global execution context” where variable definitions automatically create global vari-
ables. All nonlocal variables created in ActionScript are scoped to the movie clip in which
they are created. To make a variable directly accessible throughout all timelines in a movie,
we must deliberately add a property to
_global. For example:
// Add a property, age, to _global. Creates a global variable, age.
_global.age = 31;
// Access age from any timeline:
trace(age); // Displays: 31
Using global variables, we can make a function, property, or object available globally, much
like the built-in classes and objects (Math, TextField, Mouse, etc.). For example, if we want
to make the CountDown class a global class, we can use the following code (see Function.
apply( ) for the full listing for CountDown):
_global.CountDown = function (obj, meth, delay) {
// ... Class code here
};
Then, from any timeline, we can create instances of CountDown directly, as in:
theTimer = new CountDown(game, ...