Featured Post

BokCoi

A fast paced action game that gives you lots of fun instantly!

Sunday, September 25, 2016

Swift - Why didBeginContact not called on collision?

There are several reasons why contact and collision are not detected in your Swift project. Below is a checklist I've made when setting up collision on Swift:

1. On Swift 3.0, function didBeginContact is changed to:

optional func didBegin(_ contact: SKPhysicsContact) { ... }


2. Have you implemented the SKPhysicsContactDelegate protocol?

class yourClass: SKScene, SKPhysicsContactDelegate { ... }


2. Have you implemented the Contact Delegate?

self.physicsWorld.contactDelegate = self


3. Have you defined the necessary Category Bit Masks?

let firstCategory: UInt32 = 0x1 << 0
let secondCategory: UInt32 = 0x1 << 1


4. Have you assigned the correct Category Masks to your Sprite Nodes?

firstNode.physicsBody?.categoryBitMask = firstCategory


5. Have you configured the correct Contact and Collision Masks to your Sprite Nodes?

firstNode.physicsBody?.contactTestBitMask = secondCategory
firstNode.physicsBody?.collisionBitMask = secondCategory


6. Remember that one of the colliding Sprite Nodes must be dynamic

yourNode.physicsBody?.isDynamic = true


Tuesday, May 10, 2016