Game Development Reference
In-Depth Information
}
}
Listing 15-7 shows the updated didBeginContact method . This method will do one of two
things depending on if you run into the enemy or an obstacle.
The player makes contact with the enemy. Stop the timer and set the
gameStarted to false. Then transition to the “game over” scene.
The player makes contact with an obstacle; reset the spaceman to the starting
point.
Listing 15-7 . Contact with Enemy Node Enhancement
func physicsWorld(world: SCNPhysicsWorld, didBeginContact
contact: SCNPhysicsContact) {
if
contact.nodeB.physicsBody!.categoryBitMask ==
CollisionCategoryEnemy {
gameOverlay.stopTimer()
gameStarted = false
sceneView.overlaySKScene
= GameOverView(size: view.bounds.size, score:
gameOverlay.timeNode.text)
}
if
contact.nodeB.physicsBody!.categoryBitMask ==
CollisionCategoryObstecles {
resetSpaceman()
}
}
Now when you run your game and you get to the enemy, the player will be shown a
“game over” view and their time. There is one other item you need to deal with, and that
is to update the sceneView.overlaySKScene to the original timer screen.
In func renderer(aRenderer: SCNSceneRenderer, didSimulatePhysicsAtTime time:
NSTimeInterval) , you update the if statement where you are checking for the touchCount
and whether the game is not started, as shown in Listing 15-8 . You will reset the space-
man to his starting location and then change the overlay back to the timer.
Search WWH ::




Custom Search