暂停和重新创建带有通知问题的计时器 Swift [英] Pausing and Recreating timers with notifications issue Swift

查看:53
本文介绍了暂停和重新创建带有通知问题的计时器 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个计时器,一个增加分数,一个生成敌人.我使用通知使计时器无效,然后我使用另一个通知来重新创建计时器.当我退出然后打开应用程序时,会生成两组敌人.我认为 timerRecreate = true 以及 GameScene 中的常规计时器也被调用.

So I've got two timers, one that increases the score and one that spawns enemies. I used a notification to invalidate the timers, and then I'm using another one to recreate the timers. When I quit and then open the app, there are two sets of enemies being spawned on top of each other. I think timerRecreate = true and also the regular timers in GameScene are also being called.

GameViewController.swift 文件:

GameViewController.swift file:

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

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

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("startTimers:"), name:UIApplicationDidBecomeActiveNotification, object: nil)
}
func pauseTimers(notification : NSNotification) {
    println("Observer method called")
    timer.invalidate()
    scoretimer.invalidate()
}

func startTimers(notification : NSNotification) {
    println("Observer method called")
    timerRecreate = true
}

GameScene.swift 中的计时器代码

Code for timers in GameScene.swift

override func didMoveToView(view: SKView) {
    //Spawn timer for enemy blocks
    timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("spawnEnemies"), userInfo: nil, repeats: true)

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

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

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

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

我认为问题是当您最初打开应用程序时,无论是退出应用程序还是第一次打开应用程序后,timerRecreate 都设置为 true 以及块的定期生成,因此生成了两组块同时.我该如何解决这个问题?

I think the problem is when you initially open the app, be that after quitting out of it or opening it for the first time, timerRecreate is set to true as well as the regular spawning of blocks so two sets of blocks are spawned at the same time. How can I fix this?

推荐答案

已修复!我希望...

无论如何,这就是我所做的.我创建了另一个名为 timerz 的布尔值,并在发出 DidBecomeActive 通知时将其设置为 false.同时,timeRecreate 设置为true.这保证了两个计时器集不会同时运行.在有关 timRecreate 是否为真的 if 语句中的更新函数中,我将 timeRecreate 设置为 false,将 timerz 设置为 true.所以计时器被重新创建,然后它切换回产生它们的旧方式.我还把这个产生它们的旧方法放在一个关于 timerz 是真还是假的 if 语句中.

Anyway heres what I did. I created another boolean called timerz and set it to false when the DidBecomeActive notification was made. At the same time, timeRecreate is set to true. This guarantees that both timer sets arent running at the same time. In the update function inside the if statement on whether timRecreate was true, I set timeRecreate to false and timerz to true. So the timers are recreated and then it switches back to to old way of spawning them. I also put this old method of spawning them inside an if statement on whether timerz was true or false.

这篇关于暂停和重新创建带有通知问题的计时器 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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