More Timer/Interpolator Tweaks

December 30th, 2009 | 2 comments

The more I work with these two classes, the more I find needs improvements. Today I decided to address a few issues:

Issue: All Interpolators and all Timers went into one big bucket (per type). This made it hard or impossible to conditionally update certain interpolators or timers. For example, perhaps your game screen is interpolating something but you pause the game. But your pause menu also needs an interpolator. The old class had no way of supporting this since it was one big static bucket.

Solution: Implement new InterpolatorCollection and TimerCollection classes. Then each screen can have its own set of timers and interpolators if desired.

Issue: Once I have these collections, sometimes I have a class that just needs a single interpolator or timer. Creating that collection adds overhead that might not be needed.

Solution: I’ve made a parameterless internal constructor for the collection classes to be able to utilize but I’ve now added public constructors that allow single timers or interpolators to be created. In addition, I’ve moved the update logic of the interpolators and timers into their classes and made that public to allow for updating these individual items. Lastly, I added an IsActive property to the class so users can tell when the interpolator or timer is invalid.

Issue: Let’s say you’re using a timer or interpolator for your splash screen but want users to be able to skip it. The previous system had no way of doing this in a nice way. You could call Stop on the interpolator or timer and then invoke it’s callback manually, but that’s a bit of a pain.

Solution: I’ve added a ForceFinish method to Interpolator that updates the interpolator to its end value and calls both the step and completed callbacks before flagging itself as invalidated, just as it would if it finished normally. I’ve also added a ForceTick method to Timer that forces the tick callback to be invoked as well as resetting the timer to 0 and possibly invalidating the timer if it is not set to repeat.

With those changes in place, both classes have become that much more helpful in various places and situations. You can grab all of the necessary files to use these classes in this one convenient zip file: TimerInterpolator.zip.


Possibly Related Posts

(Automatically Generated)
Interpolation: Polish's Best Friend
Using interpolators and timers
Borrowing From The iPhone SDK
Advancing Timing and Interpolation
Advancing Timing and Interpolation

  1. January 26th, 2010 at 10:26

    hi
    thanks for the update, now its a better and more useable system. i used a mod of your system in a project before and ran in some of this issues. i did not solve them as well as you did (i say time pressure).

    but shouldnt the timer.stop method look like this?

    public void Stop()
    {
    valid = false;
    tick = null;
    Tag = null;
    }

    to avoid referencing classes. i mean, when the timer is stopped, nothing can start it again so its useless. we can as well kill all references cant we?

  2. January 31st, 2010 at 10:45

    That’s a good point. :)

You must be logged in to post a comment.