Using Impact Timers

Impact Timers
One of the great unsung features of Impact is timers. How can something so apparently dull be so great? Well, having things occur at certain times or intervals is essential in video games. Examples would be making items disappear after a specified period of time - say a magic wand that disappears from the screen with a poof 3 seconds after it is used. Or making new entities appear at certain intervals - say goblins arising out of a cursed box every 5 seconds, and so forth. Impact makes it really easy to do this with the built in timers. Getting a handle on how they work is the trick.
Creating Timers (clearing up confusion)
How to create a new timer:
The best way to set a timer that will always be used the same way is to create a new timer in the init function:
this.timer = new ig.Timer(0.5);
Will set a 0.5 second timer. Below is a real example of this in use for an explosion entity from the YOSS game.
ig.module( 'game.entities.explosion' ) .requires( 'impact.entity' ) .defines(function(){ EntityExplosion = ig.Entity.extend({ size: {x: 48, y: 48}, type: ig.Entity.TYPE.NONE, collides: ig.Entity.COLLIDES.NEVER, animSheet: new ig.AnimationSheet( 'media/Explode5-m.png', 48, 48), init: function( x, y, settings ) { //Received the coordinates of the center of the creating entity. //Subtract half of the size of the explosion from these numbers //in order to center the explosion on top of the entity. x = x - (this.size.x/2); y = y - (this.size.y/2); this.parent( x, y, settings ); //We only want the explosion animation to loop once so stop is true. this.addAnim( 'idle', 0.05, [0,1,2,3,4,5,6,7], true ); //Start timer so this entity can be killed after 0.5 seconds. this.timer = new ig.Timer(0.5); }, update: function(){ //If it has been more than 0.5 seconds then kill this explosion entity. if (this.timer.delta() > 0) { this.kill(); } this.parent(); }, }); });
As you can see, as soon as the explosion is initiated, a timer is set at 0.5 seconds. What happens from there is that
is called during each update. Upon the first update that number will be something like -0.5 and will get incremented upwards (-0.49, -0.48,...-0.1, -0.09...). Eventually that number will become > 0 and instruction to kill the explosion entity is sent and that particular explosion is finito.
this.timer.delta()
Resetting timers
this.timer.reset()
This will simply reset your timer back to the original set time (0.5 in the above example). I like setting the timers in my init with the timer number in place. Even if the timer isn't used until later for that particular entity, you can use
to simply start the timer over. In this way, all of your check functions only have to look for a delta of > 0. If you need to tweak the timers then they are all easily accessible in your init.
this.timer.reset()
Setting a new time
this.timer.set(3);
Let's say there is a bigger ship which now needs a longer more exciting explosion or repeat explosions for 3 seconds. An existing timer can be changed to a new time simply by using set. In this example the timer is set to 3, so that the when
is called again from update, it will be -3. It will then increment up to 0. Any timer will continue to increase in number if it is not set or reset. So checking
this.timer.delta()
8 seconds after it is set would yield 5, because it started at -3 and continued for 8 seconds[-3..5].
this.timer.delta()
Gotchas
One gotcha that has hit me a few times is forgetting to include () after delta:
Doing this (wrong way)
this.timer.delta
will return null.
Doing this (correct way)
this.timer.delta()
will give you the change in time in seconds that you want.
And hopefully now, you know what time it is.
(See more in the Impact Timer Documentation.)
MikeL
