Libgdx 监听器进入和退出触发多次

Libgdx Listener enter and exit fires multiple times

我目前正在尝试实现 clickListener。 我找到了一个很棒的运行时工具,它叫做 Overlap2D,我在那里制作了一些漂亮的按钮并加载了它们,一切正常。 因为我想为我的按钮制作 "hover" 效果,所以我将 ClickListener 与进入和退出方法一起使用,它看起来是这样的:

      @Override
       public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor){

               playButton.setLayerVisibility("MouseOver", true);
               playButton.setLayerVisibility("pressed", false);
               playButton.setLayerVisibility("normal", false);     

               System.out.println("Actor enter : "+fromActor);

       }

       @Override
       public void exit(InputEvent event, float x, float y, int pointer, Actor toActor){


               playButton.setLayerVisibility("MouseOver", false);
               playButton.setLayerVisibility("pressed", false);
               playButton.setLayerVisibility("normal", true);

               System.out.println("Actor exit : "+toActor);

       }
       @Override
       public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){

               System.out.println("touchdown");

               return true;
       }
        @Override
       public void touchUp (InputEvent event, float x, float y, int pointer, int button){

               System.out.println("touchup");

       }

这就是问题所在,当我按下或向上触摸按钮时,触地得分和触地触球被调用一次。但是 enter 和 exit 方法在 touchdown 和 touchupo 事件期间也会被调用 O.o 看起来是这样的:

touchdown
Actor enter : null
Actor exit : Image
Actor enter : Image
touchup
Actor exit : Image
Actor exit : Image
Actor enter : Image
Actor exit : Image
Actor enter : Image

我打印了 fromActor 和 toActor 也是为了调试^^ 而且我仍然不知道为什么它会如此频繁地触发退出和进入事件...... 有人有想法吗?

谢谢:)

您需要检查传递给 enterexit 方法的 pointer。如果 pointer-1,则鼠标光标刚刚开始或停止在 actor 的边界上悬停。如果 pointer 不是 -1,则演员刚刚收到点击或点击释放(分别针对 enterexit)。

因此,如果您将光标移到按钮上并单击然后移开,您将获得两个 enter 事件和两个 exit 事件。

  • 悬停 -> 使用指针 -1 输入
  • 点击向下->用指针0进入
  • 释放点击 -> 指针 0 退出
  • 将光标移开 -> 退出 带指针 -1

此外,在使用 ClickListener 时,请确保在重写方法时调用超级方法,这样它才会正常运行!