LibGDX exception: Exception in thread "LWJGL Application" java.lang.NullPointerException



我的代码以前工作得很好,但现在当我启动应用程序时,我得到一个空白屏幕和一个错误。

"线程"LWJGL Application"中的异常com.mygdx.game.Bunny.render (Bunny.java: 70)com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop (LwjglApplication.java: 215)在com.badlogic.gdx.backends.lwjgl.LwjglApplication 1.美元运行(LwjglApplication.java: 120)"

指向第70行:

batch.draw(paddle, paddleRect.x, paddleRect.y);

我的完整代码是:

package com.mygdx.game;
import java.util.Iterator;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
public class Bunny extends ApplicationAdapter {
    SpriteBatch batch;
    Texture paddle;
    Texture blockImg;
    Rectangle paddleRect;
    float speed = 300.0f;
    private OrthographicCamera camera;
    private Array<Rectangle> blocks;
    private void spawnBlock(int x, int y){
        Rectangle block = new Rectangle();
        block.x = x*90;
        block.y = 400-(y*30);
        block.width = 30;
        block.height = 10;
        blocks.add(block);
    }
    @Override
    public void create () {
        Rectangle paddleRect = new Rectangle();
        paddleRect.x = 0;
        paddleRect.y = 0;
        paddleRect.width = 60;
        paddleRect.height = 20;
        batch = new SpriteBatch();
        paddle = new Texture("paddle.png");
        blockImg = new Texture("block.png");
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 400, 400);
        blocks = new Array<Rectangle>();
        for(int x = 0; x < 5; x++){
            for(int y = 0; y < 4; y++){
                spawnBlock(x, y);
            }
        }
    }
    @Override
    public void render () {
        Gdx.gl.glClearColor(0.5f, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(paddle, paddleRect.x, paddleRect.y);
        for(Rectangle block: blocks){
            batch.draw(blockImg, block.x, block.y);
        }
        batch.end();
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
            paddleRect.x += speed * Gdx.graphics.getDeltaTime();
        else if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
            paddleRect.x -= speed * Gdx.graphics.getDeltaTime();
        if(Gdx.input.isKeyPressed(Input.Keys.UP))
            paddleRect.y += speed * Gdx.graphics.getDeltaTime();
        if(paddleRect.x < 0f)
            paddleRect.x = 0f;
        if(paddleRect.x > 340f)
            paddleRect.x = 340f;
        Iterator<Rectangle> iter = blocks.iterator();
        while(iter.hasNext()){
            Rectangle block = iter.next();
            if(block.overlaps(paddleRect))
                iter.remove();
        }
    }
}

create中,您想为您的类字段paddleRect分配一个新的Rectangle,但相反,您创建了一个名为paddleRect的方法变量,并且您的类字段仍然是null

试题:

public void create () {
        paddleRect = new Rectangle();
        paddleRect.x = 0;
        paddleRect.y = 0;
        ...

相关内容

最新更新