2016-12-14 47 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

是的。當我看到你的評論的時候,我發現了它。謝啦! –