多次调用 applicationWillResignActive 后游戏崩溃

Game Crashes after applicationWillResignActive is called more than once

所以当用户收到一条短信或进入通知中心或离开应用程序时,一个名为 pauseGame() 的函数应该是 运行,它第一次工作正常,但如果有人收到另一条短信或离开再次进入通知中心然后应用程序将崩溃,我的问题是为什么它第一次工作但第二次崩溃?我认为这与计时器有关,但你能不能看看我的代码,我一直在谷歌搜索,这是我最后的选择!谢谢你们!! (此外,此代码在 ios 9 上工作正常并且每次都有效,但问题出在 ios 8 上)

这是用户进入后台时调用的函数

  func applicationWillResignActive(application: UIApplication) {
    print("Reply to Text or Notification Center or Multitasking")
    NSNotificationCenter.defaultCenter().postNotificationName("pauseGame", object: self)
}

这是我的暂停功能

func pauseGame() {
    if(paused == false) {
        //STOPS UPDATE AND MOVEMENT OF NODES
        if (gameOver == false) {

            spawnTopTimer.invalidate()
            spawnBottomTimer.invalidate()
            spawnLeftTimer.invalidate()
            spawnRightTimer.invalidate()
            scoreTimer.invalidate()


            paused = true
            pauseScreenActive = true

            player.physicsBody!.categoryBitMask = colisionType.Enemy.rawValue
            player.physicsBody!.contactTestBitMask = colisionType.Player.rawValue
            player.physicsBody!.collisionBitMask = colisionType.Player.rawValue

            //RESETS NODES TO VISABLE
            pauseBtn.hidden = true
            screenContainer.hidden = false
            resumeBtn.hidden = false
            homeBtn.hidden = false
            highscoreLabel.hidden = false
            currentScoreLabel.hidden = false
            pauseLabel.hidden = false


            //PAUSE SCREEN NODES
            screenContainer.size = CGSize(width: self.frame.size.width , height: self.frame.size.width)
            screenContainer.alpha = 0.8
            screenContainer.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
            screenContainer.zPosition = 900

            pauseLabel.text = "PAUSED"
            pauseLabel.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.69)
            pauseLabel.fontSize = 60
            pauseLabel.zPosition = 999

            resumeBtn.size = CGSize(width: 400, height: 80)
            resumeBtn.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.6)
            resumeBtn.zPosition = 999

            homeBtn.size = CGSize(width: 400, height: 80)
            homeBtn.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.45)
            homeBtn.zPosition = 999

            currentScoreLabel.text = "Current Score: " + String(score-1)
            currentScoreLabel.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.33)
            currentScoreLabel.fontSize = 40
            currentScoreLabel.zPosition = 999

            if(highscore == 0) {
                highscoreLabel.text = "Highscore: " + String(0)
            } else {
                highscoreLabel.text = "Highscore: " + String(highscore-1)
            }
            highscoreLabel.position = CGPointMake(self.frame.size.width/2, self.frame.size.height*0.26)
            highscoreLabel.fontSize = 40
            highscoreLabel.zPosition = 999
        }
    }
}

这是我的通知程序

 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGame"), name: "pauseGame", object: nil)

这里是

之后恢复游戏的代码
if self.nodeAtPoint(location) == resumeBtn {
            if(pauseScreenActive == true) {
            //HIDE PAUSE SCREEN
            screenContainer.hidden = true
            resumeBtn.hidden = true
            homeBtn.hidden = true
            highscoreLabel.hidden = true
            currentScoreLabel.hidden = true
            pauseBtn.hidden = false
            pauseLabel.hidden = true
            player.physicsBody!.categoryBitMask = colisionType.Player.rawValue
            player.physicsBody!.contactTestBitMask = colisionType.Enemy.rawValue
            player.physicsBody!.collisionBitMask = colisionType.Enemy.rawValue
            spawnTopTimer = NSTimer.scheduledTimerWithTimeInterval(ySpeed, target: self, selector: Selector("spawnTop"), userInfo: nil, repeats: true)
            spawnBottomTimer = NSTimer.scheduledTimerWithTimeInterval(ySpeed, target: self, selector: Selector("spawnBottom"), userInfo: nil, repeats: true)
            spawnLeftTimer = NSTimer.scheduledTimerWithTimeInterval(xSpeed, target: self, selector: Selector("spawnLeft"), userInfo: nil, repeats: true)
            spawnRightTimer = NSTimer.scheduledTimerWithTimeInterval(xSpeed, target: self, selector: Selector("spawnRight"), userInfo: nil, repeats: true)
            print("Resume Speed with xValue: " + String(xSpeed) + " and yValue " + String(ySpeed))
            scoreTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("scoreCalculator"), userInfo: nil, repeats: true)
            paused = false
            pauseScreenActive = false
            }
        }

我怀疑您的问题可能出在您的 GameScene 中。如果您正在注册 GameScene 以接收 NSNotifications,您还需要删除注册,因为多次注册可能会导致崩溃。

尝试在 willMoveFromView:(SKView *)view 方法中包含 [[NSNotificationCenter defaultCenter] removeObserver:self];

是的!!!!那行得通,我不得不将其更改为 swift,即

NSNotificationCenter.defaultCenter().removeObserver(self) 

但它解决了我的崩溃问题!非常感谢,我已经被困在这个问题上好几天了 :) <3 Big Ups to you sir!!