LibGDX - 具有静态最终的单例 class 导致 TextureRegion 失败?

LibGDX - Singleton class with static final causes TextureRegion failure?

我有一个 class 看起来像这样:

public class MyResources
{

    public static final Size textureSize = new Size(72, 96);
    public Texture texture;
    public TextureRegion textureRegion;

    private static BGRResources instance = null;

    protected BGRResources()
    {
        // Protected to prevent direct instantiation.

        texture = new Texture(Gdx.files.internal("data/mytex.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        textureRegion = new TextureRegion(texture, 0, 0, 72, 96);
        //textureRegion = new TextureRegion(texture, 0, 0, textureSize.width, textureSize.height);

    }

    public static MyResources getInstance()
    {
        if (instance == null)
        {
            instance = new MyResources();
        }
        return instance;
    }
}

大小是一个简单的 class,只有 public 宽度和高度浮动。

我想把设置textureRegion的那一行替换成下面那一行,被注释掉了。当我这样做时,该区域不再起作用——当它被绘制在图像中时我什么也得不到。上面这行,对我的 C 编程(即预处理器)大脑来说应该是完全相同的,工作正常。

为什么?我尝试在更简单的非 libGDX class 中复制该问题,但做不到。我想也许静态最终大小 class 在用于创建 textureRegion 之前实际上并没有被实例化,但是调试器和我简化它的尝试似乎都证明了这一点。

如前所述,我是C位的,这样的事情顺序很清楚。它在这里对我来说变得混乱,所以我可以使用任何关于如何正确声明 #define 等价物或单例 classes 的教育,这些可能会伴随答案而来。谢谢

您提到您的尺寸 class 有两个用于宽度和高度的浮点字段。当您使用您的 Size class 实例化您的 TextureRegion 时,由于方法重载,它实际上调用了一个单独的构造函数,而不是您传入整数常量时。该单独的构造函数需要标准化为 0 - 1.0 范围内的 UV 坐标。查看 TextureRegion API and the Textures section of open.gl 了解更多详情。

针对您的用例最简单的解决方法是在您的大小 class 中使用整数而不是浮点数。