Advertisement
dermetfan

touchpad example

Dec 5th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package com.me.mygdxgame.screens;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.InputMultiplexer;
  5. import com.badlogic.gdx.Screen;
  6. import com.badlogic.gdx.graphics.GL20;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.Texture;
  9. import com.badlogic.gdx.graphics.g2d.Animation;
  10. import com.badlogic.gdx.graphics.g2d.Sprite;
  11. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  12. import com.badlogic.gdx.maps.tiled.TiledMap;
  13. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  14. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  15. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  16. import com.badlogic.gdx.scenes.scene2d.Actor;
  17. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  18. import com.badlogic.gdx.scenes.scene2d.Stage;
  19. import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
  20. import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
  21. import com.badlogic.gdx.scenes.scene2d.ui.Skin;
  22. import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
  23. import com.badlogic.gdx.scenes.scene2d.ui.Touchpad.TouchpadStyle;
  24. import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
  25. import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
  26. import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
  27. import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
  28. import com.badlogic.gdx.utils.Array;
  29. import com.me.mygdxgame.entities.Player;
  30.  
  31. public class Play implements Screen {
  32.  
  33.     private Stage stage;
  34.  
  35.     @Override
  36.     public void show() {
  37.         stage = new Stage();
  38.         Gdx.input.setInputProcessor(new InputMultiplexer(stage, player));
  39.  
  40.         // touchpad krams
  41.         Skin touchpadSkin = new Skin();
  42.         touchpadSkin.add("touchBackground", new Texture("img/touchBackground.png"));
  43.         touchpadSkin.add("touchKnob", new Texture("img/touchKnob.png"));
  44.  
  45.         TouchpadStyle touchpadStyle = new TouchpadStyle();
  46.         Drawable touchBackground = touchpadSkin.getDrawable("touchBackground");
  47.         Drawable touchKnob = touchpadSkin.getDrawable("touchKnob");
  48.         touchpadStyle.background = touchBackground;
  49.         touchpadStyle.knob = touchKnob;
  50.  
  51.         Touchpad touchpad = new Touchpad(10, touchpadStyle);
  52.         touchpad.setBounds(15, 15, 200, 200);
  53.         touchpad.addListener(new ChangeListener() {
  54.  
  55.             @Override
  56.             public void changed(ChangeEvent event, Actor actor) {
  57.                 Touchpad tpad = (Touchpad) actor;
  58.                 player.getVelocity().x = tpad.getKnobPercentX() * player.getSpeed();
  59.                 player.getVelocity().y = tpad.getKnobPercentY() * player.getSpeed();
  60.             }
  61.  
  62.         });
  63.  
  64.         // add to stage
  65.         stage.addActor(touchpad);
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement