简体   繁体   English

按钮声音不起作用

[英]Button Sound not Working

I have btnSound which works well except when I moving from screen to screen as when I click play button to move from menu screen to play screen 我的btnSound效果很好,除了当我从屏幕移动到屏幕时,例如单击播放按钮从菜单屏幕移动到播放屏幕时,

I have this : 我有这个 :

playButton.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        btnSound.play();
        gameMain.setScreen(new PlayScreen(gameMain);
    }
});

Any Solution ? 有什么办法吗?

Sound disposing when you change screen. 更换屏幕时声音会消失。 You can 您可以

  • Add touchUp listener method for change screen but sound still stay same as current. 添加touchUp侦听器方法以更改屏幕,但声音仍保持与当前相同。 So when you click to button sound will play but screen change when you up your touch. 因此,当您单击按钮时,声音会播放,但触摸时屏幕会改变。
  • Play sound in next screen. 在下一个屏幕中播放声音。

  • Prevent dispose of sound when you changing screen. 切换屏幕时,请避免声音的处置。

Not much code, so i have to guess. 没有太多的代码,所以我不得不猜测。 btnSnd is a part of lets say - MenuScreen, and when you call the gameMain.setScreen() method, MenuScreen is disposing (look for dispose() method) to free the memory (and btnSnd with it, because it's part of it) My suggestion - start the sound in the constructor of PlayScreen instead of the Listener of the button. btnSnd是可以说的一部分-MenuScreen,当您调用gameMain.setScreen()方法时,MenuScreen正在处置(寻找dispose()方法)以释放内存(以及btnSnd,因为它是其中的一部分)建议-在PlayScreen的构造函数中而不是按钮的Listener处启动声音。

by the way, start using better names - buttonSound instead of btnSnd, that makes the code easier to read. 顺便说一句,开始使用更好的名称-buttonSound而不是btnSnd,这使代码更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM