Why is stage undefined in my Actionscript 3 class even though I've imported the Stage class?

mltt picture mltt · Jan 12, 2010 · Viewed 9k times · Source
 package    {

 import flash.display.Stage;

 public class MyGlobal {

  public static  var CX:Number = stage.stageWidth / 2;
  public static  var CY:Number = stage.stageHeight / 2;  

 }

}

The Error is "1120: Access of undefined property stage." WHY?

Answer

Joel Hooks picture Joel Hooks · Jan 12, 2010

As mentioned, stage is a property of classes that inherit from DisplayObject. Stage is not available to a class until it has been added to the stage, generally via the addChild method of a DisplayObjectContainer.