Trouble Creating a Countdown Timer

I’m trying to create a timer that will countdown from 60 seconds to o seconds using text display. I’ve been trying various methods but none of them have worked this is what I have so far that does work and run:

– Timer for Game

local timer = 60

timerText = display.newText( " " … timer,-15, 210, Font, 45)
timerText:rotate(90)
timerText.isVisible = false

– Hide Timer Until Ready
local function showtimer (event)
timerText.isVisible = true
end
background:addEventListener ( “touch”, showtimer )

what else should I add in order to create the countdown sequence? [import]uid: 71568 topic_id: 12541 reply_id: 312541[/import]

Try This
[lua]local timerTicks = 5
local timerText = display.newText( "time: " … timerTicks,120, 210, Font, 45)
timerText:rotate(90)
timerText.isVisible = false

function updateTime()
timerTicks = timerTicks - 1
timerText.text = "time: " … timerTicks
end

local function showtimer (event)
if event.phase == “ended” then
timerText.isVisible = true
local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )
end
end
Runtime:addEventListener ( “touch”, showtimer )[/lua] [import]uid: 71210 topic_id: 12541 reply_id: 45849[/import]

I tried running the code above but although I think it’s accurate the text is not being displayed. Does anyone know why this is happening? [import]uid: 71568 topic_id: 12541 reply_id: 45870[/import]

the text will be shown only when u click the screen…if its still not coming just check the location of the text is with in the screen bounds or check whether any error message is generated in the terminal window. [import]uid: 71210 topic_id: 12541 reply_id: 45873[/import]

Runtime error
/Users/marthwolf/Desktop/Bounce Squirrel/main.lua:315: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
I get this runtime error message but it has nothing to do with your code
it refers to this:

–Remove Text Upon Touch for the later timer
local function killinstructions (event)
instructions:removeSelf()
end

background:addEventListener (“touch”, killinstructions)

Is there a way to correct this error ? [import]uid: 71568 topic_id: 12541 reply_id: 45875[/import]

Just make sure “instructions” is active at the time when you call removeSelf(). you may be trying to remove instructions when it is not available. I can’t find declaration for instructions in your code.

else put
[lua]if instructions then
instructions:removeSelf()
end[/lua] [import]uid: 71210 topic_id: 12541 reply_id: 45876[/import]

I think its occurring because ur given the code in touch event.
if inside touch event you should give the event function as

[lua]local function killinstructions (event)
if event.phase == “ended” then
instructions:removeSelf()
end
end[/lua]

or else use “tap” instead of touch.the touch event will be called many number of times as the same event is used to get move events also.

[import]uid: 71210 topic_id: 12541 reply_id: 45877[/import]

The main problem was the removeSelf method but I fixed that by using instructions.isVisible = false

because the terminal kept saying removeself had a nil value,

After fixing that I changed some of the code values for the timer you gave me and then the timer was visible, the only problem I’ve noticed so far is that in the Corona Simulator the timer moves slowly and then very quickly, but I’m going to try to build it for my device and maybe on the device there won’t be this problem I’ll let you know.

Do you know a way to pause all physics on screen when the timer = 0? [import]uid: 71568 topic_id: 12541 reply_id: 45907[/import]

you may use

[lua]function updateTime()
timerTicks = timerTicks - 1
timerText.text = "time: " … timerTicks
if timerTicks == 0 then
physics.stop()
end
end[/lua]
also if you put isVisible the object will still be available in memory and it will lead to memory leak.
[import]uid: 71210 topic_id: 12541 reply_id: 45908[/import]

I built the code for my device and for some reason every time you tap the screen the timer winds down faster so in a matter of 10 seconds it goes from 60 to 50 and then down to 0 in 10 second, my app is very interactive so the screen will be getting tapped many times while the timer is winding is there a way to fix this? [import]uid: 71568 topic_id: 12541 reply_id: 45909[/import]

oops you should remove the event listener once the timer is shown…
use
[lua]local function showtimer (event)
if event.phase == “ended” then
Runtime:removeEventListener ( “touch”, showtimer )
timerText.isVisible = true
local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )
end
end
Runtime:addEventListener ( “touch”, showtimer )[/lua] [import]uid: 71210 topic_id: 12541 reply_id: 45911[/import]

That solved the problem, do you know how to make the timer stop winding down at zero? [import]uid: 71568 topic_id: 12541 reply_id: 45916[/import]

we are calling the function like
local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )

the function will be called timerTicks number of time so it won’t go down than 0. [import]uid: 71210 topic_id: 12541 reply_id: 45918[/import]

ERROR: Cannot translate an object before collision is resolved.
ERROR: Cannot translate an object before collision is resolved.
/Applications/CoronaSDK/Corona Terminal: line 9: 3691 Bus error “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
logout
When the corona physics engine stops the simulator crashes, is there a way to fix this? [import]uid: 71568 topic_id: 12541 reply_id: 45925[/import]

try using physics.pause() [import]uid: 71210 topic_id: 12541 reply_id: 45929[/import]

It solved the crashing problem I was having. Since I have a score in my game is there a way to display the final score once the time=0?

Thank You very much for your help technowand. [import]uid: 71568 topic_id: 12541 reply_id: 45938[/import]

is this what you want ?

[lua]local timerTicks = 5
local timerText = display.newText( "time: " … timerTicks,120, 210, Font, 45)
timerText:rotate(90)
timerText.isVisible = false

score = 100
local scoretext = display.newText( "Score: " … score,120, 90, Font, 45)

function updateTime()
timerTicks = timerTicks - 1
timerText.text = "time: " … timerTicks
if timerTicks == 0 then
scoretext.text = "Score: "…120
end
end

local function showtimer (event)
if event.phase == “ended” then
Runtime:removeEventListener ( “touch”, showtimer )
timerText.isVisible = true
local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )
end
end
Runtime:addEventListener ( “touch”, showtimer )[/lua] [import]uid: 71210 topic_id: 12541 reply_id: 45983[/import]

What I mean is a way for my application to save the highest score once the timer equals zero out of all the scores made and update this “highscore” once the timer hits zero if this is my score code:
local points = -20

scoreText = display.newText( " " … points,288, 225, Font, 20)
scoreText:rotate(90)

… I would also have a highscoreText= diplay.newText
that would be updated to save the highest score out of multiple application uses, but also once the timer hits zero a pop up that would display their score for that game session [import]uid: 71568 topic_id: 12541 reply_id: 46115[/import]

you can store the highest score in a file and read from there. That file can be updated when your current score is greater than what it is stored. if you just have one score to save i recommend you use this class library.it is very handy and easy to use.
http://developer.anscamobile.com/forum/2011/03/13/propertybag-all-your-data

just put the lua file in your application folder and use it like below
[lua]local prop = require(“property”)
local propertyBag = prop:init()

–Properties created in memory
propertyBag:setProperty(“HighScore”, yourscore)

–Save this to the disk
propertyBag:SaveToFile()

–read saved data from Disk
propertyBag:GetFromDisk()

print ( propertyBag:getProperty (“HighScore” ))[/lua] [import]uid: 71210 topic_id: 12541 reply_id: 46155[/import]

– Add Score

local points = -20

scoreText = display.newText( " " … points,288, 225, Font, 20)
scoreText:rotate(90)
localGroup:insert(scoreText)

– Add Collision Event
squirrel.myName = “squirrel”
acorn.myName = “acorn”

– Collision detection! Explained below in detail
squirrel:addEventListener(“collision”, squirrel)

function squirrel:collision (event)
if event.other.myName == “acorn” then
points = points + 10
scoreText.text = "Score: " … points
local crack = media.newEventSound (“chop.wav”)
media.playEventSound (crack)
end
end

– Highscore

local function save( event ) – function to save score
local path = system.pathForFile( “Highscore.txt”, system.DocumentsDirectory ) – where the file will be saved
local file = io.open( path, “w+b” ) – opening the file

file:write(Highscore …"") – writing the variable ‘score’ in the score.txt file
io.close( file ) – closing the file
– Saves our data

end

local function resumeStart()
local path = system.pathForFile( “Highscore.txt”, system.DocumentsDirectory ) – where the file will be saved
local file = io.open( path, “r” ) – opens the file under the variable file

if file then – if there is a file then
local contents = file:read( “*a” ) – read the contents of the file, and read the whole string(*a)

local prevState = explode(", ", contents) – put the contents of score.txt into a table
print(‘file’)
Highscore = prevState[1] – read the table at position 1 and assign the variable score

io.close( file ) – close file

else – if there is no file
Highscore=0 – the score then starts at 0
end
end

–ANSCA sample code–
function explode(div,str)
if (div==’’) then return false end
local pos,arr = 0,{}
– for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) – Attach chars left of current divider
pos = sp + 1 – Jump past current divider
end
table.insert(arr,string.sub(str,pos)) – Attach chars right of last divider
return arr
end
–ANSCA sample code^–

resumeStart() – call the starting function

local scoretext = display.newText(‘Highscore:’,258,60,native.systemFont,20)
scoretext:rotate(90)
local t = display.newText(’’,258,80,native.systemFont,20) – score text
t.text=Highscore – update t to the score

save() – calls save, so when the user reloads the application the score is still there
t.text=Highscore – updates t to reflect new score
–This is what I have so far but it is not working, it links the scores and chooses the highest one but I have not found a way to link my displayed score to the highscores list, I think i can get it if I just change some values
do you know how to do this?

[import]uid: 71568 topic_id: 12541 reply_id: 49051[/import]