Reputation: 1345
I have to detect collision between two SKSpriteNode(wall, man) which is the child of a SKNode background. The background node is child of main SKScene gameScene class. When I want to detect collision using the method
- (void) didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
NSLog(@"contact happened");
}
But no collision detection is found. Please help.
Upvotes: 1
Views: 606
Reputation: 7390
You're probably having one of two issues:
You didn't set the contactDelegate
property of your SKPhysicsWorld
object.
You didn't set your category and contact test bits correctly.
Here's how to set the category and contact test bits:
#define kCategoryOne (1 << 0)
#define kCategoryTwo (1 << 1)
bodyA.categoryBitMask = kCategoryOne;
bodyA.contactTestBitMask = kCategoryTwo;
bodyB.categoryBitMask = kCategoryTwo;
bodyB.contactTestBitMask = kCategoryOne;
Upvotes: 1