我有一个看起来像这样的类(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;
}
}
Size 是一个简单的类,只有公共(public)宽度和高度 float 。
我想将textureRegion设置的行替换为它下面的行,该行已被注释掉。当我这样做时,该区域不再起作用——当它在图像中绘制时我什么也得不到。上面的代码行对于我的 C 编程(即预处理器)大脑来说应该是完全相同的,工作得很好。
为什么?我尝试在一个更简单的非 libGDX 类中复制该问题,但失败了。我认为静态最终 Size 类在用于创建纹理区域之前实际上并未被实例化,但调试器和我简化它的尝试似乎都证明了这一点。如上所述,我是一个 C 人,这样的事情的顺序非常清楚。这里对我来说很困惑,所以我可以使用任何关于如何正确声明 #define 等价物或可能伴随答案的单例类的教育。谢谢。
最佳答案
您提到您的 Size 类有两个用于宽度和高度的 float 字段。当您使用 Size 类实例化 TextureRegion 时,由于方法重载,它实际上调用了一个单独的构造函数,而不是传入整数常量。该单独的构造函数需要标准化为 0 - 1.0 范围的 UV 坐标。检查TextureRegion API和 Textures section of open.gl更多细节。
针对您的用例的最简单的解决方法是在 Size 类中使用整数而不是 float 。
关于java - LibGDX - 具有静态final的单例类会导致TextureRegion失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981797/