当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重启NSTimer.scheduledTimerWithTimeInterval? [英] How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed?

查看:141
本文介绍了当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重启NSTimer.scheduledTimerWithTimeInterval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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)

///这是用于在didMoveToView中生成字符的项目代码的一部分

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

}


推荐答案

您可以使用 NSNotificationCenter

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

以下是帮助方法:

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) 

}

这篇关于当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重启NSTimer.scheduledTimerWithTimeInterval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆