How does touchDragged work in libgdx?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 17:44:18

问题


I am currently learning libgdx game programming,now i have learnt how to use touchDown but iam not getting idea how to use touchDragged.How will the computer knows in which direction the finger is dragged(whether the user has dragged to left or right)


回答1:


The computer doesn't know that. Or at least the interface won't tell you this information. It looks like this:

public boolean touchDragged(int screenX, int screenY, int pointer);

It is nearly the same like touchDown:

public boolean touchDown(int screenX, int screenY, int pointer, int button);

After a touchDown event happened, only touchDragged events will occur (for the same pointer) until a touchUp event gets fired. If you want to know the direction in which the pointer moved, you have to calculate it yourself by computing the delta (difference) between the last touchpoint and the current one. That might look like this:

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;
}



回答2:


The touch dragged method is called every frame that the touch position changes. The touch down method is called every time you touch the screen down, and touch up when you release.

LibGDX - Get Swipe Up or swipe right etc.?

This can help you a little bit.



来源:https://stackoverflow.com/questions/23731073/how-does-touchdragged-work-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!