user3395989
user3395989

Reputation: 73

libGDX changing button's image

By following Kilbolt's Zombie Bird tutorial I created in my project a class to represent all buttons:

public class SimpleButton {

private float x, y, width, height;

private TextureRegion buttonUp;
private TextureRegion buttonDown;

private Rectangle bounds;

private boolean isPressed = false;

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

}

public boolean isClicked(int screenX, int screenY) {
    return bounds.contains(screenX, screenY);
}

public void draw(SpriteBatch batcher) {
    if (isPressed) {
        batcher.draw(buttonDown, x, y, width, height);
    } else {
        batcher.draw(buttonUp, x, y, width, height);
    }
}

public boolean isTouchDown(int screenX, int screenY) {

    if (bounds.contains(screenX, screenY)) {
        isPressed = true;
        return true;
    }

    return false;
}

public boolean isTouchUp(int screenX, int screenY) {

    // It only counts as a touchUp if the button is in a pressed state.
    if (bounds.contains(screenX, screenY) && isPressed) {
        isPressed = false;
        return true;
    }

    // Whenever a finger is released, we will cancel any presses.
    isPressed = false;
    return false;
}
}

Whenever I want to create a button I do it in my InputProcessor class by creating a SimpleButton object and after filling its parameters I add it to List list. My current problem is that I'm trying to create a sound button, which I did create and it work fine, but what I'm struggling with is making the button change TextureRegion between ON and OFF states. This is an example of how I create a button(actually I created the sound button) and put it in the list:

    soundButton = new SimpleButton(
            136 / 2 - (AssetLoader.soundOnNotClicked.getRegionWidth() / 0.25f),
            midPointY - 102, 15, 15, AssetLoader.soundOnNotClicked,
            AssetLoader.soundClicked);
    gameButtons.add(backButton);

I tried using the same condition that when sound ON is on it will put inside the parameters "AssetLoader.soundOnNotClicked" and when it's OFF "AssetLoader.soundOFFNotClicked", but it just show the first TextureRegion in the condition. Can someone help me figure out how I can change TextureRigions when sound is ON and OFF?

EDIT WITH CHANGES: First created a second constractor:

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

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

}

Then changed a bit the isClicked method:

public boolean isClicked(int screenX, int screenY) {
    if (isOn == false) {
        isOn = true;
    }else {
        isOn = false;
    }
    return bounds.contains(screenX, screenY);
}

Then changed the draw method:

public void draw(SpriteBatch batcher) {
    if (isPressed) {
        batcher.draw(buttonDown, x, y, width, height);
    } else {
        if (isOn == false) {
            batcher.draw(buttonUp, x, y, width, height);
        }else{
            batcher.draw(buttonOn, x, y, width, height);
        }
    }
}

Lastly I changed the creation of the button by adding the textureRegion of the OFF sound:

    soundButton = new SimpleButton(
            136 / 2 - (AssetLoader.soundButtonOff.getRegionWidth() / 0.25f),
            midPointY - 102, 15, 15, AssetLoader.soundButtonOff,
            AssetLoader.soundButtonOn, AssetLoader.soundButtonOffX);
    menuButtons.add(soundButton);

Upvotes: 0

Views: 1302

Answers (1)

Barodapride
Barodapride

Reputation: 3723

  1. Make another boolean called isOn just like isPressed
  2. Add another TextureRegion called buttonOn just like buttonDown
  3. Add TextureRegion buttonDown to your constructor and set it there like you do with buttonDown
  4. Flip the boolean whenever the button is clicked in isClicked(). (isOn = !isOn)
  5. Add the condition in your draw method to draw the buttonOn texture if isOn is true.

That should pretty much do it.

Upvotes: 1

Related Questions