In this video tutorial, we look at handling Keyboard, Mouse and Touch input in LibGDX. We look at both handling input via polling as well as an event driven approach.
You can see the video in full 1080p definition here. Once again, all the code included in the video is available below:
Polled Input Sample
package com.gamefromscratch; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class PolledInputDemo extends ApplicationAdapter { SpriteBatch batch; Texture img; Sprite sprite; @Override public void create () { batch = new SpriteBatch(); img = new Texture("badlogic.jpg"); sprite = new Sprite(img); sprite.setPosition(Gdx.graphics.getWidth()/2 - sprite.getWidth()/2, Gdx.graphics.getHeight()/2 - sprite.getHeight()/2); } @Override public void render () { // Keyboard events if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) sprite.translateX(-1f); if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) sprite.translateX(1f); if(Gdx.input.isKeyPressed(Input.Keys.SPACE)) sprite.setPosition(Gdx.graphics.getWidth()/2 - sprite.getWidth()/2, Gdx.graphics.getHeight()/2 - sprite.getHeight()/2); if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) sprite.setPosition(Gdx.input.getX(),Gdx.graphics.getHeight() - Gdx.input.getY()); Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(sprite, sprite.getX(), sprite.getY()); batch.end(); } @Override public void dispose(){ img.dispose(); } }
Event Driven Input Sample
package com.gamefromscratch; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class EventDrivenInputDemo extends ApplicationAdapter implements InputProcessor { SpriteBatch batch; Texture img; Sprite sprite; boolean movingRight = false; @Override public void create () { batch = new SpriteBatch(); img = new Texture("badlogic.jpg"); sprite = new Sprite(img); sprite.setPosition(Gdx.graphics.getWidth()/2-sprite.getWidth()/2, Gdx.graphics.getHeight()/2 - sprite.getHeight()/2); Gdx.input.setInputProcessor(this); } @Override public void render () { if(movingRight) sprite.translateX(1f); Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(sprite, sprite.getX(),sprite.getY()); batch.end(); } @Override public boolean keyDown(int keycode) { if(keycode == Input.Keys.RIGHT) movingRight = true; return true; } @Override public boolean keyUp(int keycode) { if(keycode == Input.Keys.LEFT) sprite.translateX(-1f); if(keycode == Input.Keys.RIGHT) movingRight = false; return true; } @Override public boolean keyTyped(char character) { return false; } @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { // If the user is holding down ( or was holding down, and hasnt released ) three fingers, move the sprite if(pointer ==2) sprite.setPosition(screenX,Gdx.graphics.getHeight()-screenY); return true; } @Override public boolean mouseMoved(int screenX, int screenY) { sprite.setPosition(screenX,Gdx.graphics.getHeight()-screenY); return true; } @Override public boolean scrolled(int amount) { if(amount > 0) sprite.translateY(1f); if(amount < 0) sprite.translateY(-1f); return true; } }