i have just started using java and libgdx and have this code, very simply it prints a sprite onto the screen. This works perfectly, and i learnt a lot from it.
You can use the interface KeyListener
to detect keyboard action.
public class Game implements ApplicationListener, KeyListener {
@Override
public void create() {
//Important
this.addKeyListener(this);
// TODO Auto-generated method stub
batch = new SpriteBatch();
FileHandle marioFileHandle = Gdx.files.internal("mario.png");
marioTexture = new Texture(marioFileHandle);
mario = new Sprite(marioTexture, 0, 158, 32, 64);
marioX = 0;
marioY = 0;
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 68) { //it's the 'D' key
//Move your mario
}
}
}