Check out collisionBitMask, categoryBitMask, and contactTestBitMask in the SKPhysicsBody
class.
Essentially, physics bodies with the same collisionBitMask
value will "pass-through" each other.
- Correction: If the category and collision bits match, they will interact. If they do not match, those two will not interact. And if the collision bits, and category bits, are both zero, of course that item will interact with nothing whatsoever.
Then, you set the categoryBitMask
and contactTestBitMask
values to create an SKPhysicsContact
Object on contact. Finally, your Class should adopt the SKPhysicsContactDelegate
protocol. Use the - didBeginContact:
method to detect and handle the SKPhysicsContact
object.
static const uint8_t heroCategory = 1;
static const uint8_t foodCategory = 2;
--
food.physicsBody.categoryBitMask = foodCategory;
food.physicsBody.contactTestBitMask = heroCategory;
food.physicsBody.collisionBitMask = 0;
--
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.contactTestBitMask = foodCategory;
hero.physicsBody.collisionBitMask = 0;
--
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody = contact.bodyA;
SKPhysicsBody *secondBody = contact.bodyB;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…