-1

Using SpriteKit and Timers in Swift, I am trying to create a feature that will pause the game, and resume with the correct time. I found an awesome source "Pausing" the Game in Swift that showed a system allowing just that. In my code I have the function pauseGame that should allow me to get the current time that the timers were invalidated at

func pauseGame(){
    enemyTimer.invalidate()
    enemyTimer2.invalidate()
    changeSpeed.invalidate()
    rubyTimer.invalidate()

    // 0.3 is the original delay time when the timers were created

    let calendar = Calendar.current
    let timeCaptured = calendar.date(byAdding: .nanosecond, value: Int(Int64(0.3 * Double(NSEC_PER_SEC))), to: Date())!
    let elapsedTime = timeCaptured.timeIntervalSince(Date)
    let remainingDelay = 0.3 - elapsedTime

}

All I have to do is create new timers with the value of remainingDelay, however I get an error on

    let elapsedTime = timeCaptured.timeIntervalSince(Date)

saying "Cannot convert value of type '(Date).Type' to expected argument type 'Date'"

Any ideas? Thanks for looking into it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

Date is a class, Date() returns an instance of Date initialised to "now", you want:

let elapsedTime = timeCaptured.timeIntervalSince(Date())
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 1
    Or simply `let elapsedTime = timeCaptured.timeIntervalSinceNow`. – rmaddy Jun 10 '18 at 02:44
  • Wow can't believe it was that simple! Thanks for the quick response. – Blake Olson Jun 10 '18 at 02:45
  • But this can't be right. `timeCaptured` is "now" + a few nanoseconds so calculating `elapsedTime` from "now" is just the value added to "now" on the previous line. – rmaddy Jun 10 '18 at 02:46
  • I see, you could also use timeCaptured.timeIntervalSinceNow as it is almost the same thing. Thanks for sharing – Blake Olson Jun 10 '18 at 02:53