hello! 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 lot!
hello! 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 lot!
Any class that is added to the display list can simply use stage.stageWidth and stage.stageHeight. If the instance is not added to the display list, then you can either pass it to the instance, define a Singleton which contains the information or use a public static variable inside your DocumentClass to be used for access.
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.