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;
}
}
不過,我不建議您使用整數單位作為坐標。如果您要進行像素畫,則建議使用浮點數來存儲坐標,并且僅在繪制時才對坐標進行四舍五入。這將減少看起來像生澀的運動。如果您不使用像素圖,我將一直使用浮動坐標。這是一篇很好的文章,可以幫助您理解單位。
添加回答
舉報