首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Libgdx监听器多次触发

Libgdx监听器多次触发
EN

Stack Overflow用户
提问于 2016-03-31 14:20:54
回答 1查看 1.1K关注 0票数 5

我目前正在尝试实现一个clickListener。我在运行时找到了一个很好的工具,名为Overlap2D,在那里我制作了一些很好的按钮并加载了它们,一切都很好。因为我想为我的按钮设置一个“悬停”效果,所以我使用了ClickListener和方法enter和exit,它看起来是这样的:

代码语言:javascript
复制
      @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方法在触地得分和touchupo事件O中也会被调用,看起来是这样的:

代码语言:javascript
复制
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,但我仍然不知道为什么它会触发出口和输入事件。有人有主意吗?

谢谢:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 15:35:40

您需要检查传递给enterexit方法的exit。如果pointer-1,则鼠标光标刚刚启动或停止悬停在参与者的边界上。它的pointer不是-1,然后演员只收到一个点击向下或点击发布(分别为enterexit )。

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

  • 使用指针-1输入的悬停->
  • 单击使用指针0输入的->
  • 释放使用指针0退出的->
  • 将光标移开以指针-1退出的->

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36336111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档