【发布时间】:2019-08-30 18:02:00
【问题描述】:
我在 android 上做我的第一个游戏,差不多完成了,但是我的音乐有问题,当你开始游戏或死亡时音乐开始和结束,但是如果你按下主页按钮或返回按钮它永远不会停止
我已尝试在此处寻找解决方案,但我的代码无法正常工作
这是 SoundBank 类,游戏开始时调用 playBackground,死亡时调用 stopBackground
public class SoundBank {
Context context;
MediaPlayer background, hit;
int mute;
GameOver gameOver = new GameOver();
public SoundBank(Context context){
this.context = context;
hit = MediaPlayer.create(context,R.raw.sfx_hit);
background = MediaPlayer.create(context,R.raw.background);
mute = gameOver.getMute();
}
public void playHit(){
if(mute != 1){
hit.start();
}
}
public void playBackground(){
if(background != null){
background.start();
background.setLooping(true);
}
}
public void stopBackground(){
if(background != null){
background.stop();
}
}
}
我希望当我按下主页按钮或返回按钮时音乐结束
【问题讨论】:
-
stopBackground是从哪里触发的? -
游戏在你死后启动
-
当用户按下主页或返回时,这并不意味着游戏引擎死机。