ios - 多次调用 applicationWillResignActive 后游戏崩溃

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

最佳答案

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

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

关于ios - 多次调用 applicationWillResignActive 后游戏崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158968/

相关文章:

ios - XCode 7.01 创建文件时崩溃

ios - 'NSInvalidArgumentException',原因 : '*** setObjectForKey: object cannot be nil (key: device_uid)'

ios - 设备时间格式设置为24小时时获取零日期

ios - 如何使用内置麦克风输入和蓝牙输出

swift - osx Metal 不渲染

swift - UITextField 可复制但不可编辑 Swift

android - 在 Android 5.0 中查询从联系人中获取电子邮件?崩溃

ios - 如果用户拒绝相机访问,则无法显示警报

ios - Xamarin 调试器无法连接到 iPad

ios - Phonegap 构建命令后 Cordova 插件未传输到 iOS