LibGDX has an inbuilt way of creating and skinning on screen TouchPads.
Below is an example on how to create, skin, draw and move a sprite based on TouchPad input.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
public class TouchPadTest implements ApplicationListener { private OrthographicCamera camera; private Stage stage; private SpriteBatch batch; private Touchpad touchpad; private TouchpadStyle touchpadStyle; private Skin touchpadSkin; private Drawable touchBackground; private Drawable touchKnob; private Texture blockTexture; private Sprite blockSprite; private float blockSpeed; @Override public void create() { batch = new SpriteBatch(); //Create camera float aspectRatio = (float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight(); camera = new OrthographicCamera(); camera.setToOrtho(false, 10f*aspectRatio, 10f); //Create a touchpad skin touchpadSkin = new Skin(); //Set background image touchpadSkin.add("touchBackground", new Texture("data/touchBackground.png")); //Set knob image touchpadSkin.add("touchKnob", new Texture("data/touchKnob.png")); //Create TouchPad Style touchpadStyle = new TouchpadStyle(); //Create Drawable's from TouchPad skin touchBackground = touchpadSkin.getDrawable("touchBackground"); touchKnob = touchpadSkin.getDrawable("touchKnob"); //Apply the Drawables to the TouchPad Style touchpadStyle.background = touchBackground; touchpadStyle.knob = touchKnob; //Create new TouchPad with the created style touchpad = new Touchpad(10, touchpadStyle); //setBounds(x,y,width,height) touchpad.setBounds(15, 15, 200, 200); //Create a Stage and add TouchPad stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true, batch); stage.addActor(touchpad); Gdx.input.setInputProcessor(stage); //Create block sprite blockTexture = new Texture(Gdx.files.internal("data/block.png")); blockSprite = new Sprite(blockTexture); //Set position to centre of the screen blockSprite.setPosition(Gdx.graphics.getWidth()/2-blockSprite.getWidth()/2, Gdx.graphics.getHeight()/2-blockSprite.getHeight()/2); blockSpeed = 5; } @Override public void dispose() { } @Override public void render() { Gdx.gl.glClearColor(0.294f, 0.294f, 0.294f, 1f); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); camera.update(); //Move blockSprite with TouchPad blockSprite.setX(blockSprite.getX() + touchpad.getKnobPercentX()*blockSpeed); blockSprite.setY(blockSprite.getY() + touchpad.getKnobPercentY()*blockSpeed); //Draw batch.begin(); blockSprite.draw(batch); batch.end(); stage.act(Gdx.graphics.getDeltaTime()); stage.draw(); } @Override public void pause() { } @Override public void resume() { } @Override public void resize(int width, int height) { } } |
The code for this can be found on GitHub @ https://github.com/biggz/TouchPadTest
And finally, here are the images to put in your project-android/assets/data folder
Where do I start?
This site is primarily for me to keep a track of thing’s I’ve learnt and a place to host my hobby coding projects.
I mainly create useful tools to use at work (as a Managed Service Provider) or extremely basic mobile games.