A scoring system in a game
Adding a scoring or points system in a game makes it more interesting and fun to play. Having a scoring system in the game makes it easier for the players to measure their performance, making the objective clear for the user.
It always makes sense to display the score somewhere on the main screen, so that the player can have a look at the score while playing the game.
Adding a scoring system in our Platformer game
In the first step of adding a scoring system in our game, we create a label node to display the score to the player. The initial variable will be zero.
Creating the Score label
Let's add the following code snippet in the beginning of the GameScene
class:
let scoreText = SKLabelNode(fontNamed: "Chalkduster") var score = 0
In the preceding code, you are creating an SKLabelNode
and assigning it to the font, Chalkduster
. Along with this, you are also initializing a variable score
with the value as zero.
Now, let's set the ScoreText
label created above zero. Also...