This code crashes because secondBody
is not associated with a node.
func didBegin(_ contact: SKPhysicsContact) {
// Set vars to hold bodies
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
// Sort bodies
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// Handle different contacts
if firstBody.categoryBitMask == BallBitMask && secondBody.categoryBitMask == TileBitMask {
didBallTileHit(tile: secondBody.node!) // Code crashes here :(
}
...
}
1) Aside from setting a physics body to nil
, what would cause a physics body to lose association with a node in SpriteKit?
2) How can a physics body even exist without a node in the first place?