AS3: Stage or stage?
Have you been confused with the following 2 AS3 keywords: Stage and stage. When to use which? I have been. After some research and testing, here is my findings:
“Stage” is for typing the variable as in:
var target:Stage
“stage” is a property of a display object to refer to the stage of the flash movie as in:
myMovieClip_mc.stage
To confuse the matter further, Stage in itself is also a class in AS3.
Below is an example to illustrate how to use them:
Create a simple flash movie called Test.fla. Creat a movieclip symbol “BlackBox” and make sure its linkage is enabled for export for Actionscript.
Create the following document class Test.as:
package
{
import flash.display.MovieClip;
public class Test extends MovieClip
{
public function Test()
{
var myTest:Control = new Control(this.stage);
}
}
}
Create the class file Controls.as:
package
{
import flash.display.MovieClip;
import flash.display.Stage;
public class Control extends MovieClip
{
public var box:MovieClip;
public function Control(target:Stage)
{
box = new BlackBox(); // BlackBox is a movieclip symbol in the Test.fla library
box.x = 10;
box.y = 10;
target.addChild(box);
}
}
}
Note the above technique which enables the Control.as class to draw the display(view) of the flash main movie.

Hmm, it’s really quite simple. Stage is a class which is derived from DisplayObject. There is only one instance of the Stage, wich is what _root would be in AS2.
Then every instance which is derived from DisplayObject has the property stage, which refers to the stage instance to which it is added (essentially the root of the Display list). One important thing to note is that the stage-property is populated only when the display object is actually added “to the stage” (a display list, whoose root is the stage). Therefore you generally should not reference the stage property in the constuctor, but use the addedToStage-event instead.
Stage – refers to the class flash.display.Stage.as.
stage – refers to an instance of the above class.
I have an Object on the stage (_root) called Registry. Why doesnt a reference to this Object such as the one below work from within a nested MovieClip on the display list then?
ex: this.stage.Registry (DOENST WORK…why?)
Roger, stage is a property, not a movieclip. Try the following instead:
MovieClip(this.parent).Registry
These are great examples but it doesn’t solve the fundamental issue Everyone is having… How do we call reference to Stage from a Class that has nothing to do with the MainInterface Class. MyClip.stage is great for moving things around but what if your trying to actually reference “Stage”. Which you need to do in order to get things like FULLSCREEN to work
Damon, maybe this can help you:
//DocumentClass
package {
import flash.display.MovieClip;
public class DC extends MovieClip {
public static var xRoot:*;
public function DC() {
xRoot = this;
}
}
}
//From other class
DC.xRoot.addChild(someLibraryMovieClip);
Well xRoot datatype could be Object instead of *.
Please make a downloadable example. You have errors in your tutorial.. Controls.as or Control.as? Basically I can’t get it to work. Please help.
The class, file and constructor must have the same name, so the Control class file should be name Control.as
Oscar thx, it’s very useful!
Damn man, that is OBVIOUS! Its not confusion, you are the one confused. Words that start capitaliced are Classes, and obviously the property of an object called stage is of class Stage, its like:
var point:Point = new Point(2,4);
trace(point.x)
var stage:Stage = myMovieClip.stage;
trace(stage);
Its fairly simple.
I like Oscar’s approach best, see above:
public static var docRoot:Object;
public static var docStage:DisplayObject;
public function DocumentClass() {
docRoot = this;
docStage = this.stage;
}
For anyone still staring out, this can be a confusing issue…
Senocular and some others had another approach of having the document class “extend” another “TopLevel” class (see: http://www.kirupa.com/forum/showthread.php?p=1952513#post1952513 or http://www.actionscript.org/forums/showthread.php3?t=143510), but I don’t really get the point of this approach.
For one, it’s not clearly explained in any of the posts: TopLevel class needs to reside in the same directory as the document class, ie: cannot be accessed in another package using, for example, the path “import com.TopLevel;”. This approach causes compiler reference errors, and I suspect that’s why most people are shrugging their shoulders trying to apply this method.
However, if TopLevel.as resides in the same directory as your document class, you can allow your document class to subclass it, and call without error TopLevel.stage, er whatever static property, from any object in your program.
But, to me this is extra setup that requires more code and an extra class that might cause confusion down the line. Creating static properties in the document class seems more immediate and portable. I’m not really seeing the benefit of the subclass approach…
I am glad theres a flash programmer in the blog sphere. I’m so confused with the FlashMX movie properties to AS3 ,Adobe CS4 Flash