Is _root Evil?

ActionScript 2.0

People tend to feel strongly about their personal preferences.  Some folks love the global _root property, while others absolutely hate it.  Not everyone is vocal, of course, so any passion expressed in online discussion of the empirical merits of _root may be skewed.  Hard to say.

In this article, I hope to give you enough information to make your own informed decision.  Then you can get as passionate as you like. ;)  

What is _root?

_root is a global property, which means it can be accessed from anywhere in your code.  From the point of view of any timeline, from any on() or onClipEvent() event handler attached to any object, or scoped to any function, _root always refers to the same thing.  And what is that?  Succinctly, _root refers to what is generally called “the main timeline.”

Where the confusion starts

Now, since I said “always” and “the same thing,” allow me to hastily add that caveats do apply:  the definition of “main timeline” is somewhat malleable, and I suspect it is this potential for confusion that frustrates people who prefer not to use _root.

Your SWF doing the loading

To be more specific, _root actually refers to the main timeline of the current document level.  Chances may very well be that your current document level is _level0, the default.  Flash does, however, support multiple levels.  This is something akin to opening more than one document at the same time in your favorite word processor.  If you use the global loadMovieNum() function to load external SWFs at runtime, then _root will point to whatever level is currently active.

If you don’t load external movie clips, or if you load them into movie clip containers via loadMovie(), MovieClip.loadMovie(), or the MovieClipLoader class, then _root will always point to _level0.

As it turns out, we’re actually still not in the clear, but I want to pause for a moment to explain when a person might use _root.  Objects in ActionScript are hierarchical.  You might, for example, have three movie clips nested inside each other where the innermost movie clip contains a button.  This button may be expected to execute a function defined in a frame of the main timeline.

From an objective point of view, the button’s position is here …


From the button’s point of view, the function it intends to trigger is positioned here …


… which can be difficult to scan visually.  Now, using _root in the following context provides a shortcut to the main timeline …


Less to type, and that’s essentially the reason people who use _root do.

Now, obviously, if the desired function exists in _level0, but the nested movie clips exist in _level1 (and if _level1 is the currently active level), then _root would refer to _level1’s main timeline and the function would not be found.  You would have to explicitly refer to _level0.function() from the button’s point of view.  Depending on setup of your main and loaded SWFs, this may not be feasible.

Your SWF being loaded by another

Even if you don’t load external SWFs into levels, you may see one additional case in which _root seems to change meaning again.  Your SWF might actually get loaded by another SWF.  In such a case, the above information still applies, but from the opposite angle.  The true _root is still the main timeline of the current document level, but only the outermost SWF’s main timeline counts.  This makes perfect sense, of course:  any SWF loaded into another loses the privilege of calling its own main timeline the main timeline.  If a loaded SWF contains references to _root in its own code, those references will no longer resolve to the main timeline of that SWF, but to the main timeline of its topmost parent, however long the hierarchy stretches.


Seemingly, _root changes meaning every time you turn around.  But remember, it just ain’t so.  The global _root property refers to the same location every time.

_root always refers to:

  • the main timeline
  • of the current document level
  • of the outermost SWF

Except … except if the MovieClip._lockroot property of the loaded SWF is set to true.  Introduced in Flash MX 2004 (Flash Player 7), the MovieClip._lockroot property allows references to _root in a loaded SWF to continue resolving to its own timeline.  This feature was presumably introduced to allow developers to load older, _root-reference–heavy movies without breaking.


Admittedly, _root takes a bit of thinking to “get,” but once it makes sense, it makes sense.  If the buck stops with you — and it often doesn’t, in team settings — you might just decide that _root is a legitimate shortcut for referring to the common starting point of all your movie’s objects.  You might purposefully write a _root reference in the full knowledge that it cannot resolve to the desired timeline in isolation — that this SWF must load another, or be loaded by another, in order for _root to point where you intend.  I compare this to using root-relative versus document-relative hyperlinks in HTML.

On the other hand, you may find the above too much to keep track of, to the point that the “shortcut” of using _root costs more in terms of thinking than it saves in terms of typing.  One of the founding principles of object-oriented design, after all, is to reduce (ultimately, to circumvent) dependence on outside factors, such as the varying location to which _root may resolve.

Whatever you decide, make sure you know the reasons for your choice, then stick with what works best for you.

Leave a Reply