2016-12-14 56 views
1

我使用AVFoundation在我的游戏中实现了背景音乐。但是,当离开GameScene时,音乐继续播放。我知道在viewController中你可以使用viewWillDisappear,但是我找不到任何与GameScenes类似的东西。在离开GameScene时在SpriteKit中停止音乐

var audioPlayer = AVAudioPlayer() 
let audioPath = Bundle.main.path(forResource: "electroDrive", ofType: "wav") 

//.......// 

//.......// 

    do { 

     try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!)) 

    } 
    catch { 
     // process error 

    } 

    audioPlayer.play() 


//Code to go to other scene 
if viewController != nil { 
     self.viewController?.performSegue(withIdentifier: "push", sender: viewController) 
    } 
+0

您可以显示当你去到下一个场景的代码? – Jack

+0

是的,给我一秒 –

回答

2

停止AVAudioPlayer在此之前你SEGUE代码segueing到下一个场景

插入之前

audioPlayer.pause() 
+0

是的。当我看到你的评论的时候,我发现了它。谢啦! –