_root.mainZ // Access the variable mainZ on the main timeline
_root.firstName // Access the variable firstName on the main timeline
We can even combine a reference to _root with
references to movie clip instances, drilling down the nested
structure of a movie in the process. For example, we can address the
variable x inside the clip
square that resides on the main movie timeline,
as:
_root.square.x
That reference works from anywhere in our movie, no matter what the
depth of clip nesting, because the reference starts at our main movie
timeline, _root. Here's another nested
example showing how to access the variable area in
the instance triangle that resides on the timeline
of the instance shapes:
_root.shapes.triangle.area
Any reference to a variable that starts with the
_root keyword is called an absolute
reference because it describes the location of our
variable in relation to a fixed, immutable point in our movie: the
main timeline.
There are times, however, when we want to refer to variables on other
timelines without referring to the main timeline of a movie. To do
so, we use the _ parent property, which refers to
the timeline upon which the current movie clip instance resides. For
example, from code attached to a frame of the clip
square, we can refer to variables on the timeline
that contains square using
this syntax:
_ parent.myVariable
References that start with the keyword _ parent
are called relative references because they are
resolved relative to the location of the clip in
which they occur.
Figure 2-1. A sample movie clip hierarchy
To display the value of the variable color (which
is in the shapes clip) from code attached to the
timeline of triangle, we could use an absolute
reference starting at the main timeline, like this:
trace(_root.shapes.color);
But that ties our code to the main movie timeline. To make our code
more flexible, we could instead use the _ parent
property to create a relative reference, like this:
trace(_ parent.color);
Our first approach (using _root) works from a
top-down perspective; it starts at the main timeline and descends
through the movie clip hierarchy until it reaches the
color variable. The second approach (using
_ parent) works from a bottom-up perspective; it
starts with the clip that contains the trace( )
statement (the triangle clip), then ascends one
level up the clip structure where it finds the
color variable.
We can use _ parent twice in a row to ascend the
hierarchy of clips and access our size variable on
the main timeline. Here we attach some code to
triangle that refers to size on
the main movie timeline:
trace(_ parent._ parent.size);
Using the _ parent property twice in succession
takes us up two levels, which in this context brings us to the main
timeline of the movie.