Game Development Reference
In-Depth Information
like the way that looks. To stop this from happening, modify the current
if
statement to
look like the following:
if playerNode!.position.y >= 180.0 &&
playerNode!.position.y < 6400.0 {
Save this change and run the game again. Try making it to the top of the game and see
what happens. This time, when you reach the top of the scene, the background and fore-
ground will stop moving, and the
playerNode
will continue out of the viewport. This is
a little better.
Winning the Game
At this point, you know that the player is about to fly off the top of the scene, but you
need to test to see when the player actually flies off the scene to determine whether he ac-
tually wins. To do this, add the following
else if
to the current
if
block in the
up-
date()
method:
else if playerNode!.position.y > 7000.0 {
gameOverWithResult(true)
}
This code checks to see whether the
playerNode
has flown more than 7,000 points up
the scene, and if it has, it calls a new instance method
gameOverWithResult()
,
passing
true
to this method, which indicates that the player won. Make these changes,
and let's take a look at the
gameOverWithResult()
method shown here:
func gameOverWithResult(gameResult: Bool) {
playerNode!.removeFromParent()
playerNode = nil
if gameResult {
println("YOU WON!")
}
else {
println("YOU LOSE!")
}
}