用于使声音打开或关闭的声音按钮不会更改按钮图像 [英] Sound button to make sound ON or OFF does not changing button image

查看:104
本文介绍了用于使声音打开或关闭的声音按钮不会更改按钮图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的菜单屏幕上有一个声音按钮,它有两张图像.一个图像正常,另一个图像被选中(指示声音为OFF).

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

我正在使用布尔变量获取此按钮的状态.如果为true,则声音将播放,否则声音将为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;

创建如下所示的soundButton:soundTexture1为正常图像(ON),而soundTexture2为十字标记(OFF).

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

仅当此soundBool为true时,我才用play()调用所有声音. 现在,当我单击此按钮时,将出现十字标记,声音将关闭并且在声音方面可以正常工作. 每当我回到菜单时,由于声音为OFF,应该显示带有十字标记的图像.但是它没有显示十字标记图像.它显示了第一张图像. . 如果有人解释如何解决这个问题,将会很有帮助.

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.

推荐答案

根据soundBool的状态在soundButton上使用setChecked(boolean status);.

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

    ...
}

此外,别忘了优先保存soundBool的状态,以便在游戏重新出现时可以恢复该值.

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.

这篇关于用于使声音打开或关闭的声音按钮不会更改按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆