Who doesn't like some friendly competition? We're familiar with scoreboards from the games we made in the previous chapters. So, we are not strangers to tracking the score. Perform the following steps to set the score:
setScore()
with a parameter called scoreNum
:local setScore = function( scoreNum )
local newScore = scoreNum gameScore = newScore if gameScore < 0 then gameScore = 0; end
scoreText.text = "Score: " .. gameScore scoreText.xScale = 0.5; scoreText.yScale = 0.5 scoreText.x = (scoreText.contentWidth * 0.5) + 15 scoreText.y = 15 end
When setScore(scoreNum)
is called within any function, it will refer to all the methods using the gameScore
variable. Assuming gameScore
=
0
at the start of the application, the value increments to what gameScore
is set to.
In scoreText.text
=
"Score: " .. gameScore
, "Score: "
is the string that displays on the device during game play. The gameScore
variable takes the current value given to the variable and displays it as a string.