Q. I’d like to have a counter on the screen go backwards every time my guy gets killed — a number that shows the lives you have left. How do you do that?
A. While there are a lot of ways this could be done, I’m going to show you the fastest and easiest way. It takes advantage of the fact that Lua is a “typeless” language. In many languages you might specify the variable livesLeft is an integer. Then you can add or subtract or do any other mathematical functions. But you wouldn’t be able to display it unless you changed it to a string type.
With a typeless language you can treat the variable livesLeft as an integer at one point and as a string at another point. There are pros and cons to using a typeless language, but today we’re going to look at one of the pros.
Our tiny example program is going to show the number 3 in the center of the screen and whenever you tap the screen it decrements by one, until it gets to zero. At that point further taps don’t do anything.
Here’s the entire code you need to test this.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
local crawlspaceLib = require("crawlspaceLib") display.setStatusBar(display.HiddenStatusBar) local livesLeft = display.newText( "3", centerX, centerY, native.systemFont, 24 ) local screenTapped = function(event) if event.phase == "began" and tonumber(livesLeft.text) > 0 then livesLeft.text = livesLeft.text - 1 end end Runtime:addEventListener("touch", screenTapped) |
The first line isan’t abolutely necessary, but the Crawl Space Lib is free and gives you a lot of handy shortcuts, such as using centerX and centerY when we display the text.
Here’s where you can get the Crawl Space Library:
http://www.crawlspacegames.com/crawl-space-corona-sdk-library/
After that we hide the status bar just to be all nice and neat and then display the number 3 on the screen using display.newText. You can see that we assigned that text object to the variable called livesLeft.
Next we define a function called screenTapped and that’s where the magic happens. We first check to see whether the tap is just starting (event.phase == “began”) and whether the livesLeft.text property is still above zero.
Then we just subtract one from the variable: livesLeft.text = livesLeft.text – 1 — that’s where we’re treating the text version of the number as an actual number.
The last line of the program sets up an event listener that waits for a tap on the screen and when that happens, calls the screenTapped function.
But let’s look back at the first line in that function. If we can treat livesLeft.text as a number when we subtract one, why can’t we use it as a number to see if it’s greater than zero?
Actually, I don’t know why the difference, I just know that I had to wrap livesLeft.text inside the tonumber() function to get it to work. 😉 The tonumber() function takes whatever is passed and “casts” it as a number.
There’s just a little chunk of code but you could tweak it into the manager that handles the lives left part of your game.
So I’m thinking instead of this:
local livesLeft = display.newText( “3”, centerX, centerY, native.systemFont, 24 )
write this:
local livesLeft = 3
display.newText( livesLeft, centerX, centerY, native.systemFont, 24 )
then you should be able to check the variable against zero without converting it. I haven’t tested it but seems like it would work.