I'm not really a fan of this :
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
and prefer to do the following:
let contactcontactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactcontactMask {
// Redball and GreenBar have contacted
case RedBallCategory | GreenBarCategory :
score += 1 // ++ is deprecated
scoreLabel.text = "\(score)"
// Remove GreenBar
removeFromParent( let GreenBar = contact.bodyA.categoryBitMask == GreenBarCategory ? contact.bodyA.node! : contact.bodyB.node!)
GreenBar.removeFromParent
// Greenball and RedBar have contacted
case GreenBallCategory | RedBarCategory :
score += 1 // ++ is deprecated
scoreLabel.text = "\(score)"
// Remove RedBar
removeFromParent( let RedBar = contact.bodyA.categoryBitMask == RedBarCategory ? contact.bodyA.node! : contact.bodyB.node!)
RedBar.removeFromParent
default :
//Some other contact has occurred
}
You can just add as many RedBallCategory | GreenBarCategory cases as you need for all the contacts that you have to take action for in your game. Code each potential contact individually and you won't loose yourself in if...then...else