当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重启NSTimer.scheduledTimerWithTimeInterval?

时间:2015-10-10 10:19:11

标签: ios swift sprite-kit

我是swift& amp;的新手spritekit并想知道如何检测主页按钮是否被按下?

主要问题是我NSTimer.scheduledTimerWithTimeInterval正在didMoveToView生成多个角色,一切进展顺利,但是当我用主页按钮暂停游戏并在几秒钟后重启游戏时,人物涌出很多。我假设NSTimer没有被暂停,因此,重新启动应用程序时会涌入很多字符。我想知道一个简单的解决方案和示例,当按下主页按钮时我将如何暂停,并在应用程序重新启动时恢复?我很乐意来到你这里!

  testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,
            target: self,
            selector: "timerUpdate",
            userInfo: nil,
            repeats: true)

///这是用于在didMoveToView

中生成字符的项目代码的一部分
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appEnterIntoBackGround:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appBecomeActive:"), name:UIApplicationWillEnterForegroundNotification, object: nil)

    //start the timer and calls the timerUpdate method every 1.0 seconds


    myTimer = NSTimer.scheduledTimerWithTimeInterval(1.0,
        target: self,
        selector: "timerUpdate",
        userInfo: nil,
        repeats: true)

  if(skview.level == 3) {

    myTimer2 = NSTimer.scheduledTimerWithTimeInterval(1.0,
        target: self,
        selector: "timerUpdate",
        userInfo: nil,
        repeats: true)

    }

    testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,
        target: self,
        selector: "timerUpdate",
        userInfo: nil,
        repeats: true)


}

func appEnterIntoBackGround(notification : NSNotification) {
    let skview = self.view as! GameSKView!
    print("App is in Background")
    testEnemy.invalidate()  //remove timer here when add enter into background.
    if(skview.level == 3) {
    myTimer2.invalidate()
    }
    myTimer.invalidate()
}

func appBecomeActive(notification : NSNotification) {
     let skview = self.view as! GameSKView!
   // print("App is active now")
    //add your timer again when app enter into foreground
    testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,target: self,selector: "timerUpdate",userInfo: nil,repeats: true)

    if(skview.level == 3) {

        myTimer2 = NSTimer.scheduledTimerWithTimeInterval(1.0,
            target: self,
            selector: "timerUpdate",
            userInfo: nil,
            repeats: true)

    }

    testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,
        target: self,
        selector: "timerUpdate",
        userInfo: nil,
        repeats: true)

}

1 个答案:

答案 0 :(得分:2)

您可以使用NSNotificationCenter,当您的应用进入后台或前景时会触发通知,如下面的代码所示:

var testEnemy : NSTimer?

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appEnterIntoBackGround:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appBecomeActive:"), name:UIApplicationWillEnterForegroundNotification, object: nil)

    testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,
        target: self,
        selector: "timerUpdate",
        userInfo: nil,
        repeats: true)
}

以下是辅助方法:

func timerUpdate() {
    print("called")
}

func appEnterIntoBackGround(notification : NSNotification) {
    print("App is in Background")
    testEnemy!.invalidate()  //remove timer here when add enter into background.
}

func appBecomeActive(notification : NSNotification) {

    print("App is active now")
    //add your timer again when app enter into foreground
    testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,target: self,selector: "timerUpdate",userInfo: nil,repeats: true) 

}