Add Physics to the Player

Apple Game Frameworks and Technologies — by Tammy Coron (83 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Write Code to Interface with the Scene | TOC | Move the Player Using Physics 👉

You can move players (and enemies) around the scene in many ways. In Gloop Drop, you used actions. In Val’s Revenge, you’ll use the physics engine to move Val, the main character of the game. But before you can do that, you first have to set up a physics body for the player sprite node.

In Chapter 4, Working with Physics and Collision Detection, you learned the ins and outs of using the physics engine, including how to write code to set up the different physics bodies and properties. Now it’s time to learn how to set up physics using the Scene Editor.

Open the GameScene.sks file and select the player sprite node. Look at the Attributes Inspector and scroll down to the Physics Definition section. Switch the Body Type from None to Bounding Circle. Notice the blue outline around the player sprite (enhanced here, for clarity) and the different options available in the inspector for this physics body as shown in the image.

images/UsingTheSceneEditorToAddPhysics/scene-edit-phy-setup-body.png

For the most part, these settings are a 1:1 match to the properties you can access in code. For example, Dynamic is a match for physicsBody?.isDynamic and Allows Rotation is a match for…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.