_root, _parent, and this
by ilyas usal
- _root
_root is an alias, just as _parent and this. It is used to access the root of
the current level. For instance, if you write this in the first frame of your
movie:
Quite normal, since the default level is 0. But if you load a movie in
_level1, and if that movie is referring to _root, it will mean _level1. So be
careful about that: to access the _root of the master movie from the loaded
movie, you have to write:
_level0._root.myVariable = ...
- _parent
OK, now _parent is quite similar, except it refers to the clip that is one
"layer" higher in the tree. In this example, _root creates (hence contains)
firstClip, so it is his parent, firstClip creates secondClip,
which creates thirdClip:
- this
this refers to the current object. It is particularly useful in Flash MX when
you build a prototype, as we will see later.
It is also vital now that Flash handles variables scope differently. In Flash
5, declaring a variable in an onClipEvent(enterFrame) made the variable
relative to the clip. It's over now, since all the variables left alone are
relative to the current timeline. That's why if several objects use the same
method, prototype or function at the same time, they can all modify the same
variables, so the animation cannot run normaly.
That's why you have to tell Flash when your variables depend on the current
object by declaring them as part of this:
onClipEvent (enterFrame)
{
this.myVar = 5;
}
If you hadn't put this, and that there was a variable called
myVar on the _root, this variable would have
been replaced by that one. You see the problem?
Hope this tutorial helped. If you have any
questions, please post them on the forums at
http://www.kirupa.com/forum/
|