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?
physicsBodyproperty instead. I believe doing this in turn sets the body'snodeproperty accordingly. - Nicolas Miaricontactpassed to your method (is it a collision delegate of some sort?) contains no dettached physics bodies (i.e., bodies not owned by a node); What would be the point? - Nicolas MiariSKNodeand/orSKPhysicsBody, override the involved properties with observers and try to catch when node becomesnil? - Nicolas Miari