Getting current time in milliseconds Cocos2d

后端 未结 5 2009
误落风尘
误落风尘 2021-01-06 08:32

I\'ve tried to google around but I still can\'t find the best answer.

All I want is very simple, I just want to get the current time in milliseconds.

How can

相关标签:
5条回答
  • 2021-01-06 08:57

    Take the current time in seconds and multiply by 1000 go get number of milliseconds:

    double ms = CFAbsoluteTimeGetCurrent() * 1000.0;    
    
    0 讨论(0)
  • 2021-01-06 08:58
    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);
    
    0 讨论(0)
  • 2021-01-06 08:59

    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"];
    
    0 讨论(0)
  • 2021-01-06 09:07

    Why not convert the current time into a float value, then multiply the current time by 10^3 to convert it into milliseconds

    0 讨论(0)
  • 2021-01-06 09:09

    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.

    0 讨论(0)
提交回复
热议问题