【问题标题】:ClickListener touchDragged pointer always zeroClickListener touchDragged 指针始终为零
【发布时间】:2020-03-02 23:18:16
【问题描述】:

在处理侦听器类并实现对多点触摸手势的处理时,我遇到了一个可能的错误。

代码实现

public class MyListener extends ClickListener {
    private List<Pointer> pointers = new ArrayList<Pointer>();

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointerIndex, int button) {
        System.out.println("Listener: touch down" + pointerIndex);
        pointers.add(new ListenerPointer(x, y, button));
        event.handle();
        return super.touchDown(event, x, y, pointerIndex, button);
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointerIndex) {
        System.out.println("Listener: dragged " + pointerIndex);

        // Update the current point the user is dragging.
        for (ListenerPointer pointer : pointers) {
            if (pointer.getPointerIndex() == pointerIndex) {
                pointer.update(x, y);
                event.handle();
            }
        }
    }
}

当用新手指在屏幕上触摸时,仍将旧手指放在屏幕上,indexPointer 会增加。导致以下日志:

Listener: touch down0
Listener: touch down1

如果我随后在屏幕上移动两个手指,它只会触发 touchDragged 事件,并且 pointerIndex 始终为零。即使touchDown手势说它的pointerIndex为1。touchDragged的日志总是:

Listener: dragged 0
Listener: dragged 0

我认为这可能是 LibGDX 代码中的一个错误,因为这么简单的一段代码真的不会出错。

【问题讨论】:

    标签: java android libgdx touch-event


    【解决方案1】:

    我以为我已经仔细阅读了 touchDragged 的​​文档,但我真是个傻瓜。它声明如下:

    Called when a mouse button or a finger touch is moved anywhere, but only if touchDown previously returned true for the mouse button or touch.

    我猜super.touchDown(event, x, y, pointerIndex, button) 只有在 pointerIndex 为 0 时才返回 true。解释为什么在 pointerIndex > 0 时 touchDragged 事件没有触发。

    简单的解决方法是让touchDown 总是返回true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2016-05-02
      • 2012-01-16
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多