1

我正在尝试在屏幕上绘制 .png,但我不断收到错误消息。我正在使用的类是 Screen 的子类。

这是我得到的错误:

FATAL EXCEPTION GLThread 1335 java.lang.NullPointerException  at SplashScreen.java.32

这一行是:

batch.begin()

这是我在 Screen 子类中的代码:

private SpriteBatch batch;
private Texture splashTexture;
private Camera camera;


 final int CAMERA_WIDTH = Gdx.graphics.getWidth();
static final int CAMERA_HEIGHT = Gdx.graphics.getHeight();



@Override
public void render(float delta) {
   Gdx.gl.glClearColor(0, 0, 0, 1);
   Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
   batch.begin();
   batch.draw(splashTexture, 0, 0);
   batch.end();

}

@Override
public void resize(int width, int height) {
   // TODO Auto-generated method stub

}

@Override
public void show() {
   splashTexture = new Texture(Gdx.files.internal("splash.png"));
   camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    camera.position.set(CAMERA_WIDTH, CAMERA_HEIGHT, 0);
}
4

2 回答 2

1

您的 SpriteBatch 为空,您创建了纹理和相机,但没有创建批次。你提到的断点与它无关。

private SpriteBatch batch;
private Texture splashTexture;
private Camera camera;

...

@Override
public void show(){
    splashTexture = new Texture(Gdx.files.internal("splash.png"));
    camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
    camera.position.set(CAMERA_WIDTH, CAMERA_HEIGHT, 0);

    batch = new SpriteBatch(); //create it like this.
}

...
于 2014-01-10T08:07:47.203 回答
0
batch=new spritebatch();

你的问题就解决了。

于 2014-01-10T13:32:29.457 回答