Game Development Reference
In-Depth Information
this.exploded = true;
this.velocity = powerupjs.Vector2.zero;
this.playAnimation("explode");
sounds.player_explode.play();
};
First, the player character can't explode if the character wasn't alive in the first place, or if the player
finished the level. In either of those cases, you simply return from the method. Then, you set the alive
status to false and the exploded status to true . You set the velocity to zero (explosions don't move).
Then, you play the “explode” animation. This animation is stored in a sprite sheet and consists of
25 frames of an explosion. Finally, you play an appropriate sound.
Because gravity also no longer affects an exploded character, you only do the gravity physics if the
player isn't exploded:
if (!this.exploded)
this.velocity.y += 55;
In the update method of the Level class, you check whether the timer has reached zero, and if so,
you call the explode method:
if (timer.gameOver)
player.explode();
Drawing Mountains and Clouds
To make the level background a bit more interesting, let's add mountains and clouds to it. You do
this in the Level constructor. First, let's have a look at how to add a few mountains. For that, you use
a for instruction. In the body of that instruction, you create a sprite game object, give it a position,
and add it to the backgrounds list. This is the complete for instruction:
for (var i = 0; i < 5; i++) {
var sprid = "mountain_" + (Math.ceil(Math.random()*2));
var mountain = new powerupjs.SpriteGameObject(sprites[sprid], ID.layer_background_2);
mountain.position = new powerupjs.Vector2(Math.random() *
powerupjs.Game.size.x - mountain.width / 2,
powerupjs.Game.size.y - mountain.height);
backgrounds.add(mountain);
}
The first step is to create the sprite game object. You want to choose randomly between the different
mountain sprites that you have. Because there are two mountain sprites, you create a random
number (either 1 or 2) to select between them. You use the number to create the ID that corresponds
to that sprite.
Then you calculate the position of the mountain. The x position is chosen randomly, and you use a
fixed y position so the mountain is at the appropriate height (you don't want mountains hanging in
the sky). Finally, the mountain object is added to the backgrounds list.
 
Search WWH ::




Custom Search