0

I have the following code:

-(void)update:(ccTime)delta {
    totalTime += delta * 10;
    currentTime = (int)totalTime;
    if (myTime < currentTime) {
    myTime = currentTime;
        [timeLabel setString:[NSString stringWithFormat:@"Time: \n%i:%02i:%02i.%i", myTime/36000,(myTime/600)%60,(myTime/10)%60,myTime%10]];
    }

}

This code is working good, is there a way to add milliseconds though? And if so, could I have it to the thousandths place: 1:23:45.678

  • 2
    possible duplicate of [Getting current time in milliseconds Cocos2d](http://stackoverflow.com/questions/7027896/getting-current-time-in-milliseconds-cocos2d) – lucianomarisi May 20 '14 at 23:22
  • I'm not sure exactly how that works though, will that work with the code I have? One answer said that delta (without multiplying by 10) was milliseconds, and I'm pretty sure that's not the case. Do you know is there a way to do it by multiplying or dividing "myTime' by something? – MRRPlatinum May 20 '14 at 23:27

1 Answers1

0

There's no need to use Cocoas2d for this, just use standard Cocoa.

@property CFTimeInterval startTime; // put this in your *.h file

-(void)update:(ccTime)delta {

    if (fabs(self.startTime - 0) < 0.1) { // if startTime is close to 0, set it to the current time (cannot do == comparisons with a CFTimeInterval)
      self.startTime = CFAbsoluteTimeGetCurrent();
    }

    CFTimeInterval timeSinceStart = CFAbsoluteTimeGetCurrent() - self.startTime;
    int hours = floor(timeSinceStart / 3600);
    int minutes = floor(timeSinceStart / 60) - (hours * 60);
    double seconds = timeSinceStart - (hours * 3600) - (minutes * 60);

    NSLog(@"%02i:%02i:%06.3f", hours, minutes, seconds);

    [timeLabel setString:[NSString stringWithFormat:@"Time: \n%02i:%02i:%06.3f", hours, minutes, seconds]];
}
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110