Question

"StageOrientationEvent.ORIENTATION_CHANGE" event does not fire (dispatch) when the Auto Rotate feature of the device is disabled.

I'm developing a game that only must work on PORTRAIT mode. I speculate many users unchecked this option display settings.

How can I detect screen rotation regardless of device setting. Should I use accelerometer for this purpose or there is a better way?

Auto-rotate screen" option in Display settings of Google Nexus

Was it helpful?

Solution

If your game must always work in PORTRAIT mode; then you don't have to worry about device orientation changes. What you want to do is make sure your game always launches in portrait mode despite what the device settings are.

In the Application Descriptor file; set these values:

<aspectRatio>portrait</aspectRatio>
<autoOrients>false</autoOrients>

The application descriptor file is the "MainApplication-app.xml' file that Flash Builder will create as you create your main application.

In documentation theory those changes should do it; but if memory serves me I had problems in some versions of Android, where the app launched in the wrong orientation and would not change.

So, inside my application I added applicationComplete and activate handlers on my main application file and used those methods to specify the aspect ratio of the game. Something like this:

<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
applicationComplete="viewnavigatorapplication1_applicationCompleteHandler(event)"
activate="viewnavigatorapplication1_activateHandler(event)"> 
<fx:Script><![CDATA[
    import mx.events.FlexEvent;

    protected function viewnavigatorapplication1_applicationCompleteHandler(event:FlexEvent):void{
      this.stage.setAspectRatio( StageAspectRatio.PORTRAIT);
    }

    protected function viewnavigatorapplication1_activateHandler(event:Event):void{
      if(this.stage){
        this.stage.setAspectRatio( StageAspectRatio.PORTRAIT );
      }
}

]]></fx:Script>
</s:ViewNavigatorApplication>

I have no memory of testing this specific use case on iOS Devices; so it is possible I only had the problem on Android. Or it is possible I did the Android code first and that is why I never had a problem on iOS.

OTHER TIPS

Although I have used this method, I cannot take credit for it but please see: Please see this thread

Remember if you are developing for iOS, you CANNOT LOCK Device Orientation. You HAVE to add event listeners to handle orientation changes for iOS. The very first app I did got rejected for trying to lock orientation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top