Update the Score Label

Program the label to display the live score.

Step 1

At the moment, the game can do the following things:

  • Move the sprite to a random location every half second
  • Start the sprite in a new location every time the game begins
  • Play a sound and vibrate when the sprite is touched

Step 2

The next thing to include in your game is scoring. You have already set up a score label in the UI. Now you need to program the game to keep score. To do this, create a new Variable called “score”. Variable blocks are like containers that hold information for later use.

  • Go to the Blocks editor
  • Open the Variable drawer
  • Drag out the initialize global name to block 
  • Click on the word “name” and change it to “Score”
  • Go to the Math drawer 
  • Snap a number “0” block to the empty slot of the score variable block

This means that when the game begins, the score is set to zero.

Step 3

Create a new procedure called UpdateScore that will automatically change the ScoreLabel text to include the score. 

Go to the Procedures drawer and pull out a to procedure do block. Click on the word “procedure” and change it to “UpdateScore”. 

Find the set ScoreLabel.Text to block in the ScoreLabel drawer and snap it to the new procedure. 

Step 4

  • Go to the Text drawer
  • Drag out a Join block 
  • Snap it to the set ScoreLabel.Text to block
  • Go back to the Text drawer 
  • Pull out a blank Text block
  • Snap it to the first empty slot in the Join block and insert the text “Score:”

Hover the mouse over the word “Score” in the Variable block. Pull out get global Score and snap it to the second slot in the Join block. 

Now the real score will show in the ScoreLabel text to the word “Score:”

Here is what the UpdateScore Procedure should look like: