开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜