_global Object | Flash 6 |
container for global variables |
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 variables. 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, "timesUp", 10000);
Global variables do not become properties of all objects. For example, you cannot access the global variable age as a property of a Date object:
_global.age = 31; // Make the global variable now = new Date(); // Make the Date object trace(now.age); // age is not a property of now. Displays: undefined
Similarly, global variables cannot be accessed on movie clips via property-style dot notation. For example, a global variable named password cannot be accessed as form_mc.password (where form_mc is a movie clip). Similarly, from form_mc's timeline, password cannot be accessed as this.password. The global variable password can be accessed only as _global.password or directly as password (without any qualifying object reference) from the current timeline.
Assigning a timeline variable with the same name as a global variable overrides (but does not overwrite!) the value of the global variable. For example, if a movie clip named player1_mc assigns the variable age a value of 10, then age will contain 10 inside player1_mc:
// CODE ON player1_mc TIMELINE _global.age = 31; // Create a global variable named age var age = 10; // Create a timeline variable also named age trace(age); // Displays: 10
But from any other timeline, the global variable age will still contain 31. In fact, if we remove the timeline variable age from player1_mc, references to age will then access the global variable:
delete age; trace(age); // Displays: 31
To assign or reassign the value of a global variable, we must use a fully qualified reference to the _global property, as in:
_global.age = 21; // This change in age is visible from every timeline that // does not specify its own value for age.
Local function variables similarly override globals:
_global.age = 21; // Set global variable function showAge () { var age = 5; // Set local variable trace(age); } showAge(); // Displays: 5 // Local variable overrides global variable trace(age); // Displays: 21 // Global variable still exists outside the function
For more information on how variables override each other, see Section 9.7.1. To remove a global variable, use the delete operator, as follows:
delete _global.age; // Deletes age. (The reference to _global is mandatory.)
When using global variables, be careful to specify uniquely identifiable names, particularly in movies that will be used by other developers or loaded into their movies. One option is to preface variables with your domain name, as in:
_global.moockorgGravity = 9.8;
Or, for easier reading, create an object that contains all globals, as in:
_global.moockorg = new Object(); _global.moockorg.gravity = 9.8;
Java developers may even prefer:
// Create namespace. if (!_global.org) { _global.org = new Object(); } if (!_global.org.moock) { _global.org.moock = new Object(); } // Assign variable in namespace. _global.org.moock.gravity = 9.8;
To make globals easy to identify, consider using a naming convention such as the suffix _gl, as in:
_global.moockorgGravity_gl = 9.8;
In Flash 6, a so called "getter/setter" property cannot be added directly to the _global object. See Object.addProperty( ) for details.
Chapter 2; "The Scope Chain," in Chapter 9