0

我正在创建一个 flex 移动项目,我想在单击按钮时强制应用程序为纵向,当我单击其他按钮时再次允许更改方向。

这是我单击第一个按钮时的代码,我想强制纵向模式:

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;
}

它工作正常,如果需要,它会改变方向。

现在,当我想再次允许改变方向时,我只放了:

stage.autoOrients = true;

如果当我单击第一个按钮时应用程序是纵向的并且不需要更改任何内容,它就可以正常工作。但是,如果它是横向的并且必须更改为纵向,那么当我再次允许更改方向时,它就无法正常工作。

你知道我是否必须允许或改变一些事情吗?或者,有没有更好的方法来做到这一点?

提前致谢

4

1 回答 1

0

这是一个小技巧,但我已经解决了。

首先,如果必须改变方向,我会保存旧的方向。当我再次单击允许更改方向的按钮时,首先我放置旧的方向,然后我允许自动定位。

这是新代码的一部分:

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
}

允许自动定向的按钮:

if(oldOrientation != null){

        stage.setOrientation(oldOrientation);
        oldOrientation = null;              
}
于 2013-03-11T17:45:59.487 回答