_root, Evil _root

As soon as you start writing your first lines of code in ActionScript, it's pretty likely that you'll use _root.

_root is an identifier that references the main timeline of your movie.

When you use _root, you're using absolute notation.

If you have a movie clip on the stage called my_mc, you can attach code like this to the main timeline:

Actionscript:
  1. trace(_root.my_mc); // _level0.my_mc

But you can also use relative notation to achieve the exact same result:

Actionscript:
  1. trace(my_mc); // _level0.my_mc

This is the preferred way. Using relative paths is considered a best practice.

From these two lines of code, one thing is obvious: you save some typing by avoiding _root! But that's not it.

So what's the problem with _root?

A very common problem is when you load an external file (using MovieClip.loadMovie() or MovieClipLoader.loadClip()) which uses _root:
the target paths are no longer valid and the code is now broken - nothing works anymore.

I see it all the time: "my .swf works on its own but when I use loadMovie() it stops working",

This could be a real problem is versions of Flash prior to MX 2004.
An immediate solution could be using loadMovieNum() instead, but that wasn't always possible.

loadMovieNum() could be a solution because each level has its own _root so, paths are preserved when loading the .swf.

In MX 2004, _lockroot was introduced in order to help solving the problem.

But just because _lockroot exists, that doesn't mean you should use _root. _lockroot can be regarded as a workaround for a bad practice.

Another problem is portability and it's closely related with the last one. The difference is that there isn't a _lockroot solution. You'll have to use 'find and replace' or rewrite your code.
If you attach your code to a movie clip's timeline and then want to move that movie clip to another timeline or use attachMovie() on another timeline, once again, the target paths will no longer be valid and the code will be broken.

As in Flash MX or higher all the code should be attached to a (the main) timeline, not to Movie Clips or Buttons, using _root is useless and is always dangerous.

_root is considered a bad practice and you should stay way from it. Chances are that someone who looks at your code will take you for a newbie that doesn't really know what you're doing.

_root does look ugly.

Conclusion:

  • _root is not necessary, there is no reason to use it
  • _root is considered a bad practice
  • _root causes problems under certain circumstances
  • Use relative paths only

Links:

1 comment to _root, Evil _root

  • Jack

    I am running into the Evil Absolute problem. I have 4 frames. Each frame has 3 sliders. Each slider comes from a single movie clip with the AS2 below. My problem is that the frames have a “Previous” button. When you click on it, it resets the values. I try to remove the first line of my code below and use _root.perc. It works when I click previous – except it sets all the values to all the sliders to the initial value.

    SLIDER_MC:

    //this.perc = 0;
    rail.onPress = function() {
    handle._x = _xmouse;
    handle.onEnterFrame = function() {
    _root.perc = Math.abs(handle._x*4/rail._width);
    };
    };
    handle.onPress = function() {
    this.startDrag(true, 0, 0, rail._width, 0);
    this.onEnterFrame = function() {
    _root.perc = Math.abs(this._x*4/rail._width);
    };
    };
    handle.onRelease = stopDrag;
    handle.onReleaseOutside = stopDrag;

    The code on each frame is similar to this. You can see where I have been playing with the _root.perc.

    FRAME 3 ActionScript:

    // Get the values of the sliders
    this.onEnterFrame = function():Number {
    total3.text = 0;
    sliderValue9.text = 1+slider9_mc._root.perc;
    sliderValue8.text = 1+slider8_mc._root.perc;
    sliderValue7.text = 1+slider7_mc._root.perc;
    };
    stop();
    // Next Page Button
    nextPage3_btn.onPress = function():Void {
    };
    nextPage3_btn.onRelease = function() {
    gotoAndPlay(“Screen4″);
    };
    // Previous Page Button
    prevPage3_btn.onPress = function():Void {
    };
    prevPage3_btn.onRelease = function() {
    gotoAndPlay(“Screen2″);
    };

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>