我在Sprite Kit中创建了这个游戏,我让用户能够通过按下按钮来静音,然后才能点击并启动游戏。然而,当游戏结束并且GameOverScene出现时,音乐仍然保持静音。当一个新的GameScene被调用时,音乐会再次开始播放。任何人都知道如何解决这个问题。这是我的音乐代码。
var backgroundMusicPlayer: AVAudioPlayer!
func playBackgroundMusic(filename: String) {
let url = NSBundle.mainBundle().URLForResource(
filename, withExtension: nil)
if (url == nil) {
println("Could not find file: \(filename)")
return
}
var error: NSError? = nil
backgroundMusicPlayer =
AVAudioPlayer(contentsOfURL: url, error: &error)
if backgroundMusicPlayer == nil {
println("Could not create audio player: \(error!)")
return
}
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
}
override func didMoveToView(view: SKView) {
muteSound.name = "muteSound"
muteSound.position.x = view.center.x
muteSound.position.y = view.center.y + 100
self.addChild(muteSound)
playBackgroundMusic("Box Office.mp3")
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node.name == "muteSound" {
backgroundMusicPlayer.stop()
}
}