Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Syntax and Language Fundamentals > About dot syntax and target paths > About scope and targeting | |||
When you nest instances, the movie clip that nests a second movie clip is known as the parent to the nested instance. The nested instance is known as the child instance. The main Stage and main timeline are essentially a movie clip themselves, and can therefore be targeted as such. For more information on scope, see About variables and scope.
You can target parent instances and parent timelines using ActionScript. When you want to target the current timeline, you use the this keyword. For example, when you target a movie clip called myClip that's on the current main timeline, you would use
this.myClip.
Optionally, you can drop the this keyword, and just use
myClip
You might choose to add the this keyword for readability and consistency. For more information on recommended coding practices, see Best Practices and Coding Conventions for ActionScript 2.0.
If you trace the movie clip, for either snippet above you see _level0.myClip in the Output panel. However, if you have ActionScript that's inside the myClip movie clip but you want to target the main timeline, target the parent of the movie clip (which is the main Stage). Double-click a movie clip, and place the following ActionScript on the movie clip's timeline:
trace("me: " + this);
trace("my parent: " + this._parent);
Test the SWF file, and you'll see the following message in the Output panel:
me: _level0.myClip my parent: _level0
This indicates you targeted the main timeline. You can use parent to create a relative path to an object. For example, if the movie clip dogClip is nested inside the animating movie clip animalClip, the following statement on the instance dogClip tells animalClip to stop animating:
this._parent.stop();
If you're familiar with Flash and ActionScript, you've probably noticed people using the _root scope. The _root scope generally refers to the main timeline of the current Flash document. You should avoid using the _root scope unless it's absolutely necessary. You can use relative target paths instead of _root.
If you use _root in your code, you can encounter errors if you load the SWF file into another Flash document. When the SWF file loads into a different SWF file, _root in the loaded file might point to the root scope of the SWF file it loads into, instead of referring to its own root as you intend it to. This can lead to unpredictable results, or break functionality altogether.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000682.html