Using _root, _parent, this, and Levels


Using _root, _parent, this, and Levels

You might want to target a variable that exists in a different scope or Timeline in your SWF file. If you are writing code that is nested within a movie clip (or a component) and you want to access something on the main Timeline, such as a button, you need to use _parent to access the other Timeline. You use these scopes to tell the SWF file where to go to access those variables. When using the this keyword, you're referring to the current object in the current scope. For example, when you're within a movie clip and refer to this, you're telling the movie clip to look at itself. The _parent keyword references the parent item of the current object. For example, if you have a movie clip instance on the Stage and within the movie clip object are references to _parent, you are referring to the Timeline that the movie clip is on. You can combine the use of this and _parent to reference objects all throughout your SWF files. If you are in a nested movie clip, you can always use code similar to the following to navigate through the hierarchy of the SWF file to control other movie clip instances or component values:

this._parent._parent.otherMovieClip_mc.stop();

Another tricky concept to grasp can be the use of the this keyword. Depending on the exact context, it can refer to different things. If you use this within a movie clip instance, this refers to the Timeline of the movie clip. If you are using this in a button function, the this keyword refers to the Timeline containing the button instance rather than inside the button itself. If used with an onClipEvent() handler attached directly to a movie clip, the this keyword refers to the Timeline of the movie clip.

Consider the following code. If you place the following similar code on the main Timeline, it does very different things. Perhaps you have an SWF file playing, and there is a movie clip called myClip on the Stage that's also playing some content. You can treat movie clips like buttons using ActionScript (which you will do to the movie clip buttons that you created earlier) by giving them the onRelease event handler.

myClip_mc.onRelease = function() {  this.stop(); };

This ActionScript stops the myClip_mc instance itself if it were animating. The ActionScript inside the function targets the movie clip's Timeline using the this keyword. However, if you use the following ActionScript instead, the ActionScript stops the main Timeline instead of the movie clips Timeline:

myClip_mc.onRelease = function(){  stop(); };

The movie clip is being treated like a button, and buttons are made to target the Timeline they are sitting on, not the Timeline of the button itself (unless, of course, you use the this keyword).

You will encounter the frequent usage of _root when you use Flash and study other people's ActionScript. When users use _root, it means they are targeting the main Timeline. It's kind of like your root directory on a hard drive (for example, C:) or the root folder for a website. This is generally referred to as absolute referencing. Just as absolute referencing is not always the best idea to use in a website (it's hard to transfer your site to another domain if you use absolute referencing), it's also not a great idea to use in Flash in many circumstances because it's hard to move your ActionScript somewhere else. It can also cause problems when you load SWF files into other SWF files.

What causes problems when you are using _root is straightforward. When you refer to _root in an FLA file, you are referring to the SWF file's main Timeline when the document has been published. However, when you load that SWF file into a different SWF file, root means the SWF file that you're loading into. You can control for this by using a special property called lockroot, which tells a movie clip or a SWF file that references to root shouldn't go beyond that SWF file or movie clip. You're telling movie clips and SWF files then to behave as though they were the main document Timeline. It's a "you're the man" kind of property, and it's very, very useful. It has true or false values, and is also outside the scope of this book, but you can learn more about it in other books from the Training from the Source series.

In the application you are building, nested within the movie clips on the Stage are code or elements that the code targets. If those movie clips were empty, you would be able to load a SWF file into any of them, and that SWF file could target objects on the main Timeline. If you have code within the SWF file loaded into the movie clip, you can use _parent to target the main Timeline or objects out on the main Timeline. Another way of loading SWF files into your application however is to use levels.

Levels belong to the Flash Player, sort of like floors with invisible planks. The first SWF file to load into a level provides the foundation for the rest of the house, more or less, and loads into level 0. It determines the width and height of the Flash Player object in the HTML page, as well as the background color and the frame rate. From there, you can load other SWF files into other levels; those SWF files will inherit the background color and frame rate determined by the first SWF file, and will match their upper-left corners with the upper-left corner of the first SWF file. The SWF files stack one on top of the other like floors in a building or like pancakes in a stack. Well, pancakes without the butter and syrup, anyway.

The first document to load in the Flash Player occupies _level0. From there, levels progress from _level1 to some very large theoretical number (last this author heard, it was in excess of 2 million). You don't have to load SWF files into levels in sequence, however. You can load the first SWF file into the Flash Player, load a navigation bar SWF file into level150, and all the content in-between (so the navigation bar is always above everything else). It's all very relative. You can also load the SWF file onto _level0 and replace the main Timeline's content instead, although this may not be such a great idea if the original clip contains variables and features that you need throughout the life of your application.

Tip

When you load a SWF file into a movie clip or a component (such as the Loader), it is not being loaded onto a new level, but instead into the movie clip or component instance itself. Then the content is contained within the movie clip and should be treated like any other movie clip on the Stage. This has the advantage of making it easier to handle data such as variables.


If you need to target something on a different level of a loaded SWF file, you would use the following ActionScript:

_level2.myMovieClip_mc.gotoAndPlay(3);

Tip

Before you call any code targeting a SWF file that is being loaded, you should make sure that the content has completely loaded beforehand, or else the code you are executing will not work properly.





Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English

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