views:

506

answers:

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

+5  A: 

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.

Joel Hooks
+1  A: 

The stage property is only available to display objects that have been added to the stage. Your class does not inherit from DisplayObject, so the stage property does not exist for your class. Even if your class did inherit from DisplayObject, it needs to be added to the stage via addChild() from a parent display object.

If your class is not meant to be a display object, but needs the stage object for measurements or other properties, send it as an argument to the constructor.

Otherwise, make your class inherit DisplayObject, then you have to add an event listener for when the object is added to the stage:

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

In the onAddedToStage() method, stage will now be available (EDIT: the stage property will be available as long as the top most parent container has been added to the stage).

wmid
A: 

Anyway, how can i create an global Var, which contain real Movie width (like 800x600)? And access this Var in a script file, or inside frame script.

mltt
A: 

The answer from Sam is actually quite right. The easiest explanation is that stage is an instance and not a class member.

If your fields were not declared static, you would be able to access stage. But you would probably get a null-reference error ;)

Joa Ebert