1 回答
TA贡献1752条经验 获得超4个赞
要进行拖动,您需要记住手指最后一次触摸屏幕的位置,以便获得手指增量。另外,如果只需要调用一次,则避免将代码放入循环迭代中。一遍又一遍地为每个DownBlock取消投影屏幕的触摸点是很浪费的。
static final Vector3 VEC = new Vector3(); // reusuable static member to avoid GC churn
private float lastX; //member variable for tracking finger movement
//In your game logic:
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;
}
}
不过,我不建议您使用整数单位作为坐标。如果您要进行像素画,则建议使用浮点数来存储坐标,并且仅在绘制时才对坐标进行四舍五入。这将减少看起来像生涩的运动。如果您不使用像素图,我将一直使用浮动坐标。这是一篇很好的文章,可以帮助您理解单位。
添加回答
举报