android - How to implement libgdx touchDragged? -


i'm stuck on implementing touchdragged listener using libgdx.

here code , can suggest how drag image when user touches , moves finger?

i use stage , actor , want catch touchdragged event on actor.

thanks.

public void create () {     texture.setenforcepotimages(false);      stage = new stage();     gdx.input.setinputprocessor(stage);     // create spritebatch render sprite     batch = new spritebatch();      // load sprite's texture. note: have more     // 1 sprite in texture, see {@see textureatlas} , {@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 {@link orthographiccamera} used transform     // touch coordinates world coordinates.     camera = new orthographiccamera();      // want camera setup viewport pixels units,     // y-axis pointing upwards. origin in lower left corner     // of 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 finger down, set sprite's x/y coordinate.     if (gdx.input.istouched()) {         // unproject method takes vector3 in window coordinates (origin in         // upper left corner, y-axis pointing down) , transforms world         // coordinates.         camera.unproject(spriteposition.set(gdx.input.getx(), gdx.input.gety(), 0));     } } 

here steps:

  1. you need check if finger touching object want move. here useful methods finger's position:

    gdx.input.getx(); gdx.input.gety(); 
  2. you need use variable track whether finger moving, when touching.

  3. if variable true, change object's position finger's position.

  4. you disable variable when finger no longer touching screen.


Comments