简体   繁体   中英

Pausing timers when home button is pressed swift

I've got a bunch of timers in my GameScene.swift that call functions. How can I pause these timers when the user taps or double taps the home button, gets a text message etc? Right now when the home button is double tapped while the game is running the score which is based on a timer keeps going up and enemies are still spawned.

    //Spawn timer for enemy blocks
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("spawnEnemies"), userInfo: nil, repeats: true)

    //Timer for keeping score
    var scoretimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("scoreCounter"), userInfo: nil, repeats: true)

Any help would be greatly appreciated!

EDIT: I added these two lines to my GameViewController.swift

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseTimers:"), name:UIApplicationWillResignActiveNotification, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("startTimers:"), name:UIApplicationDidBecomeActiveNotification, object: nil)

And then these two functions:

func pauseTimers(notification : NSNotification) {
    println("Observer method called")
    timer.invalidate()
    scoretimer.invalidate()
}

func startTimers(notification : NSNotification) {
    println("Observer method called")
    timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("spawnEnemies"), userInfo: nil, repeats: true)
    scoretimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("scoreCounter"), userInfo: nil, repeats: true)
}

However, I am getting an error because when I recreate the timers in startTimers, their selectors are functions in my GameScene.swift, not GameViewController.swift. How can I fix this?

You can stop a timer by calling invalidate() on it. Though an invalidated timer will never run again, you need to re-create it later on if you want it to fire again.

To detect when your app goes in background or is interrupted and also when it comes back to foreground, you can listen for these notifications:

  • UIApplicationDidBecomeActiveNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification

An app "resigns active" when it is covered, eg because an incoming call is signaled or because the notification center/control center is opened. An app goes into background, when another app goes into foreground or when the home screen is shown (which is technically also an app).

在此处输入图片说明

@Update

The problem is that the target of the timers is self and when you are in GameViewController , then self is the GameViewController instance. You either need to recreate the timers within GameScene or you need to pass a reference to your GameScene instance instead of self as the target . The selector is the method that is called and the target is the object on that the method is called and self is always the current object.

BTW it's possible to listen to these notifications in any class and also in multiple classes at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM