You can use categoryBitMask
defined as ContactCategoryPlayer = 0x1 << 0
.
you can anther node subclass Puff
, and that has physics body with a categoryMask
defined as ContactCategoryPuff = 0x1 << 1
.
Step 1
Define unique Categories.
let ContactCategoryPlayer: UInt32 = 0x1 << 0 // bitmask is ...00000001
let ContactCategoryPuff: UInt32 = 0x1 << 1 // bitmask is ...00000010
Step 2
Assign the categories.
player.physicsBody?.categoryBitMask = ContactCategoryPlayer
puff.physicsBody?.categoryBitMask = ContactCategoryPuff
Step 3
Assign the categories.
enemy.physicsBody?.collisionBitMask = 0
puff.physicsBody?.collisionBitMask = 0
You can invoke these collision handlers via the delegate callback :
// MARK: SKPhysicsContactDelegate
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
print("contact!!!")
}
}
Here is Reference.
https://medium.com/@JohnWatson/simplified-collision-handling-in-spritekit-71de9bea6302