【发布时间】:2014-05-19 06:33:26
【问题描述】:
我目前正在学习 libgdx 游戏编程,现在我已经学会了如何使用 touchDown,但我不知道如何使用 touchDragged。计算机如何知道手指被拖动的方向(用户是否向左拖动或对)
【问题讨论】:
标签: android libgdx game-development
我目前正在学习 libgdx 游戏编程,现在我已经学会了如何使用 touchDown,但我不知道如何使用 touchDragged。计算机如何知道手指被拖动的方向(用户是否向左拖动或对)
【问题讨论】:
标签: android libgdx game-development
计算机不知道这一点。或者至少界面不会告诉你这些信息。它看起来像这样:
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 方法,松开时会调用 touch down 方法。
LibGDX - Get Swipe Up or swipe right etc.?
这对你有一点帮助。
【讨论】: