libgdx image button播放声音



我创建了一个带有三个可绘制图像的imageButton;一个是按钮,按钮向下检查和按钮检查。目的是打开和关闭游戏的声音。一旦声音关闭,应显示检查图像。

我写了这样的代码:

soundButton = new ImageButton(new TextureRegionDrawable(soundTexture1),
            new TextureRegionDrawable(soundTexture2), new TextureRegionDrawable(soundTexture3));
    stage.addActor(soundButton);
    soundButton.setPosition(Constants.WORLD_WIDTH / 4 + 300f, Constants.WORLD_HEIGHT / 4, Align.bottomLeft);
soundButton.addListener(new ChangeListener(){
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            if(!game.soundBool)
                game.soundBool=true;
                else
                    game.soundBool=false;
        }
    });

这里的声音最初是错误的,并且游戏声音在错误时会播放。一旦我实现了,声音就不应该播放。这个布尔人运作良好。

问题是,一旦我检查了按钮(声音关闭)声音即可永久关闭。随着预期,点击无法正常工作。

如何更改代码以良好工作?

您是否检查了一次 changed()方法的频率?使用Gdx.app.log()进行记录。或尝试使用其他侦听器,例如ClickedListener用于按钮。

您还可以制作自己的按钮,class MyButton extends Button来保留更干净的代码。这是我解决的方式。

最新更新