How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed?

三世轮回 提交于 2019-12-06 16:02:02

问题


I'm new to swift & spritekit and wanted to know how is it possible to detect if the home button is pressed ?

The main problem is that I have NSTimer.scheduledTimerWithTimeInterval running to generate multiple characters in didMoveToView, and everything is going well, but when I pause the game with home button and restart the game a few seconds later, the characters floods out alot. I assume that the NSTimer had not been paused , therefore, flooding alot of characters when relaunching the app. I would want to know a simple solution and example, how will I pause when home button is pressed, and resuming when the app relaunches ? I would love to here from you!

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

///This is the part of the project code for generating the characters within the 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:


You can use NSNotificationCenter for that will fire a notification when you app enter into background or foreground as shown into below code:

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

And below is the helper methods:

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) 

}


来源:https://stackoverflow.com/questions/33052867/how-to-pause-and-restart-nstimer-scheduledtimerwithtimeinterval-in-spritekit-sw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!