Search code examples
drag-and-droplibgdx

Update position of Image after drag and drop


I´ve implemented a drag and drop function on some of my images. And it works great besides that the new position of the images isn´t saved and they end up in the bottom left corner each time I restart the app. This is how I´ve done:

public GameScreen()  {
stageImage = new Stage();
@Override
public void render(float delta) {
game.batch.setProjectionMatrix(camera.combined);

    camera.update();


    game.batch.begin();
flowerImage.setSize(300, 300);
stageMove.act(Gdx.graphics.getDeltaTime());
    stageMove.draw();
}
 @Override
public void show() {
flowerImage.addListener(new DragListener() {
        public void drag(InputEvent event, float x, float y, int pointer) {
            flowerImage.moveBy(x - flowerImage.getWidth() / 2, y - flowerImage.getHeight() / 2);
            flowerImage.setPosition(x,y);

        }
    });
stageMove.addActor(flowerImage);
btnArrow.addListener(new ChangeListener() {

                @Override
                public void changed(ChangeEvent event, Actor actor) {
stageMove.clear();
game.setScreen(0);

I have to .clear() the stage when exiting the screen, otherwise the stage will create a duplicate of the image when I re-enter. So does anyone have a solution to this? I tried just acting the stage and drawing the image from it´s source, but that has the same effect.


Solution

  • "Isn't saved" - who you expect to save it for you? You have to save it after dragging and to load it when you app starts.

    https://github.com/libgdx/libgdx/wiki/File-handling