Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Trouble Creating a Countdown Timer
Started by jmmaceves Jul 16 2011 05:27 AM

- - - - -
37 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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?
uid: 71568 topic_id: 12541 reply_id: 312541


[TOPIC: post.html]
#2

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

Try This
local timerTicks = 5local timerText = display.newText( "time: "  .. timerTicks,120, 210, Font, 45)timerText:rotate(90)timerText.isVisible = falsefunction updateTime()	  timerTicks = timerTicks - 1   timerText.text = "time: " .. timerTicksendlocal function showtimer (event)  if event.phase == "ended" then    timerText.isVisible = true    local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )    endend   Runtime:addEventListener ( "touch", showtimer )
uid: 71210 topic_id: 12541 reply_id: 45849


[TOPIC: post.html]
#3

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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?
uid: 71568 topic_id: 12541 reply_id: 45870


[TOPIC: post.html]
#4

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

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.
uid: 71210 topic_id: 12541 reply_id: 45873


[TOPIC: post.html]
#5

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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 ?
uid: 71568 topic_id: 12541 reply_id: 45875


[TOPIC: post.html]
#6

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

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
if instructions then   instructions:removeSelf()end
uid: 71210 topic_id: 12541 reply_id: 45876


[TOPIC: post.html]
#7

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

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

local function killinstructions (event)  if event.phase == "ended" then         instructions:removeSelf()  endend


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.

uid: 71210 topic_id: 12541 reply_id: 45877


[TOPIC: post.html]
#8

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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?
uid: 71568 topic_id: 12541 reply_id: 45907


[TOPIC: post.html]
#9

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

you may use

function updateTime()      timerTicks = timerTicks - 1     timerText.text = "time: " .. timerTicks   if timerTicks == 0 then      physics.stop()   endend

also if you put isVisible the object will still be available in memory and it will lead to memory leak.
uid: 71210 topic_id: 12541 reply_id: 45908


[TOPIC: post.html]
#10

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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?
uid: 71568 topic_id: 12541 reply_id: 45909


[TOPIC: post.html]
#11

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

oops you should remove the event listener once the timer is shown...
use
local function showtimer (event)  if event.phase == "ended" then   Runtime:removeEventListener ( "touch", showtimer )    timerText.isVisible = true    local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )    endend   Runtime:addEventListener ( "touch", showtimer )
uid: 71210 topic_id: 12541 reply_id: 45911


[TOPIC: post.html]
#12

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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


[TOPIC: post.html]
#13

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

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.
uid: 71210 topic_id: 12541 reply_id: 45918


[TOPIC: post.html]
#14

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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?
uid: 71568 topic_id: 12541 reply_id: 45925


[TOPIC: post.html]
#15

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

try using physics.pause()
uid: 71210 topic_id: 12541 reply_id: 45929


[TOPIC: post.html]
#16

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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.
uid: 71568 topic_id: 12541 reply_id: 45938


[TOPIC: post.html]
#17

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

is this what you want ?

local timerTicks = 5local 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   endendlocal function showtimer (event)  if event.phase == "ended" then   Runtime:removeEventListener ( "touch", showtimer )    timerText.isVisible = true    local timeTaken = timer.performWithDelay( 1000, updateTime, timerTicks )    endend   Runtime:addEventListener ( "touch", showtimer )
uid: 71210 topic_id: 12541 reply_id: 45983


[TOPIC: post.html]
#18

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

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
uid: 71568 topic_id: 12541 reply_id: 46115


[TOPIC: post.html]
#19

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

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
local prop = require("property")local propertyBag = prop:init()--Properties created in memorypropertyBag:setProperty("HighScore", yourscore)--Save this to the diskpropertyBag:SaveToFile()--read saved data from DiskpropertyBag:GetFromDisk()print ( propertyBag:getProperty ("HighScore" ))
uid: 71210 topic_id: 12541 reply_id: 46155


[TOPIC: post.html]
#20

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

-- 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?

uid: 71568 topic_id: 12541 reply_id: 49051


[TOPIC: post.html]
#21

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

I've also noticed that technowand has a ebook on developing corona games would you recommend this book for semi-beginners?
uid: 71568 topic_id: 12541 reply_id: 49052


[TOPIC: post.html]
#22

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

@jmmaceves the book is actually a companion for Corona SDK game development workshop we conduct in Singapore and India. The workshop have already been attended by more than 50 aspiring developers most of them who are just beginners. We are selling the training material as it was requested by many who can't attend our workshop and are looking for e-learning solution. Now we are selling it for special discount price. An update version with all the recent additions to the SDK will be released next week and we will be publishing a hard cover version also which will of-course lead to a price increase. :)
uid: 71210 topic_id: 12541 reply_id: 49114


[TOPIC: post.html]
#23

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

I'll probably wait for the hardcover version, I've gotten most of my game done with online tutorials, the only thing I really need to finish it is to figure out a way to link my highscore text that is continous between gameplays to my score text which is new each gameplay
uid: 71568 topic_id: 12541 reply_id: 49115


[TOPIC: post.html]
#24

renvis@technowand

[GLOBAL: userInfoPane.html]
renvis@technowand
  • Contributor

  • 576 posts
  • Corona SDK

@jmmaceves can you put your code inside lua tag... its very difficult to read now.
uid: 71210 topic_id: 12541 reply_id: 49117


[TOPIC: post.html]
#25

jmmaceves

[GLOBAL: userInfoPane.html]
jmmaceves
  • Observer

  • 29 posts
  • Corona SDK

@technowand

what do you mean by "lua tag"?
uid: 71568 topic_id: 12541 reply_id: 49118



[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]