Time for action – tracking the score

The score updates through a parameter called scoreNum and displays it during the game play. The score number is received through gameScore.

  1. The next function that will be created is called setScore with a parameter called scoreNum:
    local setScore = function( scoreNum )
  2. Use a local variable called newScore and set it as scoreNum. Set the gameScore = newScore. Provide an if statement for gameScore, so that the score during game play is set to 0:
      local newScore = scoreNum
      gameScore = newScore
    
      if gameScore < 0 then gameScore = 0; end
  3. Add the scoreText display object and make it equal to gameScore. Close the function:
      scoreText.text = gameScore
      scoreText.xScale = 0.5; scoreText.yScale = 0.5
      scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 15
      scoreText.y = 20
    end

For setScore = function(scoreNum), we set a parameter called scoreNum. The scoreNum parameter will update the game score continuously through local newScore. newScore will update through gameScore, which provides the basis of the score keeping. At the same time, scoreText will display the value of gameScore during the game.