LibGDX - TexureRegion中的多个控制台错误

时间:2014-08-26 03:25:38

标签: java libgdx

这是代码的主要部分。我想要做的就是得到一个简单的图像作为游戏的背景显示,经过10个小时的研究后,我仍然无法让它得到它的工作。

public class SlingshotSteve implements ApplicationListener {
   // Creates our 2D images
   private SpriteBatch batch;
   private TextureRegion backgroundTexture;


@Override
public void create() {


    new Texture(Gdx.files.internal("background.jpg"));
    backgroundTexture = new TextureRegion(backgroundTexture, 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();

}

下面是我收到的控制台错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:118)
at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java:70)
at com.dakotapederson.slingshotsteve.SlingshotSteve.create(SlingshotSteve.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

我是新手,而且我这么慢,所以请尽可能清楚地解释这个问题。

1 个答案:

答案 0 :(得分:0)

你错过了什么,这就是你的代码应该是这样的:

Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 20, 20, 50, 50);