2015-07-01 31 views
0

所以當用戶收到一個文本或進入通知中心或離開應用一個函數稱爲pauseGame()應該運行,它的第一次正常工作,但如果有人被接收另一個文本或再次進入通知中心,那麼應用程序將崩潰,我的問題是爲什麼它第一次工作,但第二次崩潰?我認爲這與定時器有關,但是請你看看我的代碼,我一直在用谷歌搜索,這是我的最後一招!謝謝你們!! (另外這個代碼適用於iOS 9罰款和屢試不爽,但問題是在iOS 8)遊戲崩潰後applicationWillResignActive被調用不止一次

這裏是調用的函數,當用戶進入後臺

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 
      } 
     } 

回答

1

我懷疑你的問題可能在你的GameScene。如果您註冊GameScene以接收NSNotifications,則還需要刪除註冊,因爲多個註冊可能導致崩潰。

嘗試在willMoveFromView:(SKView *)view方法中包括[[NSNotificationCenter defaultCenter] removeObserver:self];

0

是的!這工作我不得不改變它迅速這是

NSNotificationCenter.defaultCenter().removeObserver(self) 

但它解決了我的崩潰!非常感謝你,我一直堅持這幾天:) < 3大上你先生!

相關問題