Wiring up the START GAME button

Just like in GameScene, we will add a touchesBegan function to the MenuScene class to capture touches on the START GAME button. To implement touchesBegan, open MenuScene.swift and, at the bottom of the class, add a new function named touchesBegan, as shown here:

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) 
        if nodeTouched.name == "StartBtn" { 
            // Player touched the start text or button node 
            // Switch to an instance of the GameScene: 
            self.view?.presentScene(GameScene(size: self.size)) 
        } 
    } 
} 

Run the project and tap the ...

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.