ActionScript File Icon

stage.stageWidth returning incorrect values ? Let me help.

Are you trying to fetch the actual stage width or height of your project on startup and are getting weird results ? Perhaps it reports a width of 500 and a height of 375 ? If that’s the case, read on, I might have a solution for you.

If you are using Flash Builder which, in turn, is using the mxmlc compiler behind the scenes, you might retrieve strange results when calling stage.stageWidth or stage.stageHeight. If you are not using the [SWF] tag, the actual default size of your project will be 500×375 all the way until Event.RESIZE fires. This is because mxmlc (the command-line AS3 compiler) is using those values as defaults.

On the other hand, if you are using a [SWF] tag, the values reported will match what you requested (again, before Event.RESIZE is triggered). In many cases that’s not what we want. Let’s suppose I’m creating an Air application meant for the iPhone 3GS. In such a case I will define my [SWF] tag as such and everything will work fine :

[SWF(frameRate=60, width=320, height=480)]

However, if I want to also target the iPhone 4S which has a different resolution (640×960), what should I do ? If I leave everything as is, the size reported before Event.RESIZE fires will be 320×480 but after, it will be 640×960. That is to say the size set in the [SWF] or with the compiler’s -default-size option is pretty much useless in that case.

The solution simply is to wait for Event.RESIZE to fire before doing anything that depends on the stage dimensions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.