Game Development Reference
In-Depth Information
Chapter 28
Adding Player Interaction
In this chapter, you add more interaction between the player and the objects in the level. Currently,
the player can walk around, and a basic physics system allows the player to jump, collide with wall
tiles, or fall out of the screen. First you look at a very simple kind of interaction: collecting water
drops. Then you see how to create the behavior that allows the player to slide over ice. Finally, you
focus on the part of the program that deals with the various player-enemy interactions in the game.
Collecting Water Drops
The first thing to add is the possibility for the player to collect water drops. A player collects a water
drop if the bomb character collides with that drop. In that case, you make the drop invisible.
Making a drop invisible once a player collects it isn't the only way to approach the problem of
drawing only the uncollected drops, but it's one of the easiest solutions. Another approach would be
to maintain a list of water drops that have been collected and then draw only those drops that the
player still has to find, but this technique requires a lot more code.
The place where you check whether the player collides with a water drop is in the WaterDrop class.
The reason is clear: as before, each game object is responsible for its own behavior. If you handle
these collisions in the WaterDrop class, each water drop checks whether it collides with the player.
You write this code in the update method. The first step is retrieving the player:
var player = this.root.find(ID.player);
If the water drop is currently visible, you check whether it collides with the player using the
collidesWith method. If so, you set the visibility status of the drop to false . You also play a sound
to let the player know the water drop has been collected:
if (this.collidesWith(player)) {
this.visible = false;
sounds.water_collected.play();
}
365
 
Search WWH ::




Custom Search