Time for dummies?

Hello there.
I’m making a “racing” 2D game and I want to display the time of the current lap.
The point is that I haven’t found the simplest way for doing this. I’ve read some topics here and there, but all of them are too hard to understand. I just want a timer which starts at 0 and counts seconds, restarting it after each lap.

I thought I could make an update function each second, but it is probably not the best way of implementing this in terms of performance. Besides that, I guess it can be inaccurate.

Is there any simple guide for Cocos2d-x time? Should I use the time class inherit from C++ or something from Cocos2d-x?

Thanks in advance.

1 Like

std::time, std::chrono: http://en.cppreference.com/w/cpp/chrono/c/time

We will create a tutorial for you by Friday.

1 Like

I have used a character map label and an update method for this in cocos2d-iphone and in cocos2d-x and the performance impact is minimal, especially compared to the CPU’s the physics engine uses.

You can accumulate the delta in the update method to track race time, or you could do even use std::time if you want more control and accuracy.

You could change the update rate to be something less than the frame rate if you like, but having hundredths of seconds screaming by is a nice effect in itself for a racing game.

std::chrono:
i think this will work

Video and written tutorial for a lap timer is now available :smiley:
Video: http://bit.ly/1cAwTh2
Written: http://bit.ly/1cAxjnv

1 Like