Implementing touch events for the restart menu
Now that our buttons are displaying, we can add touch events in the GameScene
class that are similar to the START GAME button in the MenuScene
class.
To add the touch events, open GameScene.swift
and locate the touchesBegan
function. We will add the restart menu code at the bottom of the for
loop. I am including the entire touchesBegan
function in the following code, with new additions in bold:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in (touches) { // Find the location of the touch: let location = touch.location(in: self) // Locate the node at this location: let nodeTouched = atPoint(location) // Attempt to downcast the node to the GameSprite protocol if let gameSprite = nodeTouched as? GameSprite { // If this node adheres to GameSprite, call onTap: gameSprite.onTap() } // Check...