Help with timers.

OK so i am creating a timer to countdown until a set date and i was wondering if there is a way to set it so that it keeps running and doesnt restart every time the application closes because the code i am using now restarts every time you open the app and like i said i was wondering if there is a way to keep it running. For example if i were to make a countdown to to christmas or another holiday and i want to make a countdown to there, and becausethe code i have now it restarts everytime. If someone could help me out with the code or give me a link to a tutorial, that would be greatly appreciated.
Also: sorry if my post is hard to understand :confused: [import]uid: 69054 topic_id: 18682 reply_id: 318682[/import]

Hey there,

You can’t keep it running when the app closes, although you could keep track of how much time had passed and save that, then load it and go from there.

I actually made a count down app, not for the store but for a friend - I just grabbed the date and time off his device. Could that work for you?

Peach :slight_smile: [import]uid: 52491 topic_id: 18682 reply_id: 71818[/import]

Sure thanks but could i just have the code so that i can implement it into the app im trying to make and modify it?
[import]uid: 69054 topic_id: 18682 reply_id: 71931[/import]

you need to write something that mimics “how to save” high scores to a data file and retrieve them upon the app start up.

logic would be something like this

APP START

CHECK If FILE EXSISTS.

IF File does not exsists then create it (put system time/date in it)
ELSE
IF File does exsists then get system time/date information from file.
ELSE (IF the file exisists AND
If the file’s system time/date info is NOT valid THEN create DEFAULT system time/date in it.

Peach has written a very simple scoring code that “remember” the highscore in a game
its in the “public available coding” area.

This would be a great place to start(any project).
get this code and try to get it to work as a scoring mechanism and then tweak it to the ABOVE game logic.

This is how I learn. (IE by example code)

Especially by example code from other’s who are way more brilliant than myself
(Peach P. and Johnathan B. are two well know programmers/developers who have created some great code for all us mere mortals)

Im by no means a genius or a whiz kid. But I hope this points you to a general direction.
[import]uid: 11094 topic_id: 18682 reply_id: 71942[/import]

i get were your going with this but i have absoulutaly no experience with loading, and requesting info on another file. [import]uid: 69054 topic_id: 18682 reply_id: 71970[/import]

Hey peach if you could give me that code that would be awesome, thanks. [import]uid: 69054 topic_id: 18682 reply_id: 71971[/import]

Hey again,

The code doesn’t exist any more - it was on my old laptop which unfortunately fell about 1.5ft and ruined the drive. (D’oh!)

However I have been meaning to do a new tutorial on Techority for a bit and this could be suitable.

If you like Techority on Facebook you’ll see when I post it; http://www.facebook.com/pages/Techority/154671737922696

I’ll try to update this thread too, if I remember :wink:

Peach

PS - Will aim to post it in the next day, time permitting.
PPS - It doesn’t involve any saving or loading of data. [import]uid: 52491 topic_id: 18682 reply_id: 72053[/import]

hope this helps

below is the code I pulled from the PUBLIC code area on the corona website.

– this is just to keep track of the score
local function save( event )

– where the file will be saved
local path = system.pathForFile( “score.txt”, system.DocumentsDirectory )

– opening the file
local file = io.open( path, “w+b” )

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

local function resumeStart()

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

– if there is a file then
if file then
– read the contents of the file, and read the whole string(*a)
local contents = file:read( “*a” )
local prevState = explode(", ", contents) – put the contents of score.txt into a table
print(‘file’)
score = prevState[1] – read the table at position 1 and assign the variable score
io.close( file ) – close file
else – if there is no file
score=0 – the score then starts at 0
end
end

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

resumeStart() – call the starting function

local scoretext = display.newText('Score: ‘,200,20,native.systemFont,20)
local t = display.newText(’ ',280,20,native.systemFont,20) – score text
t.text=score – update t to the score

local function resetScore() – function called when user presses the add button
score=0 – sets score to 0
save() – calls save, so when the user reloads the application the score is still there
t.text=score – updates t to reflect new score
end

[import]uid: 11094 topic_id: 18682 reply_id: 72098[/import]

Hey Troy, could you please try to post your code in < code > or < lua > tags? (It makes it easier to read.)

Anyway, countdown app, right here;

http://techority.com/2011/12/07/making-a-countdown-app-with-corona-sdk/

Peach :slight_smile:

PS - This one counts down to Christmas so is appropriate for the time of year. [import]uid: 52491 topic_id: 18682 reply_id: 72108[/import]

Thank you so much, i really appreciate the help here. [import]uid: 69054 topic_id: 18682 reply_id: 72109[/import]