PDA

View Full Version : How to get stage width and height in Flex Builder AS3 project


hieronymous
09-30-2008, 05:58 PM
I'm using Flex Builder to code an AS3 project that does not use the flex framework. I want to position my elements dynamically based upon the stage width and height. Unfortunately I do not seem to have immediate access to stage properties. The following code shows my problem:

package
{
[SWF(width='800', height='430', backgroundColor='#000000')]
public class app extends Sprite
{
public function app():void
{
trace(stage.stageWidth); //0
}
}
}

Is there an event that gets dispatched when the stage has been created and sized? I've already tried listening for the following events.
Event.RESIZE
Event.COMPLETE
Event.INIT
Event.ADDED_TO_STAGE
FlexEvent.CREATION_COMPLETE
FlexEvent.APPLICATION_COMPLETE

Event.ADDED_TO_STAGE is the only one that gets dispatched, and at that point stage.stageWidth still returns 0.

lordofduct
09-30-2008, 06:03 PM
Are you launching this into a InternetExplorer window?

I've heard of people getting this, and it usually only occurs on IE. I launch into Firefox, and into a standalone swf player and never get this bug.

you can also check the loaderInfo object. But you have to wait for it to be fully loaded first.

hieronymous
09-30-2008, 06:25 PM
I'm actually running it in Firefox 3, on a Mac. I went into my bin-debug folder and ran the html file and was surprised to find it worked in opera and safari.

So I guess the problem is in the way the html template is constructed?

hieronymous
09-30-2008, 06:51 PM
Ok, I probably should have been able to figure this out on my own, but I got the answer at http://hubflanger.com/stage-resize-and-the-stagewidth-and-stageheight-properties/

setting stageScaleMode generates an Event.RESIZE event and updates the stage properties. At that point you can get stage.stageWidth and stage.stageHeight.