Skip navigation
Currently Being Moderated

Orientation at startup

Nov 28, 2010 10:37 AM

Hello.

I am working on iPad application. I have 2 versions of my UI - one for portrait and another for landscape device orientation.

I know how to use StageOrientationEvent in order to switch my display object from portrait to landscape version. This works fine when changing the orientation of my iPad.

The problem is that I don't know how to detect the device orientation during the startup.

Unfortunately Srage.deviceOrientation returns 'unknown".

I added if statetement in my Event.ADDED_TO_STAGE event handler. My scrip looks like this:

 

 

public function MyPadApplication():void {
     addEventListener(Event.ADDED_TO_STAGE, init);
 }
 
 
private function init(e:Event = null):void {
      removeEventListener(Event.ADDED_TO_STAGE, init);
 
     if(stage.deviceOrientation == StageOrientation.ROTATED_LEFT || stage.deviceOrientation == StageOrientation.ROTATED_RIGHT){
          //Show the landscape version of my UI
     } else if(stage.deviceOrientation == StageOrientation.DEFAULT || stage.deviceOrientation == StageOrientation.UPSIDE_DOWN){
          //Show the portrait version of my UI
     } else {
          //In case of unknown device orientation
     }
 }

 

 

MyPadApplication is the name of my document class. In init function stage.deviceOrientation is always "unknown". So I always go in the last else.

Later, after the first orientation change stage.deviceOrientation starts to get the device orientation properly.

 

 

Do you have any suggestions how to solve this problem?

Thank you in advance.

 

p.s. In case you need this information - I set autoOrients to true and Aspect ratio to portrait. I also set the stage to TOP_LEFT and NO_SCALE.

 
Replies
  • Currently Being Moderated
    Nov 30, 2010 1:08 PM   in reply to Sacredcolours

    I would like also to know if anyone knows a solution, cos i have the same problem?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 15, 2010 1:03 PM   in reply to tsvete

    Is there anyone that have a solution for this problem 

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 15, 2010 1:14 PM   in reply to Sacredcolours

    Try this:

     

     

    private var startOrientation:String = stage.orientation;

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 15, 2010 5:04 PM   in reply to Colin Holgate

    I actually threw an enter frame code on the first frame of the movie, where it detects the stage orientation. It then does a series of commands based on what the orientation is. I also used the noscale and align upper left code you talked about.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 25, 2011 10:20 AM   in reply to Steve Broome

    Detecting stage.orientation on the first frame (via enter frame event) does nothing for me. It always returns "default" no matter what.

     

    If I launch the test app on my iPad holding it in portrait mode (I have the default orientation on portrait) and it launches fine; the Default-Portrait.png opens, followed by the staged at the correct orientation, stage.orientation returns "default", and any rotations to the device work properly thereafter.

     

    However, if I launch the app while holding the iPad in landscape mode, Default-Portrait.png still opens (I have all of the correct Default.pngs included), stage.orientation still returns "default", and my stage shows up in portrait mode while the device is in landscape. After about a second, my ORIENTATION_CHANGE event listener fires and detects that it should switch the stage to landscape mode.

     

    I can't help wondering if this problem's solution lies in showing the correct Default.png. I've seen some threads on here asking about it, but the only "solution" I've seen involves making sure you include all of the Default.pngs.

     

    I also updated to AIR 2.5 beta extension (http://labs.adobe.com/technologies/flashpro_extensionforair/). No change.

     

    My code, on the first and only frame:

    import flash.events.StageOrientationEvent;
    import flash.events.Event;
     
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;
     
    // Detect orientation on startup
    stage.addEventListener(Event.EXIT_FRAME, startupOrient, false, 0, true);
    function startupOrient(event:Event):void{
         var startOrient:String = stage.orientation;
         trace("Startup Orientation:",startOrient);
         changeOrientation(startOrient);
         stage.removeEventListener(Event.EXIT_FRAME, startupOrient);
         // Start listener for orientation change events
         stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, detectOrientation, false, 0, true);
    }
     
    // Detect orientation on orientation change
    function detectOrientation(event:StageOrientationEvent):void{
         trace("Orientation was:",event.beforeOrientation);
         changeOrientation(event.afterOrientation);
    }
     
    // My 2 UI MovieClips, one for portrait and one for landscape
    viewLandscape.visible = false;
    viewPortrait.visible = false;
     
    // With Auto orientation set to true and default aspect as portrait (iPhone OS Settings)
    // show different display on orientation change
    function changeOrientation(ORIENTATION:String):void{
         if(ORIENTATION == StageOrientation.ROTATED_RIGHT || ORIENTATION == StageOrientation.ROTATED_LEFT){
              viewLandscape.visible = true;
              viewPortrait.visible = false;
         } else {
              viewLandscape.visible = false;
              viewPortrait.visible = true;
         }
         trace("Orientation is now:",ORIENTATION);
    }
    

     

    iPhone OS Settings:

    Aspect ratio: Portrait

    Full screen: ON

    Auto orientation: ON

    Rendering: Auto

    Device: iPad

     

    Included files:

    HelloWorld.swf

    HelloWorld-app.xml

    Default-Landscape.png

    Default-Portrait.png

    Default.png

     

    Stage size is 768 x 1024 px.

     

    What am I doing wrong? Or is this an issue that never gets solved, and everyone's given up on?

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 26, 2011 3:10 PM   in reply to Sacredcolours

    hi, perhaps its not this easy on the iphone, but on android i

     

    stage.addEventListener(Event.RESIZE,doStageLayout);

     

    In my case I have only landscape set. The event fires once at startup , i layout my items and remove the listener, as it will never be needed again.

     

    but if you do expect oriantation changes,

    if (width>height) , your in landscape

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 28, 2011 11:44 PM   in reply to Sacredcolours

    Is there any progress on this issue?

     

    Anyone? Please.

     

    Is it possible with the new Air? 

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2011 6:12 AM   in reply to tsvete

    There are a few pages of information about what is new in AIR 2.6, but I don't think I've seen any pages on what isn't new, but is just a fix compared to the old packager. So, it could well be worth trying things that used to go wrong and see if they're still a problem in 2.6.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2011 3:33 PM   in reply to Colin Holgate

    i have seen this issue addressed, at least in what i was seeing on my ipad. in the last packager, i had this code:

     

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

     

    and it would still not work - it woudl start up upsdie down, or re-orient regardless of of it was in the right otientation or not.

     

    in the new AIR SDK, this work as expected now. very nice.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points