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 but 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 first take a look at how you can create a global element, then we'll show you how you can convert an element with a target path into a global element.

Creating a global element, such as a variable, is as simple as the following:

 _global.myVariable = "hello"; 

Because this variable is now a global element, you can reference it from any timeline simply by using its name:

 _root.myMovieClip.favoriteGreeting = myVariable; 

or

 _root.favoriteGreeting = myVariable; 

You can also use the global identifier to create global functions (see Lesson 6, Using Functions ):

 _global.myFunction = function(){    //actions…  } 

This function can now be called from any timeline simply by using the following:

 myFunction(); 

The same is true when creating instances of objects (see Lesson 4, Understanding and Using Objects):

 _global.myDateObject = new Date(); 

As mentioned above, 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 there was a movie clip instance in your project with a target path of _root.car.engine.piston . If you wanted to make it easier to reference this instance, you could give it a global address:

 _global.myPiston = _root.car.engine.piston; 

To control that instance, you would simply reference its global address from any timeline:

 myPiston.play(); 

Is there a benefit to using a global element? Usually, it comes down to a matter of preference as well as what a given project dictates. In general, though, any element that you use frequently or that's used by 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 had a movie clip instance with a variable named myVariable: If you had a global variable with the same name, you wouldn't have a problem using the variable's absolute target path to reference it on the timeline:

 _root.myMovieClip.myVariable 

However, you would have a problem if myMovieClip attempted to reference the global variable named myVariable using the following:

 myVariable 

This is because this timeline has its own local variable named myVariable . If you use the above syntax (without a target path), Flash won't be able to tell whether you're referencing the local variable 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 

Another thing to remember is that a global element uses memory that can only be freed up by deleting the global element. Therefore, using lots of global elements may not be an efficient use of memory.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

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