
Where are my display objects?
Before, the Stage class in AS2 is treated as a class whose properties are all static, but with AS3 the new and better Stage class is now a display object. Both making the stage class the main drawing area and where flash content is shown. Basically, just think of it as a practical stage, all actors and props are all objects on display and those who are backstage are objects which are simply not included in the stage yet.
The Stage is a singleton class whereas there can only be one instance of it (imagine watching a play where there are 2 or more stages, its just impossible). Even if you add multiple instances of display objects to the display list, the stage property of each display object still refers to the same Stage object (that goes for loaded SWF files too).
Did you know that the stage is not globally accessible – you can’t call it or reference it from anywhere (unless manually referenced in a variable of course). The simplest way to do so is to use the
stageproperty from any of your display objects which were added to the display list.
Yes, you heard it right from “any” display object, but take note, only if it is already added to the display list. It doesn’t matter if your display object is a child of another display object or how far the display heirarchy it is, as long as it is included in the display list.
Don’t confuse yourself with “Stage” (with capital “S”) and “stage” (with small caps). “Stage” refers to the class flash.display.Stage and “stage” is it’s instance.
Just remember, from a common coding convention: classes begin with capital letters and variables don’t.
If the display object is removed or not yet added from the display list the stage property is just set to null.
Example:
Create a Square.as. The doStuff function just tries to see if the stage is available or not from its own scope.
package { import flash.display.Sprite; import flash.display.Graphics; public class Square extends Sprite { public function Square() { // just some drawing graphics.lineStyle( 2, 0x666666 ); graphics.drawCircle( 0, 0, 48 ); x = 100; y = 100; } public function doStuff() { if( stage != null ) // if stage is available { trace( 'I see the stage! '+ stage ); } else // stage is not available { trace( 'I am not on stage... :( ' ); } } } }
Now create MyStage.as and a new Flash File (actioscript 3.0) then set MyStage as your Document class (just on your properties panel).
package { import flash.display.MovieClip; public class MyStage extends MovieClip { public function MyStage(){ var square:Square = new Square(); // instanciate a Square square.doStuff(); addChild( square ); // add the square instance into display list square.doStuff(); } } }
When you test your movie the output panel should display something like:
I am not on stage... :( I see the stage! [object Stage]
Neat Trick: code snippet
So based on this you can have a neat little trick to determine if your display objects are on stage and added to the display list.
public function isDisplayed():Boolean { return Boolean( stage != null ); }
I hope you understand a little bit more with using display objects and the stage from what I have shared.
Other Keywords
- Working with Actionscript 3 Stage.
- Knowing Flash Actionscript 3 Stage.
- Stage & stage difference in actionscript 3.
- Proper use of Flash Actionscript 3 Stage.






( 3.67 / 5 from 3 votes )
•