About dot syntax and target paths
123
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” on page 96
.
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
Chapter 19, “Best Practices and Coding
Conventions for ActionScript 2.0,” on page 731
.
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 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...