gpt4 book ai didi

java - ClickListener touchDragged 指针始终为零

转载 作者:行者123 更新时间:2023-12-02 08:55:42 24 4
gpt4 key购买 nike

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

代码实现

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 事件,且指针索引始终为零。即使 touchDown 手势表示其指针索引为 1。 touchDragged 的​​日志始终为:

Listener: dragged 0
Listener: dragged 0

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

最佳答案

我以为我已经仔细阅读了touchDragged的文档,但我真是个傻瓜。它指出以下内容:

当鼠标按钮或手指触摸移动到任意位置时调用,但前提是 touchDown 先前为鼠标按钮或触摸返回 true。

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

简单的修复方法是使 touchDown 始终返回 true

关于java - ClickListener touchDragged 指针始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60497986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com