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.

Thanks.

public void create () {
    Texture.setEnforcePotImages(false);

    stage = new Stage();
    Gdx.input.setInputProcessor(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);
    stage.addActor(sourceImage);

    // 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.
    camera.setToOrtho(false);
}

public void render () {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
    Table.drawDebug(stage);



    // 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));
    }
}
share|improve this question
    
+1 for your comment about unproject function :D,I suddenly found exactly what I was looking for. –  Saleh Feb 3 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:

@Override
   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.

share|improve this answer
    
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 at 6:15

Your Answer

 
discard

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.