I'm making a game with Sprite Kit and I have the collision detection setup like this:
ball.physicsBody?.categoryBitMask = ballCategory
borderBody.physicsBody?.categoryBitMask = borderCategory
And the handler -didBeginContact()
:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == borderCategory {
print("You Lose!")
}
}
I also have this line to set the contact delegate:
self.physicsWorld.contactDelegate = self
But when I run the game, the collision is not detected and nothing happens. What is wrong?
See the documentation here . You need to set the contactTestBitMask
if you want to receive contact/intersection notifications. Unlike collisionBitMask
whose default value is 0xFFFFFFFF
, the default value for contactTestBitMask
is 0x00000000
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.