更新:解决方法在此帖子的底部。发生SpriteBatch.end()时发生java.lang.NullPointerException
我遇到了一个问题,当我的游戏运行时,屏幕只是黑色,几秒钟后坠毁。我在我的主Game
类声明一个新SpriteBatch:
public SpriteBatch batch;
而且我实例在我的创建方法()和整个Game
类传递给一个叫播放屏幕Screen
:
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
在构造函数中播放屏幕的,我传递的Game
类设置为私有字段我设置:
this.game = game;
这是我的整个渲染方法,用于播放屏幕:
@Override
public void render(float delta)
{
update(delta);
Gdx.gl.glClearColor(0, 2, 2, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
// Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
game.batch.begin();
for (Tile[] tileArray : tileMatrix.getTileMatrix())
{
for (Tile tile : tileArray)
{
tile.draw(game.batch);
}
}
game.batch.end();
hud.stage.draw();
}
tileArray
是包含的二维阵列。在我的create()方法中,此数组中的每个图块都被初始化并分配了不同的位置。这是基础类:
public abstract class Tile extends Sprite
{
protected Texture texture;
protected String name;
protected int movementPoints;
protected int foodYield;
protected int productionYield;
protected int tradeYield;
public Tile(int mP, int fY, int pY, int tY, String name, Texture texture)
{
this.movementPoints = mP;
this.foodYield = fY;
this.productionYield = pY;
this.tradeYield = tY;
this.name = name;
this.texture = texture;
setBounds(0, 0, 50, 50);
}
当DesktopLauncher.java运行时,我得到这个错误:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.SpriteBatch.flush(SpriteBatch.java:962)
at com.badlogic.gdx.graphics.g2d.SpriteBatch.end(SpriteBatch.java:183)
at com.marcusorciuch.reciv.screens.PlayScreen.render(PlayScreen.java:108)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.marcusorciuch.reciv.ReCivMain.render(ReCivMain.java:47)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
@
game.batch.end();
是啊,我不知道怎么回事。
编辑:那些谁是有这个问题,请确保您有从Sprite
扩展一个类时,请确保您拨打super(texture)
否则Sprite
零件类的不会被实例化。
的'texture'在所有瓷砖似乎是空。 – Tenfour04
@ Tenfour04 http://pastebin.com/eteh7Vrn我已经在一个子类中实例化了Tiles中的纹理。 –
为什么你不想在'PlayScreen'类中直接创建'SpriteBatch'作为私人字段? – Enigo