Move the Player Using Physics

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

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.