2011-08-11 95 views
4

我试过谷歌周围,但我仍然无法找到最佳答案。以毫秒为单位获取当前时间Cocos2d

我想要的只是非常简单,我只想以毫秒为单位获取当前时间。

我该怎么做在cocos2d?

+1

我认为这已经是光盘在这里使用: http://stackoverflow.com/questions/889380/how-can-i-get-a-precise-time-for-example-in-milliseconds-in-objective-c – 2011-08-11 15:14:25

+1

或在这里:http: //stackoverflow.com/questions/358207/iphone-how-to-get-current-milliseconds –

回答

1

为什么不将当前时间转换成一个浮点值,然后乘以10^3,当前的时间将它转换成毫秒

+0

Ermm?我正在寻找游戏时间... –

4

首先,类变量:

CGFloat gameTime; 

然后,在你的班级初始化:

[self scheduleUpdate]; 

最后,虽然还在你的班级:

- (void) update:(ccTime)delta { 
    gameTime += delta; 
} 

delta是自上次调用更新以来的毫秒数。将游戏时间的某个地方保存在数据库中,用于终生gameTime。

0

为了得到仅为几毫秒

NSDate* date = [NSDate date]; 
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 
[formatter setDateFormat:@"ss"]; 

NSString* str = [formatter stringFromDate:date]; 

float seconds = str.intValue; 

float miliSeconds = seconds/1000; // HERE is your answer 

如果你想获得全职格式取代

[formatter setDateFormat:@"hh:mm:ss"]; 
0

以当前时间以秒为单位乘以1000去拿毫秒数:

double ms = CFAbsoluteTimeGetCurrent() * 1000.0;  
2
try this 

time_t rawtime; 
struct tm * timeinfo; 
time (&rawtime); 
timeinfo = localtime (&rawtime); 

CCLog("year------->%04d",timeinfo->tm_year+1900); 
CCLog("month------->%02d",timeinfo->tm_mon+1); 
CCLog("day------->%02d",timeinfo->tm_mday); 

CCLog("hour------->%02d",timeinfo->tm_hour); 
CCLog("mintus------->%02d",timeinfo->tm_min); 
CCLog("seconds------->%02d",timeinfo->tm_sec); 
相关问题