When to use _root?

Noob question here: I know it’s sometimes necessary to include _root in your actionscript. Can anyone provide an explanation of when it’s appropriate?

Thanks!

It’s never completely neccesary. Theres always a way to get around it and avoid it. I recommend using a variable for the _root of the current clip your working on. You can put this in the root timeline of you movie:

var this_clips_root:MovieClip = this;

Then you can refer to “this_clips_root” instead of “_root”

In a nutshell, _root refers to your main timeline. If you have code buried in several nested movieclips but you want to target something on the main timeline you can simply use _root.NameOfObject.

You can sort of think of it like using windows explorer (if you have a pc). Imagine the C:/ drive is the _root timeline, a movieclip on the _root is a folder on the C:/ drive. A movieclip within that movieclip is like a subfolder within that folder, and a textbox (or what have you) within that nested movieclip is like a text file inside the subfolder, e.g.

C:/folder1/folder2/mytext.txt == _root.MovieClip1.MovieClip2.mytext.txt

SO, if you are writing code on the timeline of MovieClip2 and want to target MovieClip1 there are several ways to do it. You can use _parent to tell flash to go out one timeline (one level above) as in _parent.MovieClip1 or you can use _root to start at the main timeline and work in like _root.MovieClip1.

The advantage comes in when you have really deeply nested objects like
_root.MC1.MC2.MC3.MC4.MC5.MC6.MC7. If you are writing code at the timeline of MC6 and want to get to MC1 you could write

_parent._parent._parent._parent._parent.MC1 or you could write _root.MC1

More explanation than you ever wanted but hopefully it makes sense! :stuck_out_tongue:

Much appreciated!! :beer: Thanks guys!

np:mountie: