Where is as2 _root in as3?
If you remember in as2 we had _root which let us access to methods and variables in the root of the project and refer to absolute paths, I thought that as3 didn’t have _root propriety, but I was wrong. root property can be useful if you want to share some value to all the classes in the project, add events to main class that call methods declared inside the class where we are working on or call generic methods declare in root class.
To use root propriety the class that you are working on most extend a DisplayObject (Sprite,MovieCLip,etc…) because you have to add the class to the parent class. And note, you can’t use the root propriety in the constructor method, because the addChild haven’t been process yet! Well let’s stop writing and see some code:
Main.as
In the Main class I’m declaring a variable named myValue which I did the proper sets and get. I am declaring two classes, in one of them I will set the value at the Main class and in the other I will get that value back. I’m also creating a movieClip just for test purposes.
ExampleClass.as
In ExampleClass I’m setting myValue that is propriety of the Main class with some text and I’m tracing the movieClip that I create in the Main Class.
AnotherClass.as
In AnotherClass I’m tracing the myValue propriety. So the result should be:
[object MovieClip]
This value is inside the Main Class
So it works ok, you only have to be carefull not refer root inside the constructor method, have the certain that you already addChild the class when you are referring to root and that the class extends a display object. If you do that, you can too have access to stage propriety, let’s check the next example:
ExampleClass.as
The result should be or stage width.
Another way to access to main proprieties, methods, etc… is with static members. In the next example I’m declaring a static property in the main class and setting and trace the value of it in the example class.
Main.as
ExampleClass.as
The result should be “Hello Word!”, if you didn’t know this, read some books about object oriented programming and static members.
Another way to do the same thing is to pass the instance of main class as an argument to the other classes.
Main.as
Example.as
There are still another ways to access to the Main class code. I don’t know which techniques are more correct, I usually use them all depending in what I need. I hope I help someone.
October 28th, 2008 at 2:55 am
you can make a static variable named instance in Main
public static var instance : Main;
and in Main constructor you can assign
main = this;
then from any other class you can
import Main; // or other structure that point to main
and then easily you can use in code
Main.instance to get to the main class
introducing one more variable to class
private var _main : Main = Main.instance;
is even better and you can just refer to it through _main
there are other ways but this is what I use, somewhat like Singleton but being this is main class there is no need for getInstance and checking in constructor if it’s already created
October 28th, 2008 at 2:55 am
I meant instead of main = this;
instance = this;
in 4th line of prev comment
October 28th, 2008 at 9:04 am
If the classes who need to share variables are project specific, you might prefer to go with a Singleton:
public class SharedProjectData {
static private var _value : String;
static public function set myValue( value : String ) : void { _value = myValue; }
static public function get myValue() : String { return _value; }
}
In project classes you can access like SharedProjectData.myValue
well… usually there’d be just one static method called getInstance(), but for the sake of simplicity
Also many devs would go with MVC or something similar to keep things clean.
Anyway, I don’t recommand using root or stage to store any values. As you said in your post: you couldn’t access data a) from withing constructor and b) when instance is not in display chain.
October 28th, 2008 at 11:09 am
Off course Sev, you are right.I did soft examples not getting in design patterns
by the way, this is a awesome book http://www.as3dp.com/
October 29th, 2008 at 2:59 am
I would avoid trying to emulate tactics used in AS2 and make an extra effort to port your solution to AS3 without using root. Root can be the display object of your choosing. I suggest you emphasize separation of view logic from business logic. Keep your views dumb and you’ll be happier!