Playing with stage.orientation in mobile AIR apps

转载: http://polygeek.com/4301_air-mobile_playing-with-stage-orientation-in-mobile-air-apps

Playing with stage.orientation in mobile AIR apps

If you create a mobile app or game you may think that you want the orientation locked to either portrait or landscape all the time. You can do that by setting the appropriate properties in the application descriptor file – app.xml. If you do want to lock it down then just use:

<aspectRatio>portrait</aspectRatio>
<autoOrients>false</autoOrients>

That will lock the app to portrait, always. Or of course change it to <aspectRatio>landscape</aspectRatio> to lock it to landscape. It’s that easy.

But you don’t want to do that, probably.

It’s quite possible that you will come into instances where you need to switch back and forth or at least have the option for switching. For instance, you’re game may only play in landscape mode it might be awkward for the user to see their high scores screen in landscape.

A better option is to set the orientation in code.

stage.autoOrients = true/false;
stage.setOrientation( StageOrientation.DEFAULT );

If you want just create a simple Flex Mobile project and add these buttons to the HomeScreen:

<s:VGroup width="100%" height="100%" >

	<s:Button label="set DEFAULT" width="100%" click="{ stage.setOrientation( StageOrientation.DEFAULT ) }" />

	<s:Button label="autoOrient = true" width="100%" click="{ stage.autoOrients = true }" />

	<s:Button label="autoOrient = false" width="100%" click="{ stage.autoOrients = false }" />

</s:VGroup>

Set the -app.xml file with autoOrients=true and the aspectRatio to be what you want the app to look like by default – I’m using portrait in this example.

Here’s what happens if you start the app up with the phone in landscape:

  • Since I set the aspectRatio to portrait it will start up that way even though physically the phone oriented in landscape.
  • Now if you rotate the phone to portrait you’ll get a re-orientation flicker. It will notice that the orientation changed and try to go to landscape and then realize it’s in portrait and then re-orient appropriately. I’d say this is a minor bug with the Flex framework. It shouldn’t create a re-orientation flicker. But whatever.
  • Now if you re-orient the phone back and forth between portrait and landscape the layout of the GUI will adapt accordingly. If you hit the autoOrient = false button then it will stop re-orienting and be locked into which ever orientation it was when you hit the button.

This should account for most use cases. If you have a game you can set the default to landscape and always be able to force the user back to that orientation. But then when the game is over you can let them switch the orientation to portrait to better view their high scores.

Besides StageOrientation.DEFAULT you also have the option to set the orientation as so:

  • stage.setOrientation( StageOrientation.ROTATED_LEFT );
  • stage.setOrientation( StageOrientation.ROTATED_RIGHT );
  • stage.setOrientation( StageOrientation.UPSIDE_DOWN );
  • stage.setOrientation( StageOrientation.UNKNOWN );

For instance in the RunPee app I set the default orientation to portrait and lock it because the layout works best that way. But there is a screen for viewing videos on youTube.com so I turn autoOrient back to true and do a ROTATED_LEFT to orient the screen best for viewing the video. Then when they leave the video screen I set it back to DEFAULT and lock it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值