Libgdx空指针2d跳转并运行游戏

时间:2015-11-25 14:23:26

标签: nullpointerexception libgdx box2d

我想在我的2d跳跃和运行游戏中实现我的玩家可以拍摄但我总是得到空指针异常。 有谁知道为什么?

在本课程中,我检查输入:     公共类InputIngame实现InputProcessor {

Player player;

public void handleInput(){
    //control our player using immediate impulses
        if (Gdx.input.isKeyJustPressed(Input.Keys.W) && PlayScreen.player.b2body.getLinearVelocity().y == 0)
            PlayScreen.player.b2body.applyLinearImpulse(new Vector2(0, 6f), PlayScreen.player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.D) && PlayScreen.player.b2body.getLinearVelocity().x <= 2)
            PlayScreen.player.b2body.applyLinearImpulse(new Vector2(0.2f, 0), PlayScreen.player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.A) && PlayScreen.player.b2body.getLinearVelocity().x >= -2)
            PlayScreen.player.b2body.applyLinearImpulse(new Vector2(-0.2f, 0), PlayScreen.player.b2body.getWorldCenter(), true);
        if (Gdx.input.isKeyPressed(Input.Keys.D) && Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT) && PlayScreen.player.b2body.getLinearVelocity().x > 1)
            PlayScreen.player.b2body.setLinearVelocity(1, PlayScreen.player.b2body.getLinearVelocity().y);
        if (Gdx.input.isKeyPressed(Input.Keys.A) && Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT) && PlayScreen.player.b2body.getLinearVelocity().x < -1)
            PlayScreen.player.b2body.setLinearVelocity(-1, PlayScreen.player.b2body.getLinearVelocity().y);
        if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE))
            player = new Player();
            player.fire();
}

在我的屏幕课上我画了火球:

@Override
public void render(float delta) {
    //separate our update logic from render
    update(delta);

    //Clear the game screen with Black
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //render our game map
    renderer.render();
    if (player.isDead == true)
        player.die();
    player.draw(runner.batch);

    //renderer our Box2DDebugLines
    b2dr.render(world, gamecam.combined);

    runner.batch = new SpriteBatch();
    runner.batch.setProjectionMatrix(gamecam.combined);
    runner.batch.begin();
    player.draw(runner.batch);
    runner.batch.end();

    if (TimeUtils.timeSinceNanos(startTime) > 1000000000) {
        startTime = TimeUtils.nanoTime();
    }

    Gdx.app.log("FPSLogger", "fps: " + Gdx.graphics.getFramesPerSecond());
}

在我的播放器类中,我设置了变量:

private PlayScreen screen;
private Array<FireBall> fireballs;

并启动他们:

public Player(PlayScreen screen){
    //initialize default values
    runner = new HardwareRunner();
    this.screen = screen;
    fireballs = new Array<FireBall>();

此方法在render方法中执行:

public void update(float dt){
    //update our sprite to correspond with the position of our Box2D body
    setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
    //update sprite with the correct frame depending on marios current action
    setRegion(getFrame(dt));

    for(FireBall  ball : fireballs) {
        ball.update(dt);
    }

}

还有这些方法:

public void fire(){
    fireballs.add(new FireBall(screen, b2body.getPosition().x, b2body.getPosition().y, runningRight ? true : false));
}

public void draw(Batch batch){
    super.draw(batch);
    for(FireBall ball : fireballs)
        ball.draw(batch);

}

然后是我的火球课:

public class FireBall extends Sprite {

PlayScreen screen;
World world;
Array<TextureRegion> frames;
Animation fireAnimation;
float stateTime;
boolean destroyed;
boolean setToDestroy;
boolean fireRight;

Body b2body;
public FireBall(PlayScreen screen, float x, float y, boolean fireRight){
    this.fireRight = fireRight;
    this.screen = screen;
    this.world = screen.getWorld();
    frames = new Array<TextureRegion>();
    for(int i = 0; i < 4; i++){
        frames.add(new TextureRegion(screen.getAtlas().findRegion("fireball"), i * 8, 0, 8, 8));
    }
    fireAnimation = new Animation(0.2f, frames);
    setRegion(fireAnimation.getKeyFrame(0));
    setBounds(x, y, 6 / HardwareRunner.PPM, 6 / HardwareRunner.PPM);
    defineFireBall();
}

public void defineFireBall(){
    BodyDef bdef = new BodyDef();
    bdef.position.set(fireRight ? getX() + 12 /HardwareRunner.PPM : getX() - 12 /HardwareRunner.PPM, getY());
    bdef.type = BodyDef.BodyType.DynamicBody;
    if(!world.isLocked())
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(3 / HardwareRunner.PPM);
    fdef.filter.categoryBits = HardwareRunner.PROJECTILE_BIT;
    fdef.filter.maskBits = HardwareRunner.GROUND_BIT |
            HardwareRunner.BRICK_BIT |
            HardwareRunner.OBJECT_BIT;

    fdef.shape = shape;
    fdef.restitution = 1;
    fdef.friction = 0;
    b2body.createFixture(fdef).setUserData(this);
    b2body.setLinearVelocity(new Vector2(fireRight ? 2 : -2, 2.5f));
}

public void update(float dt){
    stateTime += dt;
    setRegion(fireAnimation.getKeyFrame(stateTime, true));
    setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
    if((stateTime > 3 || setToDestroy) && !destroyed) {
        world.destroyBody(b2body);
        destroyed = true;
    }
    if(b2body.getLinearVelocity().y > 2f)
        b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f);
    if((fireRight && b2body.getLinearVelocity().x < 0) || (!fireRight && b2body.getLinearVelocity().x > 0))
        setToDestroy();
}

public void setToDestroy(){
    setToDestroy = true;
}

public boolean isDestroyed(){
    return destroyed;
}

}

但如果我尝试开始游戏,我会收到此错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at de.tobls.hardwarerunner.Input.InputIngame.handleInput(InputIngame.java:30)
at de.tobls.hardwarerunner.Screens.PlayScreen.update(PlayScreen.java:109)
at de.tobls.hardwarerunner.Screens.PlayScreen.render(PlayScreen.java:130)
at com.badlogic.gdx.Game.render(Game.java:46)
at de.tobls.hardwarerunner.HardwareRunner.render(HardwareRunner.java:71)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

line 30 is player.fire();
line 109 is input.handleInput();
line 130 is update(delta);
and line 71 is update(delta); in my main class

我希望有人能帮助我!

1 个答案:

答案 0 :(得分:1)

我认为你的问题是忘记了#34; 如果&#34;条件试试这个:

if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
     player = new Player();
     player.fire();
}
祝你好运;

如果有任何问题留下评论;)