【问题标题】:LibGdx ImageButton playing soundLibGdx ImageButton 播放声音
【发布时间】:2016-12-23 06:25:43
【问题描述】:

我创建了一个带有三个可绘制图像的 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 最初是假的,当它是假的时候会播放游戏声音。 一旦我做到了,声音就不应该播放了。这个布尔值运行良好。

问题是,一旦我检查了按钮(声音关闭)声音就会永久关闭。再次单击按钮无法按预期工作。

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

【问题讨论】:

    标签: libgdx imagebutton


    【解决方案1】:

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

    您也可以创建自己的 Button,class MyButton extends Button,以保持代码更简洁。 Here 是我解决它的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多