我正在尝试通过在我的android设备上拖动手指左右移动矩形。我希望矩形停止时不抬起手指就停止。这可能吗?
我的InputHandler代码:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
lastTouch.set(screenX, screenY);
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// Determines if my finger is moving to the left or right
Vector2 newTouch = new Vector2(screenX, screenY);
Vector2 delta = newTouch.cpy().sub(lastTouch);
// Moves the player right
if(delta.x > 0f) {
world.getPlayer().setVel(5.0f, 0f);
} else {
// Moves the player left
if(delta.x < 0f) {
world.getPlayer().setVel(-5.0f, 0);
}
// This is not being called
else
world.getPlayer().setVel(0f, 0f);
}
lastTouch = newTouch;
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
world.getPlayer().setVel(0f, 0f);
return false;
}
最佳答案
问题是当您触摸拖动时,输入永远不会精确地是手指所在的像素,因为触摸屏的像素精度不高。您的手指同时触摸多个像素的屏幕。然后,设备决定将哪个像素发送到应用程序。
因此,可能的解决方案是添加一个最小增量,以完全移动播放器。如果未达到最小增量,则停止运动。
if(delta.x > 20f) {
world.getPlayer().setVel(5.0f, 0f);
} else {
// Moves the player left
if(delta.x < -20f) {
world.getPlayer().setVel(-5.0f, 0);
}
// This is not being called
else
world.getPlayer().setVel(0f, 0f);
}
在这种情况下,最小增量为20。您需要围绕满足您需要的值进行评估。
关于java - 我该如何结束touchDragged触发touchUp? LibGDX,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41275009/