Add a Physics Contact Extension
Inside the Extensions group, create a new file (⌘N) using the iOS Swift File template. Name the new file GameScene+PhysicsContact.swift and replace its contents with the following:
| import SpriteKit |
| |
| extension GameScene: SKPhysicsContactDelegate { |
| func didBegin(_ contact: SKPhysicsContact) { |
| let collision = contact.bodyA.categoryBitMask |
| | contact.bodyB.categoryBitMask |
| |
| switch collision { |
| default: |
| break |
| } |
| } |
| } |
This code is the start of your physics contact manager.
To keep the code organized and make it easier to see what contacts are taking place, you’ll use code comments. Add the following code just above the line that reads default::
| // MARK: - Player | Collectible ... |
Get Apple Game Frameworks and Technologies 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.