An easy way to handle the bitmasks in swift is to create an enum of type UInt32 containing all your different collision types. That is
enum ColliderType: UInt32 {
case Player = 1
case Attacker = 2
}
And then in your Player Class add a physics body and setup the collision detection
physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(size.width, size.height))
physicsBody.categoryBitMask = ColliderType.Player.toRaw()
physicsBody.contactTestBitMask = ColliderType.Attacker.toRaw()
physicsBody.collisionBitMask = ColliderType.Attacker.toRaw()
And for your Attacker Class (or projectile, bird, meteor, etc.) setup its physics body as
physicsBody = SKPhysicsBody(circleOfRadius: size.width / 2)
physicsBody.categoryBitMask = ColliderType.Attacker.toRaw()
physicsBody.contactTestBitMask = ColliderType.Player.toRaw()
physicsBody.collisionBitMask = ColliderType.Player.toRaw()
(Note that you can setup the physics body to be whatever shape you want)
Then make sure you have a SKPhysicsContactDelegate
setup (e.g. you can let your scene be the delegate) and then implement the optional protocol method didBeginContact
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
// Additional setup...
}
func didBeginContact(contact: SKPhysicsContact!) {
println("A collision was detected!")
if (contact.bodyA.categoryBitMask == ColliderType.Player.toRaw() &&
contact.bodyB.categoryBitMask == ColliderType.Attacker.toRaw()) {
println("The collision was between the Player and the Attacker")
}
}
}
By adding more ColliderTypes you can detect more collisions in your game.