LibGDX:如何用手指移动矩形?

时间:2015-02-07 15:45:06

标签: android events libgdx move drag

我正在尝试使用libgdx用手指移动矩形但是当我在手机上运行代码时它不起作用,矩形停留在左上角。任何想法如何解决这个问题?

public class Main extends ApplicationAdapter implements InputProcessor {
ShapeRenderer shape;
public int xpos;
    public int ypos;
@Override
public void create () {
    shape = new ShapeRenderer();
}

@Override
public void render () {
    Gdx.gl.glClearColor(0, 255, 255, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    shape.begin(ShapeType.Filled);
    shape.setColor(1, 1, 0, 1);
    shape.rect(xpos, ypos, 100, 100);
    shape.end();  
} 
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    xpos = Gdx.input.getX();
    ypos = Gdx.input.getY();
    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    xpos = Gdx.input.getX();
    ypos = Gdx.input.getY();
    return true;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    xpos = Gdx.input.getX();
    ypos = Gdx.input.getY();
    return true;
} 

我还实现了InputProcessor接口的所有其他方法,并为它们提供了return false语句。但就像我说它不起作用......

1 个答案:

答案 0 :(得分:0)

if(Gdx.input.isTouched() &&rectangle.contains(Gdx.input.getX(),Gdx.graphics.getHeight() -Gdx.input.getY())) {
     rectangle.set(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY(), 200, 100);
} 
else {
     rectangle.set(rectangle.x, rectangle.y, 200, 100); 
}