javabuttonlibgdxclicklisteneroverlap2d

Libgdx Listener enter and exit fires multiple times


Im currently trying to implement an clickListener. I found a great tool with runtime, its called Overlap2D, there i made some nice buttons and loaded them, it all works fine. Because i wanted to make a "hover" effect for my buttons, i used the ClickListener with the methods enter and exit, it looks so :

      @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");

       }

And heres the problem, touchdown and touchup are called one time, when i touch down the button or up. But the enter and exit methods are also called during the touchdown and touchupo event O.o that looks so:

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

I printed the fromActor and toActor also for debugging ^^ And i still dont know why it fires the exit and enter event so much ... Any one got an idea?

Thanks :)


Solution

  • You need to check the pointer that's passed into the enter and exit methods. If the pointer is -1, then the mouse cursor just started or stopped hovering over the bounds of the actor. It the pointer is not -1, then the actor just received a click down or click release (for enter and exit respectively).

    So if you move the cursor over the button and click and then move away, you get two enter events and two exit events.

    Also, when using ClickListener, make sure you are calling through to super methods when overriding methods, so it will behave properly!