MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 123

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

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
keyword. For example, when you target a movie
this
clip called myClip that's on the current main timeline, you would use
this.myClip.
Optionally, you can drop the
keyword, and just use
this
myClip
You might choose to add the
keyword for readability and consistency. For more
this
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
in the Output
_level0.myClip
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
to create a relative path to
parent
an object. For example, if the movie clip
is nested inside the animating movie
dogClip
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
_
scope. The _
scope generally refers to the main timeline of the current Flash
root
root
document. You should avoid using the _
scope unless it's absolutely necessary. You can
root
use relative target paths instead of
.
_root
If you use
in your code, you can encounter errors if you load the SWF file into another
_root
Flash document. When the SWF file loads into a different SWF file,
in the loaded file
_root
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.
About dot syntax and target paths
123

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

This manual is also suitable for:

Flash 8

Table of Contents