I want to change the timer every millisecond but it doesnt work as expected.
NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("advanceTimer:"), userInfo: nil, repeats: true);
func advanceTimer(timer: NSTimer){
self.time += 0.001;
let milliseconds = self.time * 100;
let remaingMilliseconds = Int((milliseconds % 1000) / 10);
let seconds = Int((milliseconds / 1000) % 60)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", remaingMilliseconds)
timerText.text = "\(strSeconds):\(strFraction)";
}
The result is
The timer change up to 100 in the millisecond part (00:100) and then change to 01:00 = 40 real secs
Duncan approach:
var time: NSTimeInterval = 0;
var startTime: NSTimeInterval = 0;
//And your timer method...
func advanceTimer(timer: NSTimer){
//Total time since timer started, in seconds
self.time = NSDate.timeIntervalSinceReferenceDate() - startTime
println(self.time);
//The rest of your code goes here
}
override func didMoveToView(view: SKView) {
// Set anchor point
self.anchorPoint = CGPointMake(0.5, 0.5);
var startTime: NSTimeInterval
//Sart the timer
startTime = NSDate.timeIntervalSinceReferenceDate()
NSTimer.scheduledTimerWithTimeInterval(0.02,
target: self,
selector: Selector("advanceTimer:"),
userInfo: nil,
repeats: true)
}
Result: 456680125.54539 the first print