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 theGameScene
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
SKPhysicsContactDelegate
defines adidBegin
function that will fire when contact occurs. We can now implement thisdidBegin
function in theGameScene
class. Create a new function in theGameScene
class nameddidBegin
,...