Cover by Jesse Freeman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Create Death Animations

One of the easiest ways to show a death animation is to create a small particle explosion where the player is killed. Not only does this cut down on the amount of animations you have to create but you can also use the same technique to show damage taken by a projectile weapon. In order to do this, we will need to create two new entities, one for the explosion and the other for the actual particles. Let’s add the following inner class to our player.js module:

EntityDeathExplosion = ig.Entity.extend({
    lifetime: 1,
    callBack: null,
    particles: 25,
    init: function( x, y, settings ) {
          this.parent( x, y, settings );
          for(var i = 0; i < this.particles; i++)
               ig.game.spawnEntity(EntityDeathExplosionParticle, x, y, {colorOffset: settings.colorOffset ? settings.colorOffset : 0});
               this.idleTimer = new ig.Timer();
    },
    update: function() {
          if( this.idleTimer.delta() > this.lifetime ) {
               this.kill();
               if(this.callBack)
                     this.callBack();
               return;
          }
    }
});

This is a very simple class. It handles spawning particle entities, which we will create next, and also has a timer, which we use to call a callback() method that is supplied by the setting property. Pay special attention to this.idleTimer and the new ig.Timer(). We use these to keep track of how much time has elapsed since its instantiation, just like we did when we added invincibility to the player.

There is also something else going on here. You may have noticed that when we spawn our EntityDeathExplosionParticle, we are passing in a color offset value. If you take a look at the blood sprite, you will see that we have colored sprites for the player in red and for the zombie in green.

This sprite contains the player and zombie blood particles.

Figure 4-27. This sprite contains the player and zombie blood particles.

This is a neat trick and one that is used in a lot of sprite sheet-based games. Our blood particles are going to be 2×2 pixels in size. That means we have eight sprites for each color. When we set up our particle, we will apply the color offset to the graphic we display. So if the player is hit, we will add 0 to the offset, which will generate a random red color. For zombies we will add 1 to the offset, which will multiply by the base number of possible sprites and move the randomly selected sprite into the green zone. Let’s take a look at our particle class to see this in action. Create a new inner class with the following code:

EntityDeathExplosionParticle = ig.Entity.extend({
    size: {x: 2, y: 2},
    maxVel: {x: 160, y: 200},
    lifetime: 2,
    fadetime: 1,
    bounciness: 0,
    vel: {x: 100, y: 30},
    friction: {x:100, y: 0},
    collides: ig.Entity.COLLIDES.LITE,
    colorOffset: 0,
    totalColors: 7,
    animSheet: new ig.AnimationSheet( 'media/blood.png', 2, 2 ),
    init: function( x, y, settings ) {
        this.parent( x, y, settings );
        var frameID = Math.round(Math.random()*this.totalColors) + (this.colorOffset * (this.totalColors+1));
        this.addAnim( 'idle', 0.2, [frameID] );
        this.vel.x = (Math.random() * 2 - 1) * this.vel.x;
        this.vel.y = (Math.random() * 2 - 1) * this.vel.y;
        this.idleTimer = new ig.Timer();
    },
    update: function() {
        if( this.idleTimer.delta() > this.lifetime ) {
            this.kill();
            return;
        }
        this.currentAnim.alpha = this.idleTimer.delta().map(
            this.lifetime - this.fadetime, this.lifetime,
            1, 0
        );
        this.parent();
    }
});

As you can see, the particle has a few properties such as its maximum velocity, how long before it fades away, bounciness, and initial velocity. Most of this should look very familiar from what we did with our grenade class. As you can see in the init() method, we assign a random value to the particle’s vel.x and vel.y values, which sends each one off in different directions. Since this is blood and we don’t want it bouncing around like the grenade, the bounciness property is set to 0. We take advantage of this.currentAnim.alpha value, which assigns a new alpha value after each update, and eventually the particle disappears. Once it fades away, we call kill() to remove it from the display.

Now that we have our particle emitter and our particle, we can extend the player’s kill() method to spawn our EntityDeathExplosion where the player was killed and watch it spawn random particles as if the player exploded. Here is the modified EntityPlayer kill() method:

kill: function(){
    this.parent();
    var x = this.startPosition.x;
    var y = this.startPosition.y;
    ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {callBack:function(){ig.game.spawnEntity( EntityPlayer, x, y)}} );
}

Since we are passing a function into the settings object, we will need to re-scope the start position of the player. If you refresh your browser and run the player into the monster, you will now see him explode into tiny pieces that bounce and fade away.

The player now explodes into pieces.

Figure 4-28. The player now explodes into pieces.

As I mentioned before, this is also a great effect for us to show when an entity has been hit. Let’s override the zombie.js receiveDamage() method with the following:

receiveDamage: function(value){
    this.parent(value);
    if(this.health > 0)
          ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {particles: 2, colorOffset: 1});
},

We can actually use the same death explosion class in our zombie entity, even though it is an inner class of player. This is a neat little hack thanks to the fact that JS’s scope is global and, when any entity gets defined in Impact, it is available throughout the game engine. So, in the EntityZombie class, we simply spawn a new death explosion just like we did in the player class, but pass in a smaller number of particles to be emitted. We also pass in the colorOffset so that we can display green blood instead of red. Now when a bullet hits the zombie, little particles will shoot off of it. Also, don’t forget to use the same death animation technique we used on the player by overriding the zombie.js kill() method with the following:

kill: function(){
    this.parent();
    ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {colorOffset: 1});
}

And there you go; you have just created a nice-looking dynamic death animation for your player and monster. We can also apply the same technique to our grenades and make their explosions more visually appealing, so let’s take a look.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required