Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm a beginner in Java and in Game Development too. I was following some tutorials, but now I'm stuck on implementing a touchDragged listener using libgdx,

Can anyone suggest how to drag the image when the user touches it and then move with the users finger position?

I am using both a stage and an actor. I want to catch a touchDragged event on an actor.


public void create () {

    stage = new Stage();
    // create a SpriteBatch with which to render the sprite
    batch = new SpriteBatch();

    // load the sprite's texture. note: usually you have more than
    // one sprite in a texture, see {@see TextureAtlas} and {@see TextureRegion}.
    texture = new Texture(Gdx.files.internal("ball3.png"));
    Skin skin = new Skin();
    skin.add("default", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    skin.add("ball", texture);
    Image sourceImage = new Image(skin, "ball");
    sourceImage.setBounds(50, 125, 100, 100);

    // create an {@link OrthographicCamera} which is used to transform
    // touch coordinates to world coordinates.
    camera = new OrthographicCamera();

    // we want the camera to setup a viewport with pixels as units, with the
    // y-axis pointing upwards. The origin will be in the lower left corner
    // of the screen.

public void render () {

    // if a finger is down, set the sprite's x/y coordinate.
    if (Gdx.input.isTouched()) {
        // the unproject method takes a Vector3 in window coordinates (origin in
        // upper left corner, y-axis pointing down) and transforms it to world
        // coordinates.
        camera.unproject(spritePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0));
+1 for your comment about unproject function :D,I suddenly found exactly what I was looking for. –  Saleh Feb 3 '14 at 3:17

1 Answer 1

up vote 0 down vote accepted

You have to implement InputProcessor where you can then handle the input.

Then in your main class in create() method you have to pass your InputProcessor to Gdx like this:

public void create() {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        camera = new OrthographicCamera(w,h);
        batch = new SpriteBatch();

        Gdx.input.setInputProcessor(new MyInputPocessor()); // THIS PART HERE

InputProcessor gives you touchDragged(int x, int y, int pointer) like this:

   public boolean touchDragged (int x, int y, int pointer) {
      // Move your image here.
      return false;

also you can use other methods of InputProcessor like touchDown(), for example to check if user touched the image and not outside of it. And touchUp() for example to store the position when finger/mouse left the screen.

Close but since the OP is using a Stage and Actors he should pass his stage to the setInputProcessor method and on any Actor he could call addListener too add an EventListener subclass. –  Chase Feb 21 '14 at 6:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.