I am using SpriteKit to build a game and have run into some trouble with collision detection. I have 2 subclasses of SKSpriteNode
, Player
and Enemy
. They should both detect collisions with each other. Here's how I am initializing the Player
object's physicsBody
:
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.dynamic = NO;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.categoryBitMask = playerCategory;
self.physicsBody.collisionBitMask = enemyCategory;
self.physicsBody.contactTestBitMask = enemyCategory;
And here's how I am initializing the Enemy
objects's physicsBody
:
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
self.physicsBody.affectedByGravity = NO;
self.physicsBody.dynamic = NO;
self.physicsBody.usesPreciseCollisionDetection = NO;
self.physicsBody.categoryBitMask = enemyCategory;
self.physicsBody.collisionBitMask = playerCategory;
self.physicsBody.contactTestBitMask = playerCategory;
I have my GameScene
implementing the SKPhysicsContactDelegate
protocol and I have this in its init:
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
self.physicsWorld.contactDelegate = self;
And yet, the didBeginContact
method isn't being called as it should be. I tried initializing the physicsBodies
after the objects are created in the scene and still nothing. What am I doing wrong?
UPDATE 1
Here's the code that makes the bitMasks
, it's in a Common.h
file that is imported in Prefix.pch
file.
static const uint32_t enemyCategory = 0x1 <<0;
static const uint32_t playerCategory = 0x1 <<1;