Using _root and _parent

Do you know the difference between _root and _parent? If you are not sure, read this tutorial to find out!

Using these two properties is very important if you want to effectively work in Actionscript.

  • _root allows you specify or return a reference to the root Movieclip Timeline.
  • _parent allows you specify or return a reference to the Movieclip or object that contains the current Movieclip or object.

This sounds like some pretty difficult stuff, but in fact it's not. Just have a look at this example:


The question arises: If someone was inside object3, how would he specify a reference to object?

Note:
For the sake of simplicity, we assume that all of these Movieclips' instance names are the same—"object".

It is possible to achieve the effect by using either_root or _parent.  We are going to change the x property of the object using Actionscript. Let's see how this can be done:

// _root method
_root.object._x = 200;
// _parent method
_parent._parent.object._x = 200;

As I said, both of these lines will do the job. Nevertheless, the _root method is easier to remember and less likely to make a mistake with.

Let's again analyse what these two properties do:

_parent tells Flash to start reading from one level up. Because in the example object is two levels higher than object3, we must use _parent twice. Whereas _root specifies a reference to the main Timeline. So, you might say that _root is absolute, while _parent is relative.


OK, looks like this is it. Hope this information was helpful. Here you can find more tutorials.

Thanks for reading.
Cheers!

Average: 5 (1 vote)