Configure Physics Bodies Using User Data

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Animating Tiles | TOC | Next Steps 👉

To keep the player from leaving the dungeon, you’ll set up physics bodies around the dungeon walls. However, there’s a small problem: tile map nodes do not expose their tiles as nodes, which means you can’t assign physics bodies to the individual tiles. Instead, you’ll use custom user data, more specifically, the userData property of the SKNode class.

The userData property of the SKNode class is an NSMutableDictionary that you can use to store custom values known as “user data.” These custom values can include integers, floats, booleans, and strings. You can either set these up in code or, more conveniently, using the Scene Editor and the Attributes Inspector (there’s a User Data section near the bottom).

For Val’s Revenge, you’ll set up two custom values:

  • bodySize: Indicates the size of the physics body to use.
  • bodyOffset: Specifies the offset to use for the body.

The question now is, “Where do you set up these values and how does it all work?”

Working with User Data and Tiles

In the Project Navigator, select the MainTileSet.sks file. With the Attributes Inspector open, expand the Left Edge tile group rule and select the left_edge tile definition inside.

--

--

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.