Present the Game Over Scene

In Create Your First Component, you created a health component to monitor an entity’s health. Later, in Build a Better Health Component, you enhanced this component by giving it the ability to run certain actions when an entity takes a hit or runs out of health. At the time, you weren’t ready to implement all of the actions for the player—now you are.

Switch to the HealthComponent.swift file, and in the didAddToEntity() method, replace the following code:

 // TODO: Add code to restart the game
 // but for now, reset the player's health
 self​.currentHealth = ​self​.maxHealth

with this code:

 self​.componentNode.scene?.​loadGameOverScene​()

Excellent, this small change takes care of loading the game over scene when Val has to call it quits due to her lack of health. The reason it’s such a small change is because you’re leveraging the SKScene extension you added to the project.

Build and run the project again. This time let the monsters attack Val until she can’t fight any more, thus ending the game, as shown. When the game over scene appears, tap the Start New Game button to start a new game.

images/AddingMoreScenesAndSavingTheGame/build-gameover.png

Excellent, now you have a reason for players to really avoid those monsters.

The next thing you need to do is provide a way for players to exit the map into another level; however, before you can do that, you first need a way to track what level players are on. You also need a way to track how many keys Val has in her pocket and how much treasure she has collected. In other words, you need a way to save and load game data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset