Alex, you can have a look at the
YOSS game code under entities >> explosion.js for a concrete example.
Here is a repost of the code:
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
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. Eventually that number will become > 0 and instruction to kill the explosion entity is sent and that particular explosion is finito.
I like setting the timers in my init with the timer number in place. Even if the timer isn&
039;t used until later for that particular entity, you can use #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.
Also one gotcha, for some reason I will forget to put the
( )
after the delta call which will foul things up, because null will be returned. Make sure to use
this.timer.delta()
, of course substituting your own timer's name. Hopefully that helps. :)