# 触摸屏幕移动矩形[英] Move rectangle by touching the screen

### 问题描述

```downBlocks=new Arraylist<Rectangle>;
for (DownBlocks downBlocks:getBlocks()){
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();

touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);

downBlocks.x = (int) touchPos.x - downBlocks.x;
}
}
```

## 推荐答案

```static final Vector3 VEC = new Vector3(); // reusuable static member to avoid GC churn
private float lastX; //member variable for tracking finger movement

if (Gdx.input.isTouching()){
VEC.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(VEC);
}

if (Gdx.input.justTouched())
lastX = VEC.x; //starting point of drag
else if (Gdx.input.isTouching()){ // dragging
float deltaX = VEC.x - lastX; // how much finger has moved this frame
lastX = VEC.x; // for next frame

// Since you're working with integer units, you can round position
int blockDelta = (int)Math.round(deltaX);

for (DownBlocks downBlock : getBlocks()){
downBlock.x += blockDelta;
}
}
```