Jan 302012
 

How to display and update a score in your Corona SDK application.

Obviously a score is a very important part of most games. Once you know how to create a score it’s very simple, however it can still be overwhelming for new users.

This plug and play code will demonstrate a nice, newbie friendly way of displaying a score, adding to it and updating the displayed number accordingly.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

--Create the background and make it blue
local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor( 150, 180, 200 )

--Start the score at 0
local score = 0

--Create score text and make it dark gray
local scoreText = display.newText(score, 200, 20, native.systemFont, 24)
scoreText:setTextColor( 80, 80, 80 )

--Function to add to score and update scoreText
local function addToScore()
score = score + 1
scoreText.text = score
end
bg:addEventListener("tap", addToScore)

Enjoy!

 

Peach Pellen :)

 

Like this post? Subscribe to the monthly newsletter!

 

  9 Responses to “A Simple Score System”

  1. I posted in your thread and will continue that convo there :)

    RE the blog, I haven’t read it yet – you would want to ask in the comments or in a thread.

  2. one more thing,

    just noticed this posted on todays blog

    Tutorial: Property Callbacks
    http://blog.anscamobile.com/2012/05/tutorial-property-callbacks/

    it look like it will do the job, but I’m unable to figure out how to program it to start at 100 life and then create a box in the main.lua file that if clicked will remove the health.

    any ideas?

  3. just posted.

    http://developer.anscamobile.com/forum/2012/05/01/keeping-score-health-storyboard

    i have tried doing it with health.lua as a external module. i assume thats a better way ??

  4. im not sure how to access the data in other storyboards. I’ve been trying for days and I’m stuck.

    first i tried to get a inventory set up with storyboard (like a point and click adventure game, but i had problems figuring out how to access code from each storyboard.

    i figured i could first start out with a health status as it would be more simpler then a table and learn how to access data using params. but i got confused with that.

    ill post on the forums and send you a link in the next post

  5. hey peach, always a fan of your great tutorials.

    just wondering how to implement this into a storyboard board.

    im assuming this code would be placed in the main.lua and i would need to somehow write and read from it in the scene1.lua , scene2.lua and etc files

    • Thanks Emanouel,

      You would just put the same code into Storyboard scenes/boards, yes – you’d just have to move it around a little and put things into the appropriate group. (Not localGroup, obviously.)

      If you have issues post on the forum and will see what I can do to help :)

  6. Thanks Peach! Simple, yet awesome :-) Thanks for sharing this.

  7. THIS is awesome! Peach you’re fan-freakin-tastic! Keep ‘em comin!

    • Thanks Tadd, I’m really happy to hear that – it’s simpler than most of my other recent tutorials and I was hoping people would see it as useful :)

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.