Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by BennyT

Hi all,

Had a look through the forums and couldn't specifically find anything on this.

Imagine a racing game where the level is the track and I want to time how long it takes to complete. Since the time to complete is unknown, using the Impact Timers is out.

I am looking for a best practice solution that isn't going to bog down the game.

My first thought is to use some native JavaScript and a bit of date/time maths.

So when the level loads I was going to initiate:

var startTime = Date.now();

and then have a trigger at the end of the track that runs the same line:

var finishTime = Date.now();

and then do the maths to get the minutes and seconds.

If anyone has a better why to do it, please let me know

Thanks!

1 decade ago by Joncom

Why not use ig.Timer?

/* init */
this.timer = new ig.Timer();

/* on-finish */
this.timer.pause();
var timeElapsed = -this.timer.delta();
alert('Your time: ' + timeElapsed);

1 decade ago by BennyT

Hi Joncom,

The reason why I thought I shouldn't use ig.Timer() is based what is mentioned in the documentation:

All timers are only updated once for each frame. So calling .delta() on one timer several times during one frame, will result in the exact same return value.


I don't really need it to check the time everyframe, I just need to know start and end times.

So if I use the Date.now() function, it just grabs the timestamps when I need them.

Does that make sense? Or should I really be using ig.Timer()?

1 decade ago by Joncom

Depends how competitive is your game going to be.
This game uses ig.Timer to record best times.
Seem too slow, or good enough? Your call.

1 decade ago by dominic

With the way ig.Timer is implemented it doesn't actually matter how many timers you have created. They all reference a global "current time" value that is updated once per frame. A timer instance internally only holds a start time and when queried (i.e. with .delta()) calculate the time that passed since creation or reset.

And yes, you should use an ig.Timer as Joncom proposed as it is in sync with the game time. If the game slows down because the browser is busy doing something else, you car will move slower and so should the timer.

In practice this shouldn't matter much, as there are very few browsers that can't keep up at least 20fps. So "real time" and "game time" should be the same in most cases, but it's still a good idea to use the "game time" - just in case :)

1 decade ago by BennyT

Ok thanks Joncom and dominic for your feedback and clarification, will make the switch to use the ig.Timer
Page 1 of 1
« first « previous next › last »