Flash Pro AS 3.0 Air for iOS, Orientation Event no

2019-06-09 22:55发布

问题:

I'm developing an app with a default orientation set to Portrait in Flash Air for iOS settings; and the auto orientation is off.

However after adding a certain movieClip I want it to detect if the orientation changes and then change its position for a landscape or portrait display.

The problem is that StageOrientationEvent.ORIENTATION_CHANGE wont be dispatched if I disable the auto orientation. And even if it is enabled, the listener will only dispatch when the orientation changes to upside down or default (not landscapes)

package  
{  

    import flash.display.MovieClip;  
    import flash.events.MouseEvent;  
    import flash.display.Stage;  
    import flash.events.StageOrientationEvent;  
    import flash.display.StageOrientation;  
    import flash.events.Event;  
    public class NoteBook extends MovieClip  
    {  
        public var type: String = new String();  
        public var deviceHeight: Number = new Number();  
        private var nbp: NoteBookPic;  

        public function NoteBook(DeviceHeight: Number = 720)  
        {  
            nbp = new NoteBookPic();  
            this.addChild(nbp);  

            nbp.deviderPic.visible = false;  
            this.deviceHeight = DeviceHeight;  

            this.x = 0;  
            this.y = deviceHeight / 2;  
            this.scaleY = (deviceHeight / 720) * .65;  
            this.scaleX = this.scaleY;  

            this.addEventListener(MouseEvent.CLICK, clicked);  
            this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);  
        }  
        public function exit()  
        {  
            this.removeEventListener(MouseEvent.CLICK, clicked);  
            this.stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orient);  
            this.parent.removeChild(this);  
        }  
        private function clicked(e: MouseEvent)  
        {  

        }  
        private function addedToStage(e: Event)  
        {  
            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);  
            this.stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orient);  
        }  
        private function orient(e: StageOrientationEvent)  
        {  
            trace(e.beforeOrientation, e.afterOrientation)  
        }  
    }  

}  

UPDATE:

I just read in ActionScript® 3.0 Reference for the Adobe® Flash® Platform that StageOrientationEvents are only dispatched for device rotation when autoOrientation is true; but as I mentioned above even if it is enabled, the listener will only dispatch when the orientation changes to upside down or default (not landscapes). Which in my case is a problem as my app must be a portrait app except for that very MovieClip when on my stage. And when it is on stage I want it to react to any orientation changes, two portraits and two landscapes.

回答1:

If you are locking your application to aspectRatio portrait then you'll only receive orientations that satisfy that aspectRatio, so you shouldn't ever get a landscape orientation.

To receive those orientation events you'll need to change your application configuration to set the aspectRatio to any:

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


回答2:

You're using StageOrientationEvent.ORIENTATION_CHANGE which is correct. I didn't go through your code because I don't see key stuff. Below is example key code.

One funky thing is that Capabilities.screenResolutionX and Capabilities.screenResolutionY will give you the screen dimensions but they do not change with device rotation. So you have to keep track of the screen orientation and set width and height yourself.

var bStagePortrait: Boolean;

stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChanged);

if (stage.orientation == StageOrientation.DEFAULT || stage.orientation == StageOrientation.UPSIDE_DOWN) {
    bStagePortrait = true;
} else if (stage.orientation == StageOrientation.ROTATED_LEFT || stage.orientation == StageOrientation.ROTATED_RIGHT) {
    bStagePortrait = false;
}

//  set width and height based on current rotation (Capabilities.screenResolutionX and Y do not change with device rotation)
function fSetScreenDimensions(): void {
    if (bStagePortrait) {
        iWidth = Capabilities.screenResolutionX;
        iHeight = Capabilities.screenResolutionY;
    } else {
        iWidth = Capabilities.screenResolutionY;
        iHeight = Capabilities.screenResolutionX;
    }
}

private function orientationChanged(e: StageOrientationEvent): void {
switch (e.afterOrientation) {
    case StageOrientation.DEFAULT:
        bStagePortrait = true;
        break;
    case StageOrientation.ROTATED_RIGHT:
        bStagePortrait = false;
        break;
    case StageOrientation.ROTATED_LEFT:
        bStagePortrait = false;
        break;
    case StageOrientation.UPSIDE_DOWN:
        bStagePortrait = true;
        break;
    }
}