iosswiftsprite-kitelapsedtime

Carrying Elapsed Time over to another ViewController


I have a small elapsed timer in my game and it works very well. However I am trying to figure out how to save the elapsed time when you die so I can carry it over to the Game Over Screen where the Score and High Score is displayed.

I tired a few things but none of them seem to work. I guess it's because the time is not being saved anywhere when the it's game over, but rather just reset to 00:00:00 when the game restarts.

I use two view Controllers for this timer. One is called Stopwatch the other code is in the GameScene. Here are the codes.

I wanna bring it into a label like for example:

    let timeLabel = SKLabelNode(fontNamed: "Planer")
    timeLabel.text = "Time: \(savedTimer)"
    timeLabel.fontSize = 100
    timeLabel.fontColor = SKColor.white
    timeLabel.zPosition = 2
    timeLabel.position = CGPoint (x: self.size.width/2, y: self.size.height * 0.5)
    self.addChild(timeLabel)*/

Stopwatch.swift code

import Foundation

class Stopwatch {

private var startTime : Date?

var elapsedTime: TimeInterval {
    if let startTime = self.startTime {
        return -startTime.timeIntervalSinceNow
    } else {
        return 0
    }
}
var isRunning: Bool {
    return startTime != nil
}

func start() {
    startTime = Date()
}

func stop() {
    startTime = nil
}
}

And the code I got speed out through my Game Scene:

import UIKit

class ViewController: UIViewController {

private let formatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "mm:ss:SS"
    return formatter
}()

let watch = Stopwatch()

@IBOutlet weak var elapsedTimeLabel: UILabel!

@IBAction func startButton(_ sender: Any) {
    Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.updateElapsedTimeLabel), userInfo: nil, repeats: true)

    watch.start()
}

@IBAction func stopButton(_ sender: Any) {
    watch.stop()
}

@objc func updateElapsedTimeLabel (timer : Timer) {
    if watch.isRunning {
        elapsedTimeLabel.text = formatter.string(from: Date(timeIntervalSince1970: watch.elapsedTime))
    } else {
        timer.invalidate()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override var prefersStatusBarHidden: Bool {
    return true
}
}

Solution

  • What I understand is that you're trying to save the elapsedTime of your watch after the user taps the stop button. If that's the case, in your stopButton function you are calling watch.stop(), which in turn resets the startTime = nil. So you might want to edit it like so:

    // Create a new class variable to store the time
    var savedTime: TimeInterval? = nil
    
    @IBAction func stopButton(_ sender: Any) {
        savedTime = watch.elapsedTime
        //    Use the savedTime here to pass to the game over function
        watch.stop()
    }
    

    If you don't need to save the time in your ViewController class, you can move the savedTime variable to a local one in the stopButton function.