Question

I'm creating a flex mobile project and I want to force the app to portrait orientation when I click a button, and when I click other button allow again to change the orientation.

This is my code when I click the first button, where I want to force portrait mode:

protected function click(event:MouseEvent):void{
   if(stage.orientation != StageOrientation.DEFAULT){
            stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);
            stage.setOrientation(StageOrientation.DEFAULT);
   }else{
            doSomething();
   }

}


private function orientationChanged(event:StageOrientationEvent):void{
            doSomething();
            stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);
}

private function doSomething():void{
            stage.autoOrients = false;
}

It works ok and it changes the orientation if it's needed.

Now, when I want to allow orientation change again, I've only putted:

stage.autoOrients = true;

It works ok if when I click the first button the app is in portrait and it doesn't have to change anything. But if it's on landscape and it have to change to portrait, when I allow orientation change again, it doesn't work ok.

Do you know if I have to allow or change something? Or, is there any better way to do this?

Thanks in advance

Was it helpful?

Solution

It's a little trick, but I've solved it.

First, if the orientation has to change, I save the old orientation. When I click the button that allow orientation change again, first I put the old orientation and then, I allow auto-orientation.

This is part of the new code:

private var oldOrientation:String = null;

protected function click(event:MouseEvent):void{
   if(stage.orientation != StageOrientation.DEFAULT){
            oldOrientation = stage.orientation; 
            stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);
            stage.setOrientation(StageOrientation.DEFAULT);
   }else{
            doSomething();
   }

}


private function orientationChanged(event:StageOrientationEvent):void{
            // do something if you are changing to portrait mode (the first change) and other thing if you are changing to old orientation
}

The button to allow autoorientation:

if(oldOrientation != null){

        stage.setOrientation(oldOrientation);
        oldOrientation = null;              
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top