get stage.stageHeight or stage.stageWidth from imported class
can you tell me a simple and clean way to pass the dimension of the stage to another class, imported in my documentclass?
thanks a l开发者_如何学运维ot!
Try putting these two lines in the constructor of the Class that you want to use the stage width/height:
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
Make sure you import the flash.events.Event
class.
Then create this method inside the same Class:
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
trace(stage.stageWidth, stage.stageHeight);
}
This init
method will be called only when your class is added to the stage. This means the stage variable will be accessible (not null).
This is just a test to show that the stage object is only available when the displayObject is added to the display list.
精彩评论