方向总是纵向(libgdx 中的运动控制)
Orientation always result to portrait (Motion Controls in libgdx)
我的代码如下:
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";
}
上面的代码(渲染方法内部)始终指示设备处于纵向模式,即使我旋转设备也是如此!我究竟做错了什么?如何准确检测设备是纵向模式还是横向模式?
getNativeOrientation()
不是 return 设备的当前方向,它是 return 当您正确握住它时屏幕是横向还是纵向(在大多数手机上)它应该 return 纵向,但我猜在像 HTC ChaCha 这样的平板电脑和手机上 return 是横向)。
有几种获取当前方向的方法:
- 推荐: 使用
Gdx.input.getRotation()
设备相对于其原始方向 return 的旋转角度(0、90、180、270) .将它与 getNativeOrientation()
一起使用,您应该能够为所有设备获得正确的方向。
注意:它为您提供当前应用程序状态的方向,而不是设备作为物理对象的方向。因此,如果您的清单中有 android:screenOrientation="landscape"
,此方法将始终 return 横向。
用法示例:
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),传递给游戏。
如果您将其用作控件,也许您应该使用 accelerometer or gyroscope 而不是
我的代码如下:
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";
}
上面的代码(渲染方法内部)始终指示设备处于纵向模式,即使我旋转设备也是如此!我究竟做错了什么?如何准确检测设备是纵向模式还是横向模式?
getNativeOrientation()
不是 return 设备的当前方向,它是 return 当您正确握住它时屏幕是横向还是纵向(在大多数手机上)它应该 return 纵向,但我猜在像 HTC ChaCha 这样的平板电脑和手机上 return 是横向)。
有几种获取当前方向的方法:
- 推荐: 使用
Gdx.input.getRotation()
设备相对于其原始方向 return 的旋转角度(0、90、180、270) .将它与getNativeOrientation()
一起使用,您应该能够为所有设备获得正确的方向。
注意:它为您提供当前应用程序状态的方向,而不是设备作为物理对象的方向。因此,如果您的清单中有 android:screenOrientation="landscape"
,此方法将始终 return 横向。
用法示例:
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),传递给游戏。
如果您将其用作控件,也许您应该使用 accelerometer or gyroscope 而不是