I'm using libgdx with Eclipse.


In the class inputHandler I have a list of buttons, menuButtons, composed of 2 buttons: playButton and retryButton. They both belong to the class simpleButton:

public static simpleButton playButton, retryButton;

I declared them as:

retryButton = new simpleButton(100, 100, 40, 10, retryRegion, retryRegion);
playButton = new simpleButton(80, 80, 20, 20, assetLoader.playButtonUp, assetLoader.playButtonDown);

(see note at end of question)

But when I run the game only the 2nd button runs (in the above case, playButton).

So I went to the class gameRenderer and used shapeRenderer to clearly see what was going on.

shapeRenderer.rect(inputHandler.retryButton.bounds.x, inputHandler.retryButton.bounds.y, 
    inputHandler.retryButton.bounds.width, inputHandler.retryButton.bounds.height);

shapeRenderer.rect(inputHandler.playButton.bounds.x, inputHandler.playButton.bounds.y, 
        inputHandler.playButton.bounds.width, inputHandler.playButton.bounds.height);

Although only the 2nd button was displaying, the 2nd button now adopted the code for both the buttons. i.e. the 2nd button was now both the play and retry (replay) buttons and worked perfectly.

note from before: These aren't their actual sizes (the actual sizes are equal to the sizes of the texture regions of their art graphics) but for the sake of simplicity and legibility I changed them to simpler values.


playButton on bottom (playButton working): inputHandler code: running:

retryButton on bottom (retryButton working): inputHandler code: running:

The simpleButton class:

public class simpleButton {

    private float x, y, width, height;

    private TextureRegion buttonUp;
    private TextureRegion buttonDown;

    public static Rectangle bounds;

    public static boolean isPressed;

    public simpleButton(float x, float y, float width, float height,
            TextureRegion buttonUp, TextureRegion buttonDown) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.buttonUp = buttonUp;
        this.buttonDown = buttonDown;

        bounds = new Rectangle(x, y, width, height);

1 Answer 1


not a java developer but:

public static Rectangle bounds;

is no good. Static members belongs to class not to objects (class references). So when you change bounds

 bounds = new Rectangle(x, y, width, height);

you set it for all simpleButton objects . And the last instantiated goes in top of the others.

  • \$\begingroup\$ This solved the problem! I just took out the "static". Thank you! \$\endgroup\$
    – resfever
    Sep 16, 2015 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .