< Day Day Up > Creating and Referencing Global Elements There's no denying that even with a thorough understanding of target paths, it's sometimes hard to keep track of them—especially
Trang 1< Day Day Up >
Creating and Referencing Global Elements
There's no denying that even with a thorough understanding of target paths, it's
sometimes hard to keep track of them—especially when you have timelines within
timelines within timelines As you might imagine, target paths can become quite long To help simplify things, ActionScript provides access to a global object As a global element, this special Flash object exists apart from any specific timeline and is omnipotent in relation to your Flash project: you can reference it from any timeline without a target path and take advantage of its power to reference other timelines and the variables, functions, and other dynamic elements they contain
Let's take a look at how you create a global element and how you convert an element with a target path into a global element
Creating a global element, such as a variable, is as simple as this:
_global.myVariable = "hello";
Because this variable is now a global element, you can reference it from any timeline by its name:
_root.myMovieClip_mc.favoriteGreeting = myVariable;
or
_root.favoriteGreeting = myVariable;
You can also use the global identifier to create global functions (see Lesson 5, "Using
Trang 2Functions"):
_global.myFunction = function(){
//actions
}
To call the function from any timeline, use its name:
myFunction();
The same syntax is used to create instances of objects (see Lesson 4, "Using Object Classes"):
_global.myDateObject_date = new Date();
You can easily convert an element with a target path (such as a movie clip instance) so that it can be referenced globally—that is, without a path For example, suppose a movie clip instance in your project has a target path of _root.car_mc.engine_mc.piston_mc If you want to make it easier to reference this instance, you give it a global address:
_global.myPiston_mc = _root.car_mc.engine_mc.piston_mc;
To control that instance, you simply reference its global address from any timeline:
myPiston_mc.play();
Trang 3Is there a benefit to using a global element? It's a matter of preference as well as what a given project dictates In general, though, any element you use frequently or that's used
by a number of timelines is a good candidate for becoming a global element
One thing to keep in mind, though, is that naming collisions can occur when you use global elements—that is, an element in a timeline (for example, a variable) may end up with the same name as a global element For example, suppose you have a global variable named myVariable Suppose you also have a movie clip instance named
myMovieClip_mc, which contains a variable named myVariable You have a global variable and a variable in a movie clip instance, both with the same name
You would have a problem if myMovieClip_mc attempted to reference the global
variable named myVariable using this:
myVariable
This is because this timeline has its own local variable named myVariable If you use this syntax, Flash won't be able to tell whether the script is referencing the local variable (within the movie clip instance) or the global one—a conflict it resolves by automatically referencing the closest variable to the timeline itself, which is the local one
An easy way to avoid these naming collisions is to preface global element names with a small g:
gMyVariable
gFavoriteColor
TIP
Another thing to remember is that a global element uses memory that can be freed only
by deleting the global element Therefore, using lots of global elements may not be an efficient use of memory
< Day Day Up >