方向始终为纵向(libgdx中的运动控制)

时间:2016-10-04 21:31:59

标签: java android libgdx orientation

我的代码如下:

Input.Orientation orientation = Gdx.input.getNativeOrientation();
message = "";
switch (orientation) {
    case Landscape:
        message += "Landscape\n";
        break;
    case Portrait:
        message += "Portrait\n";
        break;
    default:
        message += "Whatever\n";
}

上面的代码(内部渲染方法)总是表示设备处于纵向模式,即使我旋转设备!我究竟做错了什么?如何准确检测设备是处于纵向还是横向模式?

1 个答案:

答案 0 :(得分:5)

getNativeOrientation()没有返回设备的当前方向,当你正确握住它时,它会返回屏幕是横向还是纵向的东西(在大多数手机上它应该返回肖像,但我猜平板电脑和手机,如HTC ChaCha它返回风景)。

有几种方法可以获得当前的定位:

  • 推荐:使用Gdx.input.getRotation()以相对于其原始方向的度数(0,90,180,270)返回设备的旋转。与getNativeOrientation()一起使用,您应该能够为所有设备找到正确的方向。

注意:它为您提供当前应用状态的方向,而不是设备作为物理对象的方向。因此,如果您的清单中有android:screenOrientation="landscape",则此方法将始终返回格局。

使用示例:

int rotation = Gdx.input.getRotation();
if((Gdx.input.getNativeOrientation() == Input.Orientation.Portrait && (rotation == 90 || rotation == 270)) || //First case, the normal phone
        (Gdx.input.getNativeOrientation() == Input.Orientation.Landscape && (rotation == 0 || rotation == 180))) //Second case, the landscape device
    Gdx.app.log("Orientation", "We are in landscape!");
else
    Gdx.app.log("Orientation", "We are in portrait");
  • 在原生端(Android)上创建界面,将其传递给游戏。

  • 如果您使用此作为控件,则可以使用accelerometergyroscope代替

相关问题