"root" changes in different places

In ActionScript 3, I read that the root variable refers to an instance of a document class.

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

My goal is to have one instance of a document class (in MainGame.as ) and be able to refer to it as (root as MainGame) in my ActionScript program. How can i do this?

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

Thanks!

+4
source share
1 answer

The prefix root a DisplayObject becomes a reference to the document class when a DisplayObject added to the display list. You can continue to use root , but keep in mind that only objects in the display list will work.

You can learn more about this here:

The root property of a 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 the container of the displayed object that is outside the display list but is a child of the top-most display object in the loaded SWF file.

+5
source

All Articles