Informing the GameScene class when the player dies
So far, the GameScene
class is oblivious to whether the player is alive or dead. We need to change that in order to use our new gameOver
function. Open Player.swift
, locate the die
function, and add the following code at the bottom of the function:
// Alert the GameScene: if let gameScene = self.parent as? GameScene { gameScene.gameOver() }
We access GameScene
by traveling up the node tree. The Player
node's parent is the GameScene
class.
Run the project and die. You should see the two new buttons appear after death, as shown here:
Good work. The buttons are displaying properly, but nothing happens yet when we tap on them. To complete our restart menu, we simply need to implement tap events for the two new buttons in the GameScene
class's touchesBegan
function.