AS3 - iOS force landscape mode only?
Asked Answered
H

3

6

I've built a game for iOS that is designed for landscape mode and it works just fine except that if you turn the device to portrait mode it stops there to with the interface only filling about the middle half of the screen. How can I force the app to only allow the two landscape modes and not stop at the portrait position.

I've tried all the publishing settings. Aspect Ratio is set for Landscape and Auto Orientation is checked on, if I uncheck AUto Orientation that the app does not rotate to the other landscape mode and I hear that's an automatic rejection from Apple.

This app is ready to go except for this little flaw. Thanks for any help you can offer.

Rich

Harmonist answered 2/7, 2011 at 19:50 Comment(2)
I'm missing the difference between what you're asking to do and what Apple rejects...You want to leave auto rotate on but only let the app rotate to landscape orientations and Apple rejects for not supporting all orientations if it makes any sense for the application to function in all states.Imco
Apple does not require that an iPad app allows both landscape and portrait, they only require that both landscape's or both portrait's work. The Flash built ipa was allowing my landscape app to stop in the portrait positions to and shrinking the content to fit. The code below worked to solve the problem.Harmonist
R
10

I found the answer to this over on the Adobe forums a few days ago.

Set Aspect Ratio to Auto.

Set Auto Orientation to allowed.

Then use this code on your main clip:

var startOrientation:String = stage.orientation;
if (startOrientation == StageOrientation.DEFAULT || startOrientation == StageOrientation.UPSIDE_DOWN)
  {
    stage.setOrientation(StageOrientation.ROTATED_RIGHT);
  }
  else
  {
    stage.setOrientation(startOrientation);
  }                    

stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChangeListener);

function orientationChangeListener(e:StageOrientationEvent)
{
   if (e.afterOrientation == StageOrientation.DEFAULT || e.afterOrientation ==  StageOrientation.UPSIDE_DOWN)
   {
     e.preventDefault();
   }
}

Worked fine for me..

Richmound answered 3/7, 2011 at 2:32 Comment(0)
B
2

Another, albeit similar, way to do this is to set the aspectRatio to landscape in the application descriptor and autoOrients to false. Then enable autoOrients once the application has loaded and listen for the orientationChanging event. In Flash Builder 4.6, this may be done as follows:

Descriptor:

<aspectRatio>landscape</aspectRatio>
<autoOrients>false</autoOrients>

Application code:

private function applicationCompleteHandler():void {
    stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, stageOrientationChangingHandler);
    stage.autoOrients = true;
}

private function stageOrientationChangingHandler(event:StageOrientationEvent):void {
    if (event.afterOrientation == StageOrientation.DEFAULT || event.afterOrientation == StageOrientation.UPSIDE_DOWN) {
        event.preventDefault();
    }
}

It looks like you also have to make sure that the application has explicit dimensions; otherwise, views may still be resized even through the stage does not reorient.

Please see: http://bugs.adobe.com/jira/browse/SDK-32082

Briarwood answered 11/1, 2012 at 16:7 Comment(0)
B
1

The answer above did not work for me, because on iOS the preventDefault no longer works.

For anyone who just still isn't having good luck... Here's what I did.

I wanted:

Ipad & All Android Tablet devices to be able to Portrait or Landscape and auto-rotate in any direction

Iphones, Ipods, & All Small Screen Devices to be forced Landscape (or maybe in your case you want forced portrait).

I Did:

IN my -app.xml:

<aspectRatio>any</aspectRatio>
<autoOrients>true</autoOrients>

In Actionscript:

        var screenDPI:Number = Capabilities.screenDPI;
        var resolutionX:Number = Capabilities.screenResolutionX;
        var resolutionY:Number = Capabilities.screenResolutionY;            
        var numDiagonalDistance:Number = Math.round(Math.sqrt((resolutionX*resolutionX)+(resolutionY*resolutionY)));            
        var numDiagonalInches:Number = numDiagonalDistance/screenDPI;


            if(numDiagonalInches<6){
                MainData.SMALL_DEVICE=true;
            }else{
                MainData.SMALL_DEVICE=false;                        
            }


        if(MainData.SMALL_DEVICE){
            this.stage.setAspectRatio(StageAspectRatio.LANDSCAPE);
        }

Though I guess you could easily put StageAspectRatio.PORTRAIT or even use the reverse logic to logic ipads only or something. Good Luck!

EDITED: I'm actually editing this because even this method still has problems. You'll get landscape but its not always the landscape oriented to how you're holding the device...

EDITED AGAIN (2-27-2014): Turns out if I included -swf-version 23 (now with AIR 4.0) this problem is finally solved.

Broker answered 24/5, 2013 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.