header image
 

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.

~ by brianchau on January 13, 2008.

4 Responses to “AS3: Stage or stage?”

  1. 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.

  2. Stage - refers to the class flash.display.Stage.as.

    stage - refers to an instance of the above class.

  3. 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?)

  4. Roger, stage is a property, not a movieclip. Try the following instead:

    MovieClip(this.parent).Registry

Leave a Reply