2

I'm working on game, using LibGDX and have some problems with Input Processor. Wnen I launch my game and press Right button it crashes with NullPointerException

@Override
public boolean keyDown(int keycode) {
    if (keycode == Input.Keys.RIGHT && player.b2body.getLinearVelocity().x <=0.5) {
        player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(),true);
        return true;
    }
    return false;
}



public  void update (float dt) {


        world.step(1/10f,6,2);
        player.update(dt);
       // handleInput(dt);

        MyInputProcessor inputProcessor = new MyInputProcessor();
        Gdx.input.setInputProcessor(inputProcessor);
        if(player.b2body.getPosition().x>=SonicZP.V_WIDTH/2/SonicZP.PPM)
        gamecam.position.x = player.b2body.getPosition().x;

        if(player.b2body.getPosition().y>=SonicZP.V_HEIGHT/2/SonicZP.PPM)
            gamecam.position.y = player.b2body.getPosition().y;
        gamecam.update();



            renderer.setView(gamecam);
    }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

2 Answers2

2
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);

Should be placed in your show()-method in your Game class that extends Screen.

mtosch
  • 351
  • 4
  • 18
  • The keyDown() method above the render() implies that he is implementing InputProcessor. He should therefore not instantiate a new InputProcessor, but use the class itself as InputProcessor – Jim Sep 21 '16 at 10:36
0

It looks like your class extends Screen and implements InputProcessor.

You should remove

MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);

from you render(). Btw, this should NEVER be in render(). Instead in your show() set:

Gdx.input.setInputProcessor(this);

since the class you are in implements InputProcessor and IS your InputProcessor.

Jim
  • 995
  • 2
  • 11
  • 28