user3667192
user3667192

Reputation: 103

Displaying Game Time in Milliseconds Xcode

So I am fooling around with some apps and I am trying to get a game timer to be in milliseconds instead of just the normal 1,2,3. I want it to be in a format similar to this 1.000,2.000. Or even 1.00,2.00.

Here is my code:

(void)startTimer {
    if (count == 30) {
      // 3
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target: self
                                           selector:@selector(ElapsedTime)
                                           userInfo:nil
                                            repeats:YES];

Upvotes: 1

Views: 749

Answers (1)

Duncan C
Duncan C

Reputation: 131491

Record the start time with

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

Any time you want to see how much time has elapsed, use:

NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - start;

elapsed will be in seconds. If you want milliseconds, subtract the whole number to get the decimal part. Multiply the decimal part by 1000 to get milliseconds.

If you want your time to change more often than once a second, make the time interval of your timer less than 1.0 seconds. Note that timers aren't accurate for more than about 1/50 of a second however.

Upvotes: 2

Related Questions