LibGdx ImageButton播放声音
LibGdx ImageButton playing sound
我创建了一个包含三个可绘制图像的 imageButton;
一个是按钮向上,按钮向下和按钮 checked.Its 目的是打开和关闭声音,因为 game.once 声音是关闭的,应该显示选中的图像。
我是这样写代码的:
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时会播放游戏音效。
一旦我做到了,声音不应该 play.This 布尔值运行良好。
问题是一旦我检查了按钮(声音关闭)声音就关闭了permanantly.Again按钮点击没有按预期工作。
如何更改代码以使其正常工作?
您检查过 changed()
方法的调用频率了吗?使用 Gdx.app.log()
进行记录。或者尝试使用不同的监听器,例如按钮 ClickedListener
。
您也可以创建自己的 Button,class MyButton extends Button
以保持更简洁的代码。 Here 我就是这样解决的。
我创建了一个包含三个可绘制图像的 imageButton; 一个是按钮向上,按钮向下和按钮 checked.Its 目的是打开和关闭声音,因为 game.once 声音是关闭的,应该显示选中的图像。
我是这样写代码的:
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时会播放游戏音效。 一旦我做到了,声音不应该 play.This 布尔值运行良好。
问题是一旦我检查了按钮(声音关闭)声音就关闭了permanantly.Again按钮点击没有按预期工作。
如何更改代码以使其正常工作?
您检查过 changed()
方法的调用频率了吗?使用 Gdx.app.log()
进行记录。或者尝试使用不同的监听器,例如按钮 ClickedListener
。
您也可以创建自己的 Button,class MyButton extends Button
以保持更简洁的代码。 Here 我就是这样解决的。