Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

libgdx: 2nd var of myClass()'s values saving to/overriding 1st var of myClass()

$
0
0

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);
    }

Viewing all articles
Browse latest Browse all 434

Trending Articles