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:
- First, we need to tell the
GameScene
class to implement theSKPhysicsContactDelegate
protocol. SpriteKit can then inform the GameScene class when contact events occur. Change theGameScene
class declaration line to look like this:class GameScene: SKScene, SKPhysicsContactDelegate {
- We will tell SpriteKit to inform
GameScene
of contact events by setting theGameScene physicsWorld contactDelegate
property to theGameScene
instance. At the bottom of theGameScene 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.