ClickListener touchDragged 指针始终为零

ClickListener touchDragged pointer always zero

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

代码实现

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 代码中的错误,因为这么简单的一段代码不可能真的出错。

我以为我已经仔细阅读了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 时才 return 为真。解释为什么当 pointerIndex > 0 时 touchDragged 事件没有触发。

简单的解决方法是让 touchDown 始终 return true