简体   繁体   English

touchDragged如何在libgdx中工作?

[英]How does touchDragged work in libgdx?

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

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: 它与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. 发生touchDown事件后,只会touchDragged事件(对于相同的指针),直到触发touchUp事件。 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;
}

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.? LibGDX - 向上滑动或向右滑动等?

This can help you a little bit. 这可以帮助你一点点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM