Game Development Reference
In-Depth Information
addChild(scoreTextNode)
You have seen all of this before, but let's go over it before moving on. The first line sets
the text of the label to “SCORE :” plus the current value in the variable score . The
second and third lines set the font size and font color, respectively.
The fourth and fifth lines in the snippet are important. The fourth line sets the position
(origin) of the scoreTextNode to 10 points to the left of the right side of the scene and
20 points from the top of the scene. The fifth line of this snippet sets the node's horizontal
alignment to SKLabelHorizontalAlignmentMode.Right , which will result in
the label node's right side being set 10 points from the far-right side of the scene.
After that, the scoreTextNode is added to the scene. Once you have taken a look at
this code, add it to the bottom of the init(size: CGSize() method and let's move
on.
There is one last step you need to complete before you can run the app again and start
working on your high score. Find the didBeginContact() method in the GameS-
cene and change the if statement handling contact with the orb nodes to the following:
if nodeB.name == "POWER_UP_ORB" {
impulseCount++
score++
scoreTextNode.text = "SCORE : \(score)"
nodeB.removeFromParent()
}
Once you have saved your changes, take a look at the two lines added before the node is
removed. The first of these two lines increments the score by one, and the second of these
two lines changes the text of the scoreTextNode to reflect this increment. Make sure
you have made all of these changes and run the game again. You will now see the score
label in the top-right corner of the scene, as shown in Figure 7-8 .
Search WWH ::




Custom Search