简体   繁体   中英

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

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. 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.

by the way, start using better names - buttonSound instead of btnSnd, that makes the code easier to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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