【发布时间】: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