下面是我得到的控制台错误:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.dakotapederson.slingshotsteve.SlingshotSteve.render(SlingshotSteve.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
以下是代码:
package com.dakotapederson.slingshotsteve;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class SlingshotSteve implements ApplicationListener {
// Creates our 2D images
private SpriteBatch batch;
private TextureRegion backgroundTexture;
private Texture texture;
@Override
public void create() {
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 20, 20, 50, 50);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
}
控制台错误究竟意味着什么?因为如果他们在代码中出错,我需要知道它是什么。此外,由于我是新手,请尽可能清晰地描述代码中的错误。
答案 0 :(得分:0)
您应该阅读基本教程:https://github.com/libgdx/libgdx/wiki/A-simple-game
你在create()中缺少这一行:
batch = new SpriteBatch();