0
\$\begingroup\$

I'm using libgdx with Eclipse.


PROBLEM:

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.


Pictures:

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);
    }
\$\endgroup\$
2
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.