Updating Input Processors

Now remember we wrote a method in our LevelOne class to do the input handling?  That was a temporary solution, because it’s not very good practice to have input handling in our screen class.  LibGDX has a special interface called an InputProcessor, and to use it we can simply create a new class to use it.  Since it’s an interface, remember to use the implement keyword rather than the extend keyword.

public class InputListener implements InputProcessor
{
}

Since we would want to communicate with our player class somehow, we need to make sure to pass the Player object through the constructor like so:

private Player player;

public InputListener(Player player)
    {
        this.player = player;
    }

Afterwards, use the IDE to auto generate all the required methods.  The main method we’ll be looking at is the keyDown(int keycode) method.  This method is automatically called when a button is pressed, and the argument that’s passed through whenever the method is called, the int keycode, is the key that the user pressed.  And so we can do something like if the keycode equals to the left key, then move left:

if(keycode == Input.Keys.UP) //if up button pressed
    {
        player.setMovement("up") //then move up
    }

Unlike our handleInput() method that we made in our LevelOne class, forces can’t be implemented here bcs it’ll only apply for when key is first pressed down, and therefore we have to apply force in main loop instead, whenever the render method is called.  A workaround for this is to create a method that sets the movement of the player in the Player class, and then have another new method be called in the update method to actually move the player.

To set the movement of the player, we can simply create a new variable like so:

private String movement;

public void setMovement(String movement)
{
    this.movement = movement;
}

Then have another method that will process the value of the movement variable and apply the forces to move the player:

public void changeMovement()
{
    if(movement.equals(Constants.UP)) //if up button pressed
    {
        this.box2Body.applyLinearImpulse(new Vector2(0, 15f * this.box2Body.getMass()), 
        this.box2Body.getWorldCenter(), true);
    }
    if(movement.equals(Constants.RIGHT) && this.box2Body.getLinearVelocity().x <= 75f) //if right 
    key is being pressed
    {
        this.box2Body.applyLinearImpulse(new Vector2(50f * this.box2Body.getMass(), 0),
        this.box2Body.getWorldCenter(), true);
    }
    if(movement.equals(Constants.LEFT) && this.box2Body.getLinearVelocity().x >= -75f) //if left 
    key is being pressed
    {
        this.box2Body.applyLinearImpulse(new Vector2(-50f * this.box2Body.getMass(), 0), 
        this.box2Body.getWorldCenter(), true);
    }
}

Then remember to call the changeMovement() method in your Player class’s update method:

public void update(float dt)
{
    //existing code
    changeMovement()
}

Now remember when we also made our HUD, we also used an input processor for our stage to listen for button presses onto the button.  And because we now that we have more than one input processor, we’ll have to use something called an InputMultiplexer, which will basically help you handle the usage of different input processors. We can set that up in our LevelOne class like so:

InputProcessor processor = new InputListener(player);
InputMultiplexer inputMultiplexer = new InputMultiplexer();
inputMultiplexer.addProcessor(processor);
inputMultiplexer.addProcessor(hud.getStage());
Gdx.input.setInputProcessor(inputMultiplexer);

Video Lesson