【问题标题】:LibGDX Game set to landscape, but showing up as portrait only on certain devicesLibGDX 游戏设置为横向,但仅在某些设备上显示为纵向
【发布时间】: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


【解决方案1】:

由于到目前为止没有人帮助您,我将发布我的 android 清单,您也许应该发布您的清单,因为这几乎肯定是问题所在。我很久以前就设置了它,老实说,我真的不记得为什么一切都是这样设置的,但它对我有用。注意:我在某些模式下使用设备旋转来控制我的游戏。

至于您的手动屏幕旋转问题,您可以访问相机投影矩阵并相应地对其进行缩放以使其看起来正常。不过我不建议这样做,因为当您还必须跟踪屏幕旋转时,很难跟踪屏幕调整大小调用应该如何改变您的布局。您必须考虑在纵向模式下宽度变化是否实际上应该被视为高度变化,反之亦然(取决于您的游戏复杂性)。我建议只整理你的清单。祝你好运。

        android:screenOrientation="fullSensor"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.funferret.tr.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="sensorLandscape"
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

【讨论】:

  • 将方向设置更改为您没有进行的设置。就像你说的,我会避免弄乱相机,但我现在会根据你的建议再试一次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多