我目前正在学习libgdx游戏编程,现在我已经学会了如何使用touchDown,但我不知道如何使用touchdrag。计算机如何知道手指在哪个方向被拖动(无论用户向左还是向右拖动)
计算机不知道这些。或者至少界面不会告诉你这些信息。它看起来像这样:
public boolean touchDragged(int screenX, int screenY, int pointer);
它几乎和touchDown:
一样public boolean touchDown(int screenX, int screenY, int pointer, int button);
在touchDown
事件发生后,只有touchDragged
事件会发生(对于同一个指针),直到touchUp
事件被触发。如果您想知道指针移动的方向,您必须通过计算最后一个接触点和当前接触点之间的增量(差)来自己计算。它可能看起来像这样:
private Vector2 lastTouch = new Vector2();
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
lastTouch.set(screenX, screenY);
}
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector2 newTouch = new Vector2(screenX, screenY);
// delta will now hold the difference between the last and the current touch positions
// delta.x > 0 means the touch moved to the right, delta.x < 0 means a move to the left
Vector2 delta = newTouch.cpy().sub(lastTouch);
lastTouch = newTouch;
}
当触摸位置改变时,每一帧都会调用触摸拖动方法。touch down方法在每次你向下触摸屏幕时被调用,在你释放屏幕时向上触摸。
LibGDX - Get Swipe Up或Swipe right等?