O'Reilly logo

LibGDX Cross-Platform Development Blueprints by Indraneel Potnis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Moving the player

This section handles the movement of the player, Bob, for both desktop and mobile platforms.

Bob's movement on desktop

We are going to make Bob move by taking an input from the keyboard. First, let's add a method to the Bob class called move():

// move bob's with the specified amount
public void move (float x, float y){
    setPosition(bobSprite.getX()+x,bobSprite.getY()+y);
}

This method takes the distance Bob needs to move in the x and y directions and updates his position by that. Let's add a new class to handle the input. Create a class named InputManager in the com.packtpub.dungeonbob.managers package and type the following code:

package com.packtpub.dungeonbob.managers; import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.Input.Keys; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required