Preparing GameScene for contact events

Now that we have assigned the physics categories to our game objects, we can monitor for contact events in the GameScene class. Follow these steps to wire up the GameScene class:

  1. First, we need to tell the GameScene class to implement the SKPhysicsContactDelegate protocol. SpriteKit can then inform the GameScene class when contact events occur. Change the GameScene class declaration line to look like this:
            class GameScene: SKScene, SKPhysicsContactDelegate { 
  2. We will tell SpriteKit to inform GameScene of contact events by setting the GameScene physicsWorld contactDelegate property to the GameScene instance. At the bottom of the GameScene didMove function, add this line:
     self.physicsWorld.contactDelegate = self ...

Get Swift Game Development - Third Edition 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.