Connect the Attack Button
The last thing you’ll do in this chapter is write the code that handles the player’s attack routine—in this case, Val’s knife throw—which should happen when the player taps the Attack button.
In the Project Navigator, select the GameScene.swift file and modify the touchDown(:_) method to match the following (you’ll get a message about the missing attack() method, but for now, ignore the error):
| func touchDown(atPoint pos : CGPoint) { |
| let nodeAtPoint = atPoint(pos) |
| if let touchedNode = nodeAtPoint as? SKSpriteNode { |
| if touchedNode.name?.starts(with: "controller_") == true { |
| let direction = touchedNode.name?.replacingOccurrences( |
| of: "controller_", with: "") |
| player?.move ... |
Get Apple Game Frameworks and Technologies 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.