Game Development Reference
In-Depth Information
demise. In some cases, we have to do something special (such as jumping extra high
when jumping on the turtle). On the player side, we have to load an extra animation
for the player dying. Because we do not want to handle input of the player anymore
once he/she has died, we need to update the current alive status of the player. We do
this by a member variable isAlive that we set to true in the constructor of the Player
class. Inside the HandleInput method, we first check if the player is still alive. If not,
we do not handle any input by returning from the method:
if (!isAlive)
return ;
Furthermore, we will add a method called Die to let the player die. There are two
ways the player can die: by falling in a hole out of the game screen, or by colliding
with an enemy. Therefore, we pass a boolean parameter to the Die method to indicate
if the player died by falling or by colliding with an enemy.
In the Die method, we do a couple of things. First, we check if the player was
already dead. If so, we return from the method without doing anything (after all,
you can only die once). We first set the isAlive variable to false . Then, we set the
velocity in the x direction to zero, to stop the player from moving to the left or the
right. We do not reset the y -velocity, so that the player keeps on falling: gravity
does not cease to exist when you die. Then, we determine which sound to play
upon dying. If the player falls to death, the sound produced is quite different from
dying by an enemy's hand (do not try this for real, just take our word for it). If
the player dies because of a collision with an enemy, we give the player an upward
velocity as well. Finally, we play the 'die' animation. The complete method is given
as follows:
public void Die( bool falling)
{
if (!isAlive)
return ;
isAlive = false ;
velocity.X = 0.0f;
if (falling)
GameEnvironment.AssetManager.PlaySound("Sounds/snd_player_fall");
else
{
900;
GameEnvironment.AssetManager.PlaySound("Sounds/snd_player_die");
velocity.Y =
}
this .PlayAnimation("die");
}
We can check in the Update method if the player is falling to death by calculating if
the player's y -position falls outside of the screen. If this is the case, we call the Die
method:
Search WWH ::




Custom Search