1

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)

}
Jennifer
  • 1,822
  • 2
  • 20
  • 45
  • 1
    That is why SKAction is preferred way over the NSTimer in this situation (NSTimer is not affected if view or scene are paused). Read more here : http://stackoverflow.com/q/23978209/3402095 – Whirlwind Oct 10 '15 at 12:40
  • Never thought of it! Thank you for the info! It really helps! – Jennifer Oct 10 '15 at 14:11

1 Answers1

2

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) 

}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Thank you Dharmesh for the response! But Seems like I'm getting a error App is in Background fatal error: unexpectedly found nil while unwrapping an Optional value. And also I find the characters still flooding out when restarting the app. Should I set some code in the AppDelegate ? – Jennifer Oct 10 '15 at 11:14
  • No you don't need to add anything into app delegate. Can you share any sample project? – Dharmesh Kheni Oct 10 '15 at 11:16
  • Or you can check this sample project: http://s000.tinyupload.com/?file_id=65262380109658284634 – Dharmesh Kheni Oct 10 '15 at 11:20
  • I have no clue since, unlike android studio, xcode doesn't show me the location of the error – Jennifer Oct 10 '15 at 11:25