Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LibGdx图像按钮播放声音_Libgdx_Imagebutton - Fatal编程技术网

LibGdx图像按钮播放声音

LibGdx图像按钮播放声音,libgdx,imagebutton,Libgdx,Imagebutton,我创建了一个带有三个可绘制图像的imageButton; 一个是按钮向上、按钮向下和按钮选中。其目的是打开和关闭游戏的声音。一旦声音关闭,应显示选中的图像 我编写了如下代码: soundButton = new ImageButton(new TextureRegionDrawable(soundTexture1), new TextureRegionDrawable(soundTexture2), new TextureRegionDrawable(soundTextu

我创建了一个带有三个可绘制图像的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;
        }
    });
这里soundBool最初为false,当它为false时,游戏声音将播放。 一旦我把它变成现实,声音就不应该播放了。这个布尔值工作得很好

问题是,一旦我检查了按钮(声音关闭),声音就会永久地消失。再次,按钮点击并没有按预期工作


如何更改代码以使其正常工作?

您是否检查了调用
changed()
方法的频率?使用
Gdx.app.log()
进行日志记录。或者尝试使用不同的侦听器,如按钮的
ClickedListener

您还可以创建自己的按钮,
classmybutton扩展了按钮
,以保持代码更清晰。我就是这样解决的