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

后端 未结 3 498
南方客
南方客 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条回答
  • 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)

    0 讨论(0)
  • 2021-01-15 23:18

    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 :(

    0 讨论(0)
  • 2021-01-15 23:22

    Something like this?

    stage.autoOrients = false;
    
    if(YourDeviceCapsClass.isTablet)
        stage.setOrientation(StageOrientation.ROTATED_LEFT);
    else
        stage.setOrientation(StageOrientation.DEFAULT);
    
    0 讨论(0)
提交回复
热议问题