在不止一次调用applicationWillResignActive之后游戏崩溃

时间:2015-07-01 10:38:16

标签: ios swift crash sprite-kit

因此,当用户收到文本或进入通知中心或离开应用程序时,应运行名为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
            }
        }

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

YES !!!!这工作我必须把它改成swift,这是

NSNotificationCenter.defaultCenter().removeObserver(self) 

但它修复了我的崩溃!非常感谢你,我已经被困在这里好几天了:)< 3 Big Ups给你先生!!