Report a Bug

SIGN IN!

Using Impact Timers

Impact timers are extremely useful, but understanding them can be confusing. This tutorial helps make them more clear - so you can know what time it is.

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:

  1. 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.

  1. ig.module(
  2. 'game.entities.explosion'
  3. )
  4. .requires(
  5. 'impact.entity'
  6. )
  7. .defines(function(){
  8.  
  9. EntityExplosion = ig.Entity.extend({
  10. size: {x: 48, y: 48},
  11.  
  12. type: ig.Entity.TYPE.NONE,
  13. collides: ig.Entity.COLLIDES.NEVER,
  14.  
  15. animSheet: new ig.AnimationSheet( 'media/Explode5-m.png', 48, 48),
  16.  
  17. init: function( x, y, settings ) {
  18. //Received the coordinates of the center of the creating entity.
  19. //Subtract half of the size of the explosion from these numbers
  20. //in order to center the explosion on top of the entity.
  21. x = x - (this.size.x/2);
  22. y = y - (this.size.y/2);
  23. this.parent( x, y, settings );
  24.  
  25. //We only want the explosion animation to loop once so stop is true.
  26. this.addAnim( 'idle', 0.05, [0,1,2,3,4,5,6,7], true );
  27. //Start timer so this entity can be killed after 0.5 seconds.
  28. this.timer = new ig.Timer(0.5);
  29.  
  30. },
  31.  
  32. update: function(){
  33. //If it has been more than 0.5 seconds then kill this explosion entity.
  34. if (this.timer.delta() > 0) {
  35. this.kill();
  36. }
  37. this.parent();
  38. },
  39.  
  40. });
  41.  
  42. });

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
  1. this.timer.delta()
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.

Resetting timers

  1. 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

  1. this.timer.reset()
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.

Setting a new time

  1. 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

  1. this.timer.delta()
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
  1. this.timer.delta()
8 seconds after it is set would yield 5, because it started at -3 and continued for 8 seconds[-3..5].

Gotchas

One gotcha that has hit me a few times is forgetting to include () after delta:

Doing this (wrong way)

  1. this.timer.delta

will return null.

Doing this (correct way)

  1. 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.)


detj
These timers count game time? Say, I'm creating a color matching game and every session should run for 60 seconds. Now, if the frame rate is not optimal and quite slow, then the perceived time would increase. The player would see 60 sec in the counter, but ...more
Graphikos
Something I had trouble with, which is more about OOP rather than the timer, was when creating and setting the timer outside of an entity's init(). This made it so all entities would share the same timer rather than run its own.
Contributed by:
MikeL
View Profile
Category:Game Elements
Updated:October 7, 2011
Rating:
Your Rating (0)
Average Rating (4.3)
Ready to get to the point?

Your Email will remain private and is only used for good. We promise!


Please use only letters, numbers or underscores.

SIGN UP!