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

actionscript-3, stage

Solution

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.

Problem

``` 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?

Original source