Game Development Reference
In-Depth Information
In conjunction with this, you will also need to set the delegate to a class that will respond
to these collisions. In this case, you will set the
mainScene
's
contactDelegate
to
self
so that the
GameViewController
can react to the collisions.
mainScene.physicsWorld.contactDelegate = self
The
SCNPhysicsContactDelegate
has three protocols that you can use, as shown
in
Listing 14-9
.
Listing 14-9
.
SCNPhsicsContact Protocols
func physicsWorld(world: SCNPhysicsWorld, didBeginContact
contact: SCNPhysicsContact) {
println(“didBeginContact”)
}
func physicsWorld(world: SCNPhysicsWorld, didEndContact
contact: SCNPhysicsContact) {
println(“didEndContact”)
}
func physicsWorld(world: SCNPhysicsWorld, didUpdateContact
contact: SCNPhysicsContact) {
println("didUpdateContact")
}
your game, you will get the notifications when your spaceman moves around and interacts
with the environment. For this game, however, you will be using only the
didBe-
ginContact
protocol. It will be at this time you check to see whether the contact object
has the node for the spaceman and the node for the enemy. To do this, you will keep
Listing 14-10
.
SCNPhysicsWorld didBeginContact Protocol
func physicsWorld(world: SCNPhysicsWorld, didBeginContact
contact: SCNPhysicsContact) {
if contact.nodeB.physicsBody!.categoryBitMask ==
CollisionCategoryEnemy {
println("Enemy HIT!--------")