【发布时间】:2020-11-25 03:13:11
【问题描述】:
我正在制作一个 LibGDX (1.9.9) 游戏,该游戏本应在横向模式下进行修复,但当我在某些设备上运行它时,它会以纵向模式显示。
我已经在从 Android 4 到 11 的多个物理和虚拟设备以及通过 RoboVM (2.3.9) 的 iPhone 模拟器上对其进行了测试。这发生在 android 虚拟平板电脑 (Android 11) 和 iPhone 模拟器上。它在物理设备上也被卡在肖像中几次,但我无法重现它。我一直无法理解为什么会这样。
我在 android 清单中设置了横向 (android:screenOrientation="landscape")。
Gdx.input.getRotation() 在横向正确设置时返回 90,但在纵向设置时返回 0,所以也许我可以尝试检查它是否为 0 并动态更改旋转,但我无法找到找出如何做到这一点。
我尝试了一种解决方法,我检查屏幕宽度是否
How camera distorts when trying to rotate to landscape
我正在使用没有任何视口的 OrthographicCamera。
// Screen dimensions
width = ((float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight()) * GameSpecs.WORLD_HEIGHT;
height = GameSpecs.WORLD_HEIGHT;
System.out.println("width: " + width);
System.out.println("height: " + height);
System.out.println("orientation: " + Gdx.input.getNativeOrientation());
System.out.println("rotation: " + Gdx.input.getRotation());
// If showing portrait and needs to be rotated
rotateForProperLayout = width < height;
if (rotateForProperLayout) {
height = ((float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth()) * GameSpecs.WORLD_HEIGHT;
width = GameSpecs.WORLD_HEIGHT;
}
cam = new OrthographicCamera(width, height);
// Rotate camera to be in landscape
if (rotateForProperLayout) {
cam.rotate(-90);
}
cam.translate(cam.viewportWidth / 2f, cam.viewportHeight / 2f);
cam.update();
我的 Android 清单...
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:isGame="true"
android:appCategory="game"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
<activity
android:name="com.mydev.mygame.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation=“landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
非常感谢您在尝试了解此原因或可能的解决方法方面的任何帮助。如果我应该澄清任何事情或提供更多信息,请告诉我。
提前致谢。
【问题讨论】:
-
我刚刚注意到你在 android:screenOrientation 之后有一个奇怪的引用,并且语法着色搞砸了。假设这是直接从您的清单中复制的,这可能是您的问题。如果是的话,我很惊讶它居然能正常运行。
-
@funferret-com 我想那是我把它贴在这里的时候。我重新输入了该行并运行了同样的问题。
-
对不起,我不知道出了什么问题。我假设您已经完成了项目的完整清理和重建,有几次我浪费了一两个小时来寻找一些在清理项目时消失的不存在的错误。
-
再强调一点,您可能会发布更多代码,因为我们无法真正看到它在做什么。例如。我假设您发布的代码是在调整大小时调用的,而不仅仅是在 create 函数或类似的傻事中。
-
嗯。我不确定我还能发布什么相关的内容,因为它是固定在横向上的,我没有使用调整大小。我引用相机的唯一其他地方是为 SpriteBatch 设置投影矩阵时... game.batch.setProjectionMatrix(game.cam.combined);这似乎只是 Android 11 和 iOS 的问题,所以它可能是 LibGDX。我可能只需要一个解决方法。感谢您的建议。
标签: android ios mobile libgdx game-development