Introduction
When
programming a game you'll depend on timers heavily. Whether you have a damage
effect, that needs to be calculated every x seconds, or a visual special effect
to be controlled, you'll steer all that by adding time to variable and checking
it against a limit.
Like
with every work that gets repetitive, you'll get sloppy and you'll make
mistakes.
And
it's precicely for this reason that I wrote this class.
Description
It
should have a constructor where you would have to declare the timeout in
milliseconds you want to measure.
Then
you would have to update it every game-cycle, giving it the current gameTime
object. Its update method will do the appropriate math with the appropriate
values hidden in the gameTime object and return true, if the timeout has been
reached.
It
has some convenience-properties, which you may use to determine the percentage
of time passed, to determine the time that is still left, or to reset or change
the interval itself.
You
may use a defaulted field in the update method, if you wish to add the excess
time, that was not used after "triggering" the timer, to the next run
of the timer (in case you have some sort of a cyclic timer because otherwise
you would just "lose" this excess time).
References
- You may download the code here: IntervalTimer.zip
No comments:
Post a Comment