Search code examples
swiftxcodesprite-kitskphysicscontact

SpriteKit didEndContact not being called


In my didEndContact: method I increment a "currentScore".

For some reason, didEndContact: is not being called, and hence the "currentScore" is not being incremented. Any ideas why or what I should be looking for to debug this?

func didEndContact(contact: SKPhysicsContact) {
    guard goal!.barEnabled else { return }
    score += 1
}

Here is related code, let me know if I can add any other helpful details too:

var barEnabled:Bool {
    set {
        bar?.physicsBody?.collisionBitMask = newValue ? CollisionMask.Puck : CollisionMask.None
        bar?.physicsBody?.categoryBitMask = newValue ? CollisionMask.Goal : CollisionMask.None
    }
    get {
        return bar?.physicsBody?.collisionBitMask == CollisionMask.Puck
    }
}

Solution

  • In order to detect collisions, you'll have to set the contactTestBitMask of the physicsBody.

    The contactTestBitMask of a physicsBodys and the categoryBitMask of another physicsBodys have to be not 0 when an binary AND operation is applied to the 2 integers for the detection to trigger.

    In case you have not already, make sure that the physicsWorld.contactDelegate of the scene is set to self and that it implements the SKPhysicsContactDelegate.