Question

In ActionScript 3, I've read that the root variable references an instance of the Document Class.

Within my document class constructor, a trace(this == root) returns true. Later in the constructor, a constructor of another class is called. This constructor, however, states that root is null. Finally, tracing from an event listener gives me the result that root is [object Stage].

My goal is to have a single instance of a Document Class (in MainGame.as) and to be able to refer to that as (root as MainGame) throughout my ActionScript program. How can I do this?

If it matters, all of my code is in the default package.

Thanks!

Was it helpful?

Solution

The root propety of a DisplayObject becomes a reference to the Document Class once the DisplayObject is added to the display list. You can continue to use root but be aware that only objects on the display list will work.

You can read more about root here:

The root property of the Stage object is the Stage object itself. The root property is set to null for any display object that has not been added to the display list, unless it has been added to a display object container that is off the display list but that is a child of the top-most display object in a loaded SWF file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top