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 ...
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.