Adding Lives to PSK
Find your main file (PlatformerGame.cs is default, GameplayScreen.cs if you're using GameStateManagement)
You should already have a line that reads, “Texture2D loseOverlay” and we’ll be using that for this tutorial.
PLAYER.CS
Somewhere near the top near the other variable declarations, add this line.
public int lives = 3;
In the OnKilled(…) method add the following at the very bottom:
lives--;
PLATFORMERGAME.CS
In the DrawHud(…) method find this statement:
else if (!level.Player.IsAlive)
{
status = diedOverlay;
}
And change it to this:
else if (!level.Player.IsAlive)
{
if(level.Player.lives > 0)
status = diedOverlay;
else
status = loseOverlay;
}
And there you have it, you now have the most basic life system you can incorporate. Everytime you die your life meter will decrement by one and when your lives are < 1 the loseOverlay will show instead of diedOverlay.
Using this combined with the GameStateManagement example, you can have a gameover screen show when you lose all your lives, and kick the player back to the main menu or whatever you’d like.