AS3/AIR: If phone use portrait, if tablet use land

2019-02-20 20:07发布

Ok, I've written myself a simple DeviceCapabilites class to be able to check if the device is a phone or tablet etc.

But I need to be able to say that if the user is on a phone, it should be in portrait mode, and if on tablet I only wanna use landscape mode... Any ideas?

EDIT: To make it clear, I want to lock the orientation to portrait mode on phones and then use landscape on tablets.

3条回答
做自己的国王
2楼-- · 2019-02-20 20:34

You'll have to combine the setOrientation() method mentions by Barış Uşaklı with some more logic, I am afraid. As I mentioned in my comment to him, StageOrientation.DEFAULT refers to the default orientation of the device, but you don't know if that is landscape or portrait.

Fortunately, there is a simple way to figure that out. You simply see if the device is in default/upside down orientation and check that against the width/height.

var defaultPositionIsLandscape:Boolean = false;
if ( stage.orientation == StageOrientation.DEFAULT || stage.orientation == StageOrientation.UPSIDE_DOWN ) {
    defaultPositionIsLandscape = stage.stageWidth > stage.stageHeight;
}
else {
    defaultPositionIsLandscape = stage.stageWidth < stage.stageHeight;
}

if ( isTablet ) {
    if ( defaultPositionIsLandscape ) {
        stage.setOrientation( StageOrientation.DEFAULT );
    }
    else {
        stage.setOrientation( StageOrientation.ROTATED_LEFT );
    }
}

The logic is a little messy so you could probably clean it up a bit, but that is the general gist of what has to happen. I personally would make the top part a static, read-only getter in your DeviceCapabilities class for ease of access. You'll also want to expand it to avoid rotating if it is in StageOrientation.UPSIDE_DOWN (since that is technically the correct orientation that you want, just upside down)

查看更多
在下西门庆
3楼-- · 2019-02-20 20:43

I dont have an answer but I'd like to save someone else some time. When testing on an ipad, the above code posted by Josh will always result in "defaultPositionIsLandscape" to be false :(

查看更多
手持菜刀,她持情操
4楼-- · 2019-02-20 20:52

Something like this?

stage.autoOrients = false;

if(YourDeviceCapsClass.isTablet)
    stage.setOrientation(StageOrientation.ROTATED_LEFT);
else
    stage.setOrientation(StageOrientation.DEFAULT);
查看更多
登录 后发表回答