ClickListener组合拳
touchDown
touchDown方法在屏幕或者鼠标被按下时触发
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if (pressed) return false;
if (pointer == 0 && this.button != -1 && button != this.button) return false;
pressed = true;
pressedPointer = pointer;
pressedButton = button;
touchDownX = x;
touchDownY = y;
setVisualPressed(true);
return true;
}
需要注意的有两点:
- 按下即触发事件。
- 该方法有返回值,返回为false时不会继续执行接下来的touchDragged和touchUp监听。
touchDragged
touchDragged方法在鼠标被按住拖曳或者屏幕被滑动的过程中触发。
public void touchDragged (InputEvent event, float x, float y, int pointer) {
if (pointer != pressedPointer || cancelled) return;
pressed = isOver(event.getListenerActor(), x, y);
if (!pressed) {
// Once outside the tap square, don't use the tap square anymore.
invalidateTapSquare();
}
}
使用时可以和touchDown方法结合使用,如判断滑动距离。
touchUp
touch组合拳的最后一拳,在鼠标抬起或者手指离开屏幕时触发。
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
if (pointer == pressedPointer) {
if (!cancelled) {
boolean touchUpOver = isOver(event.getListenerActor(), x, y);
// Ignore touch up if the wrong mouse button.
if (touchUpOver && pointer == 0 && this.button != -1 && button != this.button) touchUpOver = false;
if (touchUpOver) {
long time = TimeUtils.nanoTime();
if (time - lastTapTime > tapCountInterval) tapCount = 0;
tapCount++;
lastTapTime = time;
clicked(event, x, y);
}
}
pressed = false;
pressedPointer = -1;
pressedButton = -1;
cancelled = false;
}
}
结束性操作/变量重置/资源回收等操作可在此处执行。
clicked
组合拳外七伤拳。监听点击事件,但是和touch事件发生冲突,不要同时使用。
public void clicked (InputEvent event, float x, float y) {
}