[英]How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed?
I'm new to swift & spritekit and wanted to know how is it possible to detect if the home button is pressed ? 我是swift&spritekit的新手,想知道如何检测是否按下主屏幕按钮?
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. 主要问题是我正在运行NSTimer.scheduledTimerWithTimeInterval
来在didMoveToView
生成多个角色,并且一切进行得很好,但是当我使用主页按钮暂停游戏并在几秒钟后重新启动游戏时,角色大量涌入。 I assume that the NSTimer
had not been paused , therefore, flooding alot of characters when relaunching the app. 我认为NSTimer
尚未暂停,因此,在重新启动应用程序时会淹没大量字符。 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 ///这是项目代码的一部分,用于在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)
}
You can use NSNotificationCenter
for that will fire a notification when you app enter into background or foreground as shown into below code: 您可以使用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)
}
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.