Game Development Reference
In-Depth Information
Before moving on, do one more thing: add the code to remove the
startGameTex-
tNode
label from the scene when the screen is tapped. You can do this by adding the fol-
lowing line to the first
if
statement in the
touchedBegan()
method:
startGameTextNode.removeFromParent()
The new
touchesBegan()
method's
if
statement now looks like the following:
if !playerNode!.physicsBody!.dynamic {
startGameTextNode.removeFromParent()
playerNode!.physicsBody!.dynamic = true
coreMotionManager.accelerometerUpdateInterval = 0.3
coreMotionManager.startAccelerometerUpdatesToQueue(NSOperationQueue(),
withHandler: {
(data: CMAccelerometerData!, error: NSError!) in
if let constVar = error {
println("There was an error")
}
else {
self.xAxisAcceleration
= CGFloat(data!.acceleration.x)
}
})
}
Ending the Game
Now it's time to add the code that will determine when the SuperSpaceMan game ends.
There are really only two ways the game ends; either the player dodges all the black holes
and collects orbs until he reaches the top of the scene and wins or he falls off the bottom
of the scene and loses. The easiest place to check the player's position as it updates is in
the
GameScene
's
update()
method. Let's go back to that method and add the neces-
sary code to do this.
Currently, the
update()
method has a simple
if
statement that moves the foreground
and background nodes according to the
playerNode
's position as long as the
player-
Node
's y-position is greater than or equal to 180.0. This means the
playerNode
could
leave the
backgroundNodes
behind and fly off into the black of space. I really don't