ios8 Swift SpriteKit - 在swift中暂停和恢复NSTimers [英] ios8 Swift SpriteKit - Pause and Resume NSTimers in swift

查看:131
本文介绍了ios8 Swift SpriteKit - 在swift中暂停和恢复NSTimers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上搜索了很多次,却找不到这个问题的答案。我知道如何使用invalidate函数暂停和恢复NSTimers - timer.invalidate。而且我知道如何恢复他们。但我有一个SpriteKit游戏。当我暂停游戏时,我会阻止所有内容和计时器。我知道我可以使用.invalidate来阻止它们,但是当我使它们无效时:

I have searched many times on the internet but could not find the answer to this question. I know how to pause and resume NSTimers by using the invalidate functions - timer.invalidate. and I know how to resume them. But I have a SpriteKit game. When I pause my game, I stop everything and the timers. I know that I can stop them using .invalidate but when I invalidate them:

例如,假设我有一个连续运行的5秒计时器,它会生成一个块。

For example lets say I have a 5 second timer that runs continously that spawns one block.

当计时器到达周期的第二个3时,我暂停游戏,并使计时器无效。当我恢复时,现在计时器第二次回到0,我必须等待另外5秒。我希望它从它停止的地方继续,3,并等待2秒钟,以便块生成。

When the timer reaches second 3 of the cycle and when I paused the game, and invalidate the timers. When I resume, Now the timers second goes back to 0 and I must wait another 5 seconds. I want it to continue from where it left off, 3 , and wait 2 seconds for the block to spawn.

            blockGenerator.generationTimer?.invalidate()

            self.isGamePaused = true
            self.addChild(self.pauseText)

            self.runAction(SKAction.runBlock(self.pauseGame))

e`

当我恢复时:

blockGenerator.generationTimer = ...

我要再等5秒,我想要计时器从它停止的地方继续

I have to wait another 5 seconds, I want the timer to continue from where it left off

如果你可以帮助我,我感谢你,谢谢。

If you can help me, I appreciate it thank you.

推荐答案

有一种方法可以暂停/恢复NSTimer实例,因为使用重复计时器我们知道下一个开火日期

There is a way to pause/resume NSTimer instances, because using repeating timers we know the next fire date.

这是一个简单的类计时器和一个协议 TimerDelegate

This is a simple class Timer and a protocol TimerDelegate

协议TimerDelegate

protocol TimerDelegate {
  func timerWillStart(timer : Timer)
  func timerDidFire(timer : Timer)
  func timerDidPause(timer : Timer)
  func timerWillResume(timer : Timer)
  func timerDidStop(timer : Timer)
}

课程计时器

class Timer : NSObject {

  var timer : NSTimer!
  var interval : NSTimeInterval
  var difference : NSTimeInterval = 0.0
  var delegate : TimerDelegate?

  init(interval: NSTimeInterval, delegate: TimerDelegate?)
  {
    self.interval = interval
    self.delegate = delegate
  }

  func start(aTimer : NSTimer?)
  {
    if aTimer != nil { fire() }
    if timer == nil {
      delegate?.timerWillStart(self)
      timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "fire", userInfo: nil, repeats: true)
    }
  }

  func pause()
  {
    if timer != nil {
      difference = timer.fireDate.timeIntervalSinceDate(NSDate())
      timer.invalidate()
      timer = nil
      delegate?.timerDidPause(self)
    }
  }

  func resume()
  {
    if timer == nil {
      delegate?.timerWillResume(self)
      if difference == 0.0 {
        start(nil)
      } else {
        NSTimer.scheduledTimerWithTimeInterval(difference, target: self, selector: "start:", userInfo: nil, repeats: false)
        difference = 0.0
      }
    }
  }

  func stop()
  {
    if timer != nil {
      difference = 0.0
      timer.invalidate()
      timer = nil
      delegate?.timerDidStop(self)
    }
  }

  func fire()
  {
    delegate?.timerDidFire(self)
  }






使您的类符合协议 TimerDelegate 并初始化计时器实例

var timer : Timer!
timer = Timer(interval: 5.0, delegate: self)






方法

start()调用委托方法 timerWillStart 并启动计时器。

start() calls the delegate method timerWillStart and starts the timer.

pause()保存当前日期和下一个开火日期之间的差异,使计时器无效并调用委托方法 timerDidPause

pause() saves the difference between the current date and the next fire date, invalidates the timer and calls the delegate method timerDidPause.

resume()调用委托方法 timerWillResume ,使用保存的差异时间间隔创建临时一次性计时器。当此计时器触发时,主计时器将重新启动。

resume() calls the delegate method timerWillResume, creates a temporary one shot timer with the saved difference time interval. When this timer fires the main timer will be restarted.

stop()调用委托方法 timerDidStop 并使计时器无效。

stop() calls the delegate method timerDidStop and invalidates the timer.

当计时器触发时,委托方法 timerDidFire 被叫。

When the timer fires, the delegate method timerDidFire is called.

这篇关于ios8 Swift SpriteKit - 在swift中暂停和恢复NSTimers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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