touchDragged如何在libgdx中工作?

suv*_*wda 6 android game-development libgdx

我目前正在学习libgdx游戏编程,现在我已经学会了如何使用touchDown但是我不知道如何使用touchDragged.计算机如何知道手指被拖动的方向(用户是向左还是向右拖动)

noo*_*one 12

电脑不知道.或者至少界面不会告诉你这些信息.它看起来像这样:

public boolean touchDragged(int screenX, int screenY, int pointer);
Run Code Online (Sandbox Code Playgroud)

它与touchDown几乎相同:

public boolean touchDown(int screenX, int screenY, int pointer, int button);
Run Code Online (Sandbox Code Playgroud)

经过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;
}
Run Code Online (Sandbox Code Playgroud)