更新:此帖子底部的解决方案。
我遇到的问题是,当我的游戏运行时,屏幕只是黑色并在几秒钟后崩溃。我在我的主Game
类中声明了一个新的SpriteBatch:
public SpriteBatch batch;
我在Create method()中实例化它并将整个Game
类传递给名为Playscreen的Screen
:
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
在PlayScreen的构造函数中,我将传递的Game
类设置为我设置的私有字段:
this.game = game;
这是我对PlayScreen的整个渲染方法:
@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
是一个包含Tiles
的二维数组。在我的create()方法中,此数组中的每个tile都被初始化并分配了不同的位置。这是基类:
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
1}}你的类的部分内容不会被实例化。
答案 0 :(得分:-1)
当您设置并传递Game
变量时,请使用final
final YourGame game; //final here
public PlayScreen(final YourGame game) { // final in the constructor declaration