May 07, 2007 Moving away from the _root

When working with multiple, nested MovieClips, or dynamic variables, sometimes you need your ActionScript to refer out to other MovieClips, or the main timeline of the swf file. When I was first teaching myself ActionScript, I learned to use _root to reference outside of functions or MovieClips.

It is this point in my learning that see how wrong I was to rely on _root for every outside-the-MovieClip function.

First lets look at how _root is wrongly commonly used:

  1. button1_btn.onPress = function(){
  2. _root.variableValue = 55
  3. _root["myDynamicVariable"+1]._visible = false
  4. }

Let's assume that this button code is on the main timeline. First of all, if 'variableValue' is on the maine timeline, and so is this button function, then you do not need to reference _root. When you call a function or variable inside of a function, Flash assumes already that you are referring to the timeline the parent function is in.

However, the dynamic variable on the second line poses a different problem. Most of the time, if you were going to do a dynamic variable without using _root, you would simply use 'this'. Like so:

  1. this["myDynamicVariable"+1]._visible = false

Using 'this' should be treated with as much caution as _root. Whenever you say 'this' you mean the entity you are currently in. So, if you are writing a regular function, 'this' refers to the main timeline, because the function is in the main timeline. This principle is implicit.

To illustrate, lets use the previous button example coded on the timeline and replace _root with 'this':

  1. button1_btn.onPress = function(){
  2. this.variableValue = 55
  3. this["myDynamicVariable"+1]._visible = false
  4. }

First things first, this doesn't work (and this is what turns people to using _root). What confuses many people is why this works on the main timeline, inside regular functions, but not in this instance. When you write a button function like this, you are making a references to an event, onPress , given by the object button1_btn. So 'this' refers to button1_btn and not the main timeline. To clarify, using 'this' inside of a timeline written button function refers to the button, calling a variable or function works as it would inside a normal function.

In many places where you might use _root, you can also use _parent, which is one MovieClip out of where you are (_parent is stackable to go out as many times as you need). For the button code, by itself, it still wont work.

  1. button1_btn.onPress = function(){
  2. _parent["myDynamicVariable"+1]._visible = false
  3. }

This function would refer to the parent of the timeline the function is in. Because as we just learned, if 'this' isn't referred, the button function is treated as a normal timeline function. So to get the function to work without using _root, we need to combine the two.

  1. button1_btn.onPress = function(){
  2. this._parent["myDynamicVariable"+1]._visible = false
  3. }

This works because 'this' refers to button1_btn, therefore _parent refers to button1_btn's parent, the timeline this function is in.

Now for the advanced solution.

Using this._parent could get a little redundant after a while, and would cease to work if you moved the function to a different MovieClip. The best solution i have found is to make a variable that is data-typed as a MovieClip and refer it to the timeline you are wanting to reference.

  1. var home:MovieClip = this

What this says is that anytime you call home you will be referring to the timeline. (You can also refer to other timelines by replacing 'this' with _parent, MovieClip or MovieClip.ChildClip).

Let's see how it would work:

  1.  
  2. var home:MovieClip = this
  3. button1_btn.onPress = function(){
  4. home["myDynamicVariable"+1]._visible = false
  5. }

So now you have no excuse what-so-ever to use _root unless you really REALLY mean it!

AddThis Social Bookmark Button Posted by Greg Ferrell at 02:08 AM. Filed under: Flash-ActionScript • (0) CommentsPermalink