Multiple Buttons pressed at once?

I’m currently having a problem where when I click on a single button, the game decides to click the one that gets put in the same place as it. This gets extremely annoying when i’m trying to change levels. This is my MouseReleased() function.

public void mouseReleased(MouseEvent e) {
		MouseClick = false;
		GuiManager.isButtonPressed(mx, my);
		
	}

And this is where the it’s checked to see if you click the buttons.(I don’t know if adding this helps)

	public static void isButtonPressed(int mx, int my){
		mx = mx /3;
		my = my / 3;
		for(int counter = 0;counter < Buttons.size();counter++){
			if (Buttons.get(counter).box.contains(mx, my)){
				Buttons.get(counter).Press();
				continue;
			}
				
		}
	}

Can anyone offer a path to a solution for this?

misleading question and code
not enough information

You have a method named “isButtonPressed” that has side effects and returns void. I can’t imagine anything more confusingly named.

Do you have multiple objects that implement the mouse listener interface on top of each other?

And why the hell would you have two buttons in the same place? :clue:

Yeah, Now that I look at it, the code is rather confusing. However, I fixed. The problem is that it should be ‘break;’ instead of ‘continue;’

I see. He didn’t put actionListener to a button as usual. It grabbed mouse press position and pool which button “is pressed” from pool of buttons. When he didn’t use ‘break’ another button (which has same position but maybe on another gamestate/level) got triggered too.

Yup. Maybe he made his own button class?
However, using break; means that whichever button is stored earlier in the array(meaning lower index) is triggered, and that may not be what you want. Watch out for that while writing new code/debugging.

precisely, I forgot to mention that. That might have made things a bit less confusing.

THIS. this is exactly what happened. I’m still relatively new, so i got continue and break mixed up.

“the game decides to click the one that gets put in the same place as it”

mind -> BOOM

might be due to the same X,Y as another button.
try:


public void mouseClicked(MouseEvent m) {
if (mouseIsClickedInside(x,y,w,h) {
//button clicked.
if (!inGameMenu) {
// ==O
}
}
}


The naming is confusing indeed. How about helping yourself by naming stuff something like this :slight_smile:

public static void handleButtonPress(int mx, int my)

You should change it, or separate pool of buttons between states.