Creating a Monster Class

Creating a monster is similar to creating a player. In fact, we are going to use the same basic class code but change its name and namespace. Create a new file called zombie.js in the entities folder.

Now, copy the following code into the monster class:

1    ig.module(
2        'game.entities.zombie'
3    )
4    .requires(
5        'impact.entity'
6    )
7    .defines(function(){
8        EntityZombie = ig.Entity.extend({
10        });
11    });

As you can see, we simply changed the entity name and class name, but everything else is the same as the code we used to start the player class. Now we are ready to add our monster’s animation and set its initial properties:

animSheet: new ig.AnimationSheet( 'media/zombie.png', 16, 16 ),
size: {x: 8, y:14},
offset: {x: 4, y: 2},
maxVel: {x: 100, y: 100},
flip: false,

Now we need to set up the animations just like we did for the player. This is a simple monster, so there are only a few sprites representing its animation. Let’s create a new init() method with the following code:

init: function( x, y, settings ) {
    this.parent( x, y, settings );
    this.addAnim('walk', .07, [0,1,2,3,4,5]);

With our default animation in place, we can start adding instances of the monster to test the level. Let’s switch over to Weltmeister, select the entities layer, and then add a monster by clicking into the layer and pressing the space bar, just as we did when adding the player. You can then click on the map to add the monster where you want it.

Select Zombie from the drop-down entity list.

Figure 4-15. Select Zombie from the drop-down entity list.

Feel free to add a few of them, as shown in Figure 4-16.

I’ve added two zombies to the level.

Figure 4-16. I’ve added two zombies to the level.

Once you have done this, refresh the game in your browser and you should see your new monsters. We haven’t added any movement logic yet, so they don’t do much right now. Let’s add some basic code to make them walk back and forth, but be smart enough not to fall off ledges. We’ll need to create an update function that will handle the basic movement logic or AI (Artificial Intelligence) for our monster:

update: function() {
    // near an edge? return!
    if( !
          this.pos.x + (this.flip ? +4 : this.size.x −4),
               this.pos.y + this.size.y+1
    ) {
          this.flip = !this.flip;
    var xdir = this.flip ? −1 : 1;
    this.vel.x = this.speed * xdir;
    this.currentAnim.flip.x = this.flip;

This function tests to see if the monster hits anything in the collision map. If it does, we toggle the value of the class flip property. After testing, the direction and velocity are updated before this.parent() is called. We will also need to define the monster’s friction and speed. You can add that toward the top of the class just under where we define the flip property:

friction: {x: 150, y: 0},
speed: 14,

Refresh the game to take a look at it in action. You will see the monster instances moving around, and when they hit the edge of a ledge, they flip and go the other way.

We want to make sure our zombies flip direction once they hit a wall or the end of a platform.

Figure 4-17. We want to make sure our zombies flip direction once they hit a wall or the end of a platform.

We just need to add a few more lines of code to clean this up. Add the following block of code to the end of your defines() function:

handleMovementTrace: function( res ) {
    this.parent( res );
    // collision with a wall? return!
    if( res.collision.x ) {
          this.flip = !this.flip;

This helps make sure that if a monster runs into a wall, that it also turns around. Collisions with walls and the collision map are handled through the handleMovementTrace function. Now we have covered all our bases and made sure our zombies will not fall off ledges or platforms, but we still have one issue. There is no collision detection between the monster and the player.

The player simply passes through zombies without collision detection.

Figure 4-18. The player simply passes through zombies without collision detection.

Before we get into adding more code to the monster, we need to talk a little bit about entity-based collision detection in Impact.

So far, we’ve handled simple interactions with walls and platforms manually. However, Impact has built-in collision detection that we can use for interaction between our entities. That is, we can focus on setting up collision relationships instead of creating all that collision code from scratch. Let’s look a little closer at how we can use Impact to do this work for us.

Get Building HTML5 Games with ImpactJS now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.