libGDX字符nullpointer

时间:2014-05-26 00:44:41

标签: java libgdx

我是编程的初学者,我正在使用libGDX制作RPG。我设置了一些东西,出于某种原因,当我到达应该显示主角的屏幕时,我收到以下错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at FirstRPG.AbstractEntity.setLinearVelocity(AbstractEntity.java:220)
    at FirstRPG.screens.Player.handleEvents(Player.java:138)
    at FirstRPG.screens.Player.draw(Player.java:193)
    at FirstRPG.screens.GameScreen.render(GameScreen.java:16)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at FirstRPG.screens.FirstRPG.render(FirstRPG.java:20)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

我设置了一个抽象类来组合scence2d和box2d,错误与setLinearVelocity方法有关:

public void setLinearVelocity(Vector2 v) {
        body.setLinearVelocity(v);
}

用于我在Player的类中设置的事件处理程序:

public void handleEvents() {
    this.move = Movement.IDLE;
    if ((Gdx.input.isKeyPressed(Input.Keys.UP)) || (Gdx.input.isKeyPressed(Input.Keys.W))) {
        setLinearVelocity(0.0f, 5.0f);
        this.setY(this.getPosition().y * PIXELS_PER_METER);
        this.dir = Direction.UP;
        this.move = Movement.WALKING;
    } else if ((Gdx.input.isKeyPressed(Input.Keys.DOWN)) || (Gdx.input.isKeyPressed(Input.Keys.S))) {
        setLinearVelocity(0.0f, -5.0f);
        this.setY(this.getPosition().y * PIXELS_PER_METER);
        this.dir = Direction.DOWN;
        this.move = Movement.WALKING;
    } else if ((Gdx.input.isKeyPressed(Input.Keys.LEFT)) || (Gdx.input.isKeyPressed(Input.Keys.A))) {
        setLinearVelocity(-5.0f, 0.0f);
        this.setX(this.getPosition().x * PIXELS_PER_METER);
        this.dir = Direction.LEFT;
        this.move = Movement.WALKING;
    } else if ((Gdx.input.isKeyPressed(Input.Keys.RIGHT)) || (Gdx.input.isKeyPressed(Input.Keys.D))) {
        setLinearVelocity(5.0f, 0.0f);
        this.setX(this.getPosition().x * PIXELS_PER_METER);
        this.dir = Direction.RIGHT;
        this.move = Movement.WALKING;
    } else {
        setLinearVelocity(0.0f, 0.0f);
        this.setX(this.getPosition().x * PIXELS_PER_METER);
        this.setY(this.getPosition().y * PIXELS_PER_METER);
        this.move = Movement.IDLE;
    }
}

我之前使用过这种制作角色的方式,我不明白为什么它会给我一个NullPointerException

1 个答案:

答案 0 :(得分:0)

不应该是setLinearVelocity(new Vector2(0.0f, 5.0f));

否则从您的代码中看起来就像是在向它传递两个浮点数而不是向量。