AS3 for Noobs (Part 3): The _root problem

When I tried to switch to AS3 and suddenly _root and _global were just gone. And it wasn’t that they just got renamed something else, they were gone and nothing that I could find had replaced them. Needless to say I was extremely confused. Why were they gone? How am I supposed to write applications without them? Obviously other people are getting along without it so what am I missing here? This is all because, like many of you using flash, I’m are a designer by nature and a developer by necessity. So what I had to do to get into AS3 is essentially rethink how I looked at flash. When I write some html and css for a website I realize when I look at the page in a browser that this column over here is a chunk of code and it looks this way because I applied these styles to it. I realize that because I wrote the code by hand and I know that I’m telling the browser to render these elements a certain way. So even though I knew the same thing was happening in flash I didn’t think of it that way. I didn’t think that I’m doing the same thing in flash whether I’m coding a movieclip by hand or I’m creating using the visual interface they are both chunks of code. In this case they are an instance of the MovieClip class. Just about every object you create in flash is and instance of some class somewhere. Now we’ve already learned about classes in the two previous articles so let’s see how those lessons can help us get around the whole problem of not having _root anymore.There are two techniques that you can use to help all your different objects and functions communicate with each other. None of these is an exact replacement of _root or _global but, I’ve found that using these instead is actually more effecient then using the old _root and _global properties.

Technique 1 - Passing Refrences

You’ve probably done this before with other functions you’ve created in the past. Basically if you know that you have two objects that need to communicate with each other you can pass object1 as a reference to object2 so that object2 can now access all the variables and functions from the object1. This might look something like the example below.You’ve probably done this before with other functions you’ve created in the past. Basically if you know that you have two objects that need to communicate with each other you can pass object1 as a reference to object2 so that object2 can now access all the variables and functions from the object1. This might look something like the example below.

First we have a class we’ve created for object1 that looks like this:

package{
import flash.display.Sprite
public class Object1 extends Sprite
{
public var foo:String;

public function Object1()
{

foo = “bar”;

}
}
}

Then we have another class for object2 that looks like this:

package{
import flash.display.Sprite

public class Object2 extends Sprite
{

var myObject:Sprite;
public function Object2(obj:Sprite)
{
// here is the reference to another object

myObject = obj;
}

public function test(){

trace(myObject[’foo’]);
}
}
}

You’ll notice that object too expects a reference to another object in it’s constructor method. It get’s the reference and assigns it to a variable called my object. Now that variable can be accessed from anywhere in the Object2 class instance. Now you should be able to do this.

var object1:Object1 = new Object1();
var object2:Object2 = new Object2(object1);
object2.test();
// should trace out “bar”

This technique of passing refrences through class methods is really only effective when you need to do a limited amout of communication between a few objects.

Technique 2 - Static Properties

Static properties are probalby the closest thing to _root and _global you will find in AS3. In the second article we looked at how different properties in a class can have different attributes. The two attributes we looked at were “public” and “private”. Now I’m going to introduce a new attribute that can be a very powerful tool to help you communicate between your different objects. If you just set a property to public or private then that property can only be accessed through an instance of that class. So if I created a new instance of Object1 I could access all those properties through that instance like this.

var obj1 = new Object1();trace(obj1.foo)
// returns bar;

The static attribute doesn’t create properties that can be accessed through the class instances but instead they are accessed through the class itself. This means that instead of every instance potentially having a different value, you have one value that is the same for all the instances. So in the example above if I had made the variable “foo” a static variable then after it was set any instance of Object1 would return the same value for “foo”. Also you can dynamically set the value of static variables from anywhere in your flash application by calling them directly through the class and not the instance. For example if I did this:

Object1.foo = “hello”;

Now anywhere in my application that I call the variable Object1.foo it have the value of “hello”. Think of static properties as just more organized versions of _root. Now instead of having 100 variables on the root because they all need to be globally accessible, you have them attached to different classes so that the global properties that deal with Object1 are tied to the class Object1. You can also make static functions that work the same way. Just keep in mind that a class treats static variables and functions differently from non-static properties. You can have instance only properties that can directly access static properties but not the other way around.But let’s say I wanted to create a static function that could be accessed from anywhere but I wanted it to perform an action for every instance of that class. Here’s an example of how it could work:

package{

import flash.display.Sprite

public class Object1 extends Sprite
{
public static instanceArray:Array = new Array();

public function Object1()

{

instanceArray.push(this);

}
public static function instanceLoop()

{

// loop through instanceArray to access all the instances of this class
}
}
}

Now you can see how we are combining the first two techniques. We are using a static array but everytime a new instance of this class get’s created it adds a reference to that instance to the static array. Now we can have static functions that access instance properties.

Don’t worry if after reading this you don’t really see how this replaces _root. For me I really had to start putting them into practice before I understood how it all worked. I would recommend just doing some simple experiments with it to try and get the hang of everything covered so you can begin to see how you would use this in your projects. It takes a little more planning on your part to map out how you want everything to work together but in the end it makes it all more effecient and organized then just slapping everything on the _root.

12 Responses to “AS3 for Noobs (Part 3): The _root problem”

  1. Leonardo Says:

    ah yeah. Keep up the good work.

  2. confused Says:

    uh ok…

    what if i want to tell the root to move a frame up from a movieclip.

    in AS2:

    _root.gotoAndStop(2);

    in AS3:

    u must create a .as file to accomplish this. Sounds redundant to me.

  3. Steve Testone Says:

    It does seem a bit redundant and to be honest some of the stuff is but it’s all a trade off. Unfortunantly I think they just had to decide what was the ultimately “best” way to do things.

    The good news is you don’t need to create an .as file for your document if you don’t want to. You just need to pass a reference of what is essentially your “root” movie which is the fla in this case to whatever mc you are trying to call it from.

    For instance if you have a movieclip called “controller” that was supposed to control the main mc you could either just say “parent.gotoAndStop(2)” or if controller is more nested you can pass a reference like

    controller.rootMc = this

    from the main timeline and now you have a property in controller that references your “root”.

  4. Andrew Says:

    Isn’t it just that “_root” has changed to “root”. Dropping the underscore “_”? That is what I did and things worked fine, exept my calling of functions on the root, but I just started with AS3…

  5. Andrew Says:

    This is what else I found: You can do this, which automatically casts the root as a Movie Clip, then I can call my script which is at the root.
    MovieClip(this.root).setButtonEnabled();

  6. Steve Testone Says:

    Using root you can still do similar things but it’s not a “global” root anymore. When you get into projects with multiple swfs root like _root doesn’t really work. Unless you want to pass around a bunch of references in a large project (which seems like a bad idea) you are better off using static properties.

  7. noponies Says:

    Nice article, with some nice techniques.

  8. burak Says:

    hi,

    I have to getBouns acording to _root in AS3. and my AS3 app, build with document class that name is Main.as

    how can I use getBounds of an mc class according to equality of old _root object in AS3

  9. Steve Testone Says:

    Where does the movie clip you are wanting to reference live? Is it in your main swf or somewhere else? Many times what I’ll do if there is a clip I need to reference I’ll create a static property called “instance” in the class file so I can reference it anywhere like this MyClass.instance . Of course this only works if there is only one instance of that class created at a time.

  10. WillAlvein Says:

    Well, hello there, Just needed to tell you thank you, this has been of so much help, I’m currently starting in AS3, and you’ve ended some severe headaches… thanks.

  11. Allen N Says:

    hello,

    I need a little help here.. I’m still confused..

    here’s my situation:

    I have a movieclip and in that movieclip I want to just simply say something like _root.flvPlayer.stop();

    i tried:

    root.flvPlayer.stop();
    _parent.flvPlayer.stop();
    _parent.root.flvPlayer.stop();
    stage.flvPlayer.stop();

    Please help!?

    Thanks

  12. Ninja Says:

    @ Allen N:

    Use this line of script as Andrew suggested before….

    MovieClip(this.root).flvPlayer.stop();

    See if that helps…

    Ninja.

Leave a Reply