April 2021
Beginner to intermediate
506 pages
11h 57m
English
Using physics to move bodies is a quick and effective way to build a controller. To move the player using physics and the on-screen controller, you need to act on certain touch events.
Open the GameScene.swift file and update the touchDown(:atPoint) method:
| | 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(Direction(rawValue: direction ?? "stop")!) |
| | } |
| | } |
| | } |
Here, you’re checking to see which node was touched using the prefix of the ...
Read now
Unlock full access