I am having a fairly huge problem. I am hoping this is due to my own stupidity and not a bug of some sort. I have code that needs to know the screen dimensions. I have not found a reliable way to do that. Using production release of Flash Builder 4.5 on iPad 2 and iPhone4/iPod Touch 4 iOS devices. In general my app works great but I can't determine the screen size and orientation at program start time. Let me explain the problems I am having:
On entry into the "init" function, the one called by the ADDED_TO_STAGE event, the values of stage.stageHeight and stage.stageWidth are both 0.
I can query Capabilities.screenResolutionX and Capabilities.screenResolutionY, but they are WRONG. They have the raw X and Y values, but regardless of the orientation. So for example I start in landscape mode by screenResolutionX contains 768 (or whatever) instead of 1024.
I look at the values of stage.width and stage.height and they don’t have valid values.
I have an onResize function setup for EVENT.RESIZE, but it doesn’t get called if the app is started from the device when it is in landscape mode already. If I start the app in portrait mode and then rotate, this does get called.
So my question is what should I query right at the startup of the app to know the real width and height of the app. There must be a way to do this but apparently not using any of the methods above!
By the way, this is on iOS devices. I can’t say how it works on others. I have confirmed these results both by printing out the results and by running it in the debugger.
Short answer :
The most easy way is to use the stage.fullScreenWidth and stage.fullScreenHeight properties.
They correctly report the screen size for any orientation and any platforms at startup time (no need to wait for ADDED_TO_STAGE or RESIZE events).
More details :
On Android and Blackberry, AIR reports the same (and correct values) at any time when using any of the following properties :
On iOS, at startup, the three syntaxes give three different results (the following example values are for an application running on iPad2 in landscape mode) :
Note that on iOS, the RESIZE event is fired two times when the application starts, but the values are correct only the second time (stage.stageWidth == stage.fullScreenWidth).