AS3/AIR: If phone use portrait, if tablet use landscape?

后端 未结 3 526
南方客
南方客 2021-01-15 23:02

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 i

3条回答
  •  萌比男神i
    2021-01-15 23:09

    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)

提交回复
热议问题