Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex 4.5 Mobile iOS problems with determining actual screen/stage resolution

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.

like image 717
Rich Sadowsky Avatar asked May 03 '11 20:05

Rich Sadowsky


2 Answers

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 :

  • stage.stageWidth & stage.stageHeight
  • Screen.mainScreen.bounds.width & Screen.mainScreen.bounds.height
  • stage.fullScreenWidth & stage.fullScreenHeight

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) :

  • stage.stageWidth/Height : the default stage size when launching a swf (500x375)
  • Screen.mainScreen.bounds : the physical resolution of the screen in portrait mode (768x1024)
  • stage.fullScreenWidth/Height : the correct screen resolution in the given orientation. (1024x768 - correct)

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).

like image 58
boblemarin Avatar answered Oct 05 '22 12:10

boblemarin


I have previously had issues with stageWidth and stageHeight not giving proper values immediately on startup, an easy way to get around this is to wait a frame or two before checking them.

One option is to delay the initialization of your app, something along the line of this:

private var _startup_delay:int = 10;

public function Constructor(){
    addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}

public function handleEnterFrame(e:Event):void{
    _startup_delay--;
   if(_startup_delay <= 0){
       init();
       removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
    }
}

Another option is to instead dispatch a fake resize event and let your previous rotation code deal with it once the delay has passed:

public function handleEnterFrame(e:Event):void{
    _startup_delay--;
   if(_startup_delay <= 0){
       stage.dispatchEvent(new Event(Event.RESIZE));
       removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
   }
}
like image 40
grapefrukt Avatar answered Oct 05 '22 12:10

grapefrukt