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 eve
If you don't want to use DragAndDrop
class, you can use this:
actor.addListener(new DragListener() {
public void drag(InputEvent event, float x, float y, int pointer) {
actor.moveBy(x - actor.getWidth() / 2, y - actor.getHeight() / 2);
}
});
Edit: method drag
instead touchDragged
In your main gamescreen class add a multiplexer so you can access events from different classes:
private InputMultiplexer inputMultiplexer = new InputMultiplexer(this);
After the gamescreen constructor add as an example:
inputMultiplexer = new InputMultiplexer(this);
inputMultiplexer.addProcessor(1, renderer3d.controller3d);
inputMultiplexer.addProcessor(2, renderer.controller2d);
inputMultiplexer.addProcessor(3, renderer3d.stage);
Gdx.input.setInputProcessor(inputMultiplexer);
In your class that is using the actors use a DragListener as and example:
Actor.addListener((new DragListener() {
public void touchDragged (InputEvent event, float x, float y, int pointer) {
// example code below for origin and position
Actor.setOrigin(Gdx.input.getX(), Gdx.input.getY());
Actor.setPosition(x, y);
System.out.println("touchdragged" + x + ", " + y);
}
}));
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.