I've tried to google around but I still can't find the best answer.
All I want 开发者_如何学Cis very simple, I just want to get the current time in milliseconds.
How can I do that in cocos2d?
First, a class variable:
CGFloat gameTime;
Then in your class initialize:
[self scheduleUpdate];
Finally, while still in your class:
- (void) update:(ccTime)delta {
gameTime += delta;
}
delta is the milliseconds since the last call of update. Save gameTime somewhere in a database for lifetime gameTime.
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);
Why not convert the current time into a float value, then multiply the current time by 10^3 to convert it into milliseconds
To get only milliseconds
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
If you want to get full time format replace
[formatter setDateFormat:@"hh:mm:ss"];
Take the current time in seconds and multiply by 1000 go get number of milliseconds:
double ms = CFAbsoluteTimeGetCurrent() * 1000.0;
精彩评论