LibGDX - 具有静态final的Singleton类导致TextureRegion失败?

时间:2016-01-24 21:36:06

标签: java static libgdx singleton final

我有一个看起来像这样的课程:

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;
    }
}

大小是一个简单的类,只有公共宽度和高度浮动。

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

为什么呢?我尝试在一个更简单的非libGDX类中复制问题,但不能。我想也许静态的最终Size类在用于创建textureRegion之前并没有实际实例化,但是调试器和我试图简化它似乎都反驳了它。

如上所述,我是一个C家伙,这样的事情的顺序非常明确。这里对我来说很混乱,所以我可以使用任何关于如何正确声明#define等价物或可能与答案一起出现的单例类的教育。感谢。

1 个答案:

答案 0 :(得分:3)

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

对于您的用例,最简单的修复方法是在Size类中使用ints而不是float。