This tutorial explains how to add a score to your app that goes up based on a timer.
I got a request today for a tutorial explaining how to have a score that goes up by X points every second that passes; this is the result of that request.
You will notice how incredibly simple it is once you know how – main.lua requires all needed code, while the project folder also contains score.lua and PNGs for numbers 0 to 9, a score background and a space.
Under no circumstances may you reuse the images in this project. (They’re mine, so please respect that – simply replace them with your own.)
Enjoy! I hope this helps you.
Peach Pellen
PS – If you would like to expand your understanding of the score method that I am using, this score tutorial should help.
DOWNLOAD THE TUTORIAL HERE.


[...] Lesson 14: Adding a Score to your App Based on a Timer [...]
bamf.
And to you ^-^;
Hi Peach,
I came across your tutorials. Thanks for all the useful info.
I was wondering how do you reset the score? Basically, I am passing to the score to another display (results.lua) after viewing the results I want to go back to the main screen and start over. However, I am getting an error for x having a nil value.
Seems like this should be simple, but I am missing something.
Thanks
Hey there,
You can reset it by doing score.setScore(0) if I recall correctly.
I’m not sure about your error for x being nil, does the error give you a line?
Peach
Hi Peach,
I get this error
score.lua:72: attempt to perform arithmetic on field ‘x’ (a nil value)
I have score.setScore(0) in my scene.
I am using the Director Class. When I leave the scene I cancel the timer and display the score on a separate scene. When I come back, I would think the scene would just load the same as before. However…
I think I might try the Storyboard. Do you think it would make a difference?
Thanks,
Rickyd
Rather than displaying the score on a separate screen try bumping it off the side of the screen so it is hidden. It’s not a great score system, it may be better to use a custom font and do the score in a normal way without images, even?
Peach
Thanks, I will try this.
I need some help—
I need to create score for game, then i need the application to tell add
scores, i have tried everything and cannt find a way to do this, plz provide
me some sample if you can so that it can make me undetstand better.
pls help me.
Thanks
The below is the code …
display.setStatusBar (display.HiddenStatusBar)
require (“physics”)
local score = 0
local scoreText
local levelText
local levelNum
function main()
setUpPhysics()
createWalls()
createBricks()
createBall()
createPaddle()
startGame()
createScore ()
createLevel ()
end
function setUpPhysics()
physics.start()
physics.setGravity(0,0)
end
function createPaddle()
local paddleWidth = 200
local paddleHeight = 30
local paddle = display.newRect( display.contentWidth /1 -
paddleWidth /1, display.contentHeight -30, paddleWidth, paddleHeight )
physics.addBody(paddle, “static”, {friction=0, bounce=1})
local movePaddle = function(event)
paddle.x = event.x
end
Runtime:addEventListener(“touch”, movePaddle)
end
function createBall()
local ballRadius = 20
ball = display.newCircle( display.contentWidth / 2,
display.contentHeight / 2, ballRadius )
physics.addBody(ball, “dynamic”, {friction=0, bounce = 1,
radius=ballRadius})
ball.collision = function(self, event)
if(event.phase == “ended”) then
if(event.other.type == “destructible”) then
event.other:removeSelf()
end
if(event.other.type == “bottomWall”) then
self:removeSelf()
local onTimerComplete = function(event)
createBall()
startGame()
end
timer.performWithDelay(500, onTimerComplete , 1)
end
end
end
ball:addEventListener(“collision”, ball)
end
function startGame()
ball:setLinearVelocity(200, 600)
end
function createBricks()
local brickWidth = 60
local brickHeight = 30
local numOfRows = 6
local numOfCols = 7
local topLeft = {x= display.contentWidth / 2 – (brickWidth *
numOfCols ) / 2, y= 50}
local row
local col
for row = 0, numOfRows – 1 do
for col = 0, numOfCols – 1 do
function createScore ()
levelScore = display.newText(‘Score:’, 50, 10, ‘akashi’, 30)
levelScore:setTextColor(254, 203, 50)
levelNum = display.newText(’1′, 540, 10, ‘akashi’, 30)
levelNum:setTextColor(254,203,50)
end
function createLevel ()
levelText = display.newText(‘Level:’, 460, 10, ‘akashi’, 30)
levelText:setTextColor(254, 203, 50)
levelNum = display.newText(’1′, 540, 10, ‘akashi’, 30)
levelNum:setTextColor(254,203,50)
end
– Create a brick
local brick = display.newRect( topLeft.x + (col * brickWidth),
topLeft.y + (row * brickHeight), brickWidth, brickHeight )
brick:setFillColor(math.random(50, 255), math.random(50, 255),
math.random(50, 255), 255)
brick.type = “destructible”
physics.addBody(brick, “static”, {friction=0, bounce = 1})
end
end
end
function createWalls()
local wallThickness = 10
– Left wall
local wall = display.newRect( 0, 0, wallThickness,
display.contentHeight )
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Top wall
wall = display.newRect(0,0, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Right wall
wall = display.newRect(display.contentWidth – wallThickness, 0,
wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Bottom wall
wall = display.newRect(0, display.contentHeight – wallThickness,
display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
wall.type = “bottomWall”
end
main()
Jameel, please post that in the Ansca Mobile forum, it is much easier to go back and forth and view your code properly that way. Someone (me or others) will be able to help you there.
Hello Mr. Peach
You are a good teacher, you explain things very well, and you are good in explaning. Thanks again from the bottom of my heart.
The Ansca Mobile froum is locked. How can i get in. Plz help me.
Thanks
Warm Regds
Jameel
Hey Jameel, Peach is my first name and I’m female, just to clear that up, haha.
The forum you should be able to post in once you are logged into your account. If you are not allowed to post there may be an activation link in your email you have missed so you should double check that