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.

Advertisements

~ by brianchau on January 13, 2008.

18 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

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

  6. 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);

  7. Well xRoot datatype could be Object instead of *.

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

  9. The class, file and constructor must have the same name, so the Control class file should be name Control.as

  10. Oscar thx, it’s very useful!

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

    😛

  12. 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…

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

  14. Okay, people. Perhaps I can shed some light on all of this. Whomever designed this language was a spastic.

    Now that’s not just abuse, consider this:

    Open a new project (call it whatever you like), leave all of the defaults, and drop a text box in there. Make it a dynamic text box and call it what you like (I chose “counterintuitive”). Then, making sure you don’t have any items selected, press F9 to get your code window. Then type the following:

    counterintuitive.text = stage.width.toString ();

    What you would expect to get is the width of the parent stage area. What you seem to get is the width of the text box.

    Huh?

    Yeah, I heard you. Huh?

    Go ahead! Try it! Resize your text box and you will get a different result for each change.

    Daniel is correct: “Stage” refers to the class and “stage” is a method for the instance of the class (which, by the way, gets me on to the bug up my nose about case-sensitivity – my name is Rex, but you could refer to me as rex or ReX or REx or REX or any of the other three permutations of my name and I’d still be the same freaking person!).

    Where do they find programmers, these days? From a mental institution?

  15. @Rex

    “Stage” is a Class.

    “stage” is a property, not a method, and any DisplayObject has a stage property, which is initialized once that particular DisplayObject is added to the display list and the ADDED_TO_STAGE event is fired. Moreover, the stage property is a reference to a DisplayObject’s parent’s stage property. The stage property itself has properties: the two you’re most interested in are:

    stage.stageWidth
    stage.stageHeight

    So, try this:

    counterintuitive.text = stage.stageWidth.toString();

  16. If it’s a class, then it’s a type 🙂

    Also, “Stage” isn’t a keyword and nor is “stage”. “Stage” is a class which by convention uses the cap S. “stage” with the lower case s is an instance of Stage which happens to be a property of type DisplayObject.

  17. Correction: Sorry I meant to say “stage” is a reference to Stage, not an instance (of many instances). There is only one Stage isn’t there?

  18. One of the stupidest things about this whole thing is that the “stage” property can not be accessed from a class instance before it is added to the display list. This means all instances that are not on the stage are naive to the fact that the Stage even exists.

    This is stupid because it is a regular occurrence for a programmer to write a class whose instances can adapt to their environment (parent or stage) prior to being added to the display list (e.g. what if I want to make my instances based on the proportions of the stage width and height, or locate the instance based on those values). To do this, I must add an event listener to detect when an instance has been added, and then run the operations after it has been added. What if the operations are heavy? For example, what if I load a different large .swf into my instance based on some stage property. Then, I can’t make a decision about which swf the instance should load until the instance has been added, which would cause a freeze, or require a loading screen.

    Another area this presents a problem is when classes use Keyboard listeners. Given the inability to access the stage property when not in the display list, Keyboard listeners must be added and removed from the stage every time an instance is added and removed from the stage.

    What this all means is the if you program classes in as3, the first functions you should write in every class are:

    function addedToStage()

    AND

    function removedFromStage()

    and in these functions, run a bunch of procedures repetitively when instances are added and removed from the display list. If I am not mistaken, I think one of the reasons for writing a program is to ride yourself of repetitive procedures.

    All this crap would go away if as3 instances could access the stage property from anywhere. Which shouldn’t be that hard since cases of multiple stages are rare.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: