拖动图像修复(libGDX)

时间:2016-06-25 12:53:42

标签: java android libgdx game-engine drag

我正在尝试制作一张可拖动的图片,仍然无法获得结果。点击它时,图像在Y轴上跳跃。

我的编码尝试:

public class MyGdxGame extends ApplicationAdapter {

SpriteBatch batch;
Texture img;
Sprite sprite;
float offsetX;
float offsetY;

@Override
public void create() {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");
    sprite = new Sprite(img);
    sprite.setPosition(200,200);

}

@Override
public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    sprite.draw(batch);
    batch.end();

    if (Gdx.input.justTouched()) {

        offsetX = Gdx.input.getX() - sprite.getX();
        offsetY = Gdx.input.getY() - (Gdx.graphics.getHeight() - (sprite.getY() + sprite.getHeight()));

    }

    if (Gdx.input.isTouched()){

        sprite.setPosition(Gdx.input.getX() - offsetX, (Gdx.graphics.getHeight() - Gdx.input.getY()) - offsetY);

    }

}

}

这是我得到的结果:

example

我该如何解决?

1 个答案:

答案 0 :(得分:1)

offsetY = Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getY();

应该有效