Present the Game Over Scene

Apple Game Frameworks and Technologies — by Tammy Coron (143 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Load the First Game Scene | TOC | Save and Load Game Data 👉

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.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.