-1
我已經開始使用Lib GDX AssetManager,而不是「正常」的方式來導入紋理。但是,在嘗試使用它時,我很快遇到了NullPointerException錯誤。下面是代碼:LibGDX TextureRegion空指針異常
assets = Generals.getAssetManager();
assets.load("Characters/c.pack", TextureAtlas.class);
assets.finishLoading();
TextureAtlas atlas = assets.get("Characters/c.pack", TextureAtlas.class);
atlas.findRegion("PriestOverworld3");
Array<TextureRegion> frames = new Array<TextureRegion>();
Texture testtex = new Texture("Characters/c.png");
TextureRegion test = new TextureRegion(testtex);
for (int i = 0; i < 3; i++) {
System.out.println(i);
frames.add(new TextureRegion(getTexture(), i * 16, 0, 16, 16));
}
playerStasis = new Animation<>(0.2f, frames);
我在該行得到一個NullPointerException
frames.add(new TextureRegion(getTexture(), i * 16, 0, 16, 16));
錯誤:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:82)
at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java:53)
at Project.sprites.Player.<init>(Player.java:55)
TextureRegion:
// TextureRegion.java:53
setRegion(x, y, width, height);
...
// TextureRegion.java:82
float invTexWidth = 1f/texture.getWidth();
什麼呢'getTexture()'? –
getTexture()獲取TextureRegion的紋理。在這種情況下,「PriestOverworld3」是TextureRegion。 c.pack是TextureAtlas,它使用c.png。 c.png是使用的紋理。 – JasperMW