1
我想要做的是通過拖動我的手指在我的Android設備上左右移動矩形。我想要矩形停止,當我停止不擡起我的手指。這可能嗎?如何通過觸發touchUp來結束touchDragged? LibGDX
我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;
}
你試過把'false'的返回值改爲'true'嗎?否則,看看[GestureDetector](https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/input/GestureDetector.GestureAdapter.html),特別是'pan()'和' panStop()'方法。您可以使用'InputMultiplexer'使用多個輸入處理器。 – Marius
'touchDragged'和'touchUp'永遠不會被調用,如果你不在'touchDown'中返回true – Tenfour04