I'm developing a game using libGDX and I would like to know how I can drag and drop an Actor. I've made my stage and drawn the actor, but I don't know how to trigger that event.
Please try to help me using my own architecture.
public class MyGame implements ApplicationListener
{
Stage stage;
Texture texture;
Image actor;
@Override
public void create()
{
texture = new Texture(Gdx.files.internal("actor.png"));
Gdx.input.setInputProcessor(stage);
stage = new Stage(512f,512f,true);
actor = new Image(texture);
stage.addActor(actor);
}
@Override
public void render()
{
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.draw();
}
}
For moving an actor to a specific location you can use MoveToAction. With this you can set the final location and also how long it it takes to move to that location.
Take a look at the Example in the libgdx examples. Here is the drag and drop test from the libgdx test classes: DragAndDropTest
If you just want to drag/slide your Actor around you need to add a GestureListener to it and pass your Stage to the Inputprocessor like this:Gdx.input.setInputProcessor(stage);
.
Here is the GestureDetectorTest from libgdx.
For drag events its the Flinglistener.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With