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 ...
Get Swift Game Development - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.