Niranjana
Niranjana

Reputation: 536

Sound button to make sound ON or OFF does not changing button image

I have a sound button in my menu screen.It has two images.one is normal and other is checked(indicating sound is OFF).

I am using a boolean variable to get the state of this button.If it is true,sound will play else sound will be OFF.I am using it to control in-game sounds.

public boolean soundBool=true;

created soundButton like this:Here soundTexture1 is normal image(ON) and soundTexture2 has a cross mark(OFF).

private void drawSoundButton() {
    soundButton = new ImageButton(new TextureRegionDrawable(soundTexture1),
            new TextureRegionDrawable(soundTexture2), new TextureRegionDrawable(soundTexture2));
    stage.addActor(soundButton);

    soundButton.setPosition(UiConstants.SOUND_X, UiConstants.SOUND_Y, Align.bottom);

    soundButton.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if (!game.buttonClickSoundBool)
                buttonClickSound.play();
            soundButton.scaleBy(0.025f);

            if(game.soundBool)
                game.soundBool=false;
            else
                game.soundBool=true;
        }
    });
}

I am calling all sounds with play() only when this soundBool is true. Now when I click this button,crossmark will come,sound will be off and works fine in terms of sound. Whenever I come back to menu,the image with cross mark should be displayed because the sound is OFF.But it does not shows the cross mark image.It shows the first image.But functionality is fine.If I click,sound will go ON. It would be helpful if someone explains how to solve this.

Upvotes: 0

Views: 88

Answers (1)

AAryan
AAryan

Reputation: 20140

Use setChecked(boolean status); on soundButton according to the status of soundBool.

private void drawSoundButton() {
    soundButton = new ImageButton(new TextureRegionDrawable(soundTexture1),new TextureRegionDrawable(soundTexture2), new TextureRegionDrawable(soundTexture2));
    soundButton.setChecked(!soundBool); // setChecked according to boolean status

    stage.addActor(soundButton);
    soundButton.setPosition( UiConstants.SOUND_X,UiConstants.SOUND_Y,Align.bottom);

    ...
}

Also don't forget to save status of soundBool in preference so that you can restore that value, when you come back in your game.

Upvotes: 1

Related Questions