Cocos2d中的毫秒计时器

时间:2014-05-20 22:13:58

标签: objective-c cocos2d-iphone

我有以下代码:

-(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]];
    }

}

这段代码运行良好,有没有办法增加毫秒数?如果是这样的话,我可以把它带到千分之一的地方:1:23:45.678

1 个答案:

答案 0 :(得分:0)

没有必要使用Cocoas2d,只需使用标准的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]];
}