简体   繁体   中英

Button Action Repeating Twice After Clicking Once

I created a Button Class for my game,based on Slick which is extends MouseOverArea Class.
I Used another class for perform action when mouse click on the button.
Now I have a problem with it.When I click on the button once,button action is repeat twice.
This is the override mouseClicked method code of my Button Class.

@Override
    public void mouseClicked(int button, int x, int y, int clickCount) { 
        if (isMouseOver() && stateBasedGame.getCurrentStateID() == stateID ) {
            for (ButtonAction ba : actionList) {
                ba.action();
            }
        }
        super.mouseClicked(button, x, y, clickCount);
    }


I use ArrayList for hold all action.
(I add only one ButtonAction listener for My created Button Object)
This is My ButtonAction class

public  interface ButtonAction {
    public void action();
}


This is how I add action to My Created Button Object.

   myButton.addAction(new ButtonAction() {
                @Override
                public void action() {
                //this line repeate twice
                    System.out.println("Click On Me.."); 
                }
            });

I need to know,is there something wrong with my code which is relevant to repeating button action

Some pointers:

  1. I would suggest you revise your mouseClicked implementation. Try adding return after you notify all your ButtonAction s. Right now you are executing super.mouseClicked every time, and this may be causing the double call for some reason.
  2. If you don't want to have duplicate Button actions for a single button, maybe you should consider storing them in a Set and having meaningful equals method so you don't add duplicates.
  3. Are you positive that myButton.addAction is invoked exactly once? You can verify this by debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM