Hi everyone.
I’m new with corona sdk and a newbie programming with lua code.
My first lines of code are about a countdown timer, and the objective is to create a timer settable by user.
Here is the lines of code:
display.setStatusBar(display.HiddenStatusBar) local widget = require( "widget" ) local button1,button2,clockTimer local runMode = true -- for Pause/Resume button state local started = false --The number of minutes remaining local minuteField = display.newText( "", 0, 0, native.systemFontBold, 50) minuteField:setTextColor( 20, 0, 0, 65 ) minuteField.x, minuteField.y = 150, 50 local secondField = display.newText( "", 0, 0, native.systemFontBold, 50) secondField:setTextColor( 20, 0, 0, 65 ) secondField.x, secondField.y = 200, 50 --Function to update the countdown clock local function updateTime() local time = os.date("\*t") local minuteText = 60 - time.min if (minuteText \< 10) then minuteText = "0" .. minuteText end minuteField.text = minuteText local secondText = 60 - time.sec if (secondText \< 10) then secondText = "0" .. secondText end secondField.text = secondText end local buttonHandler1 = function( event ) local result if runMode then button1:setLabel( "Resume" ) runMode = false result = timer.pause( clockTimer ) else button1:setLabel( "Pause" ) runMode = true result = timer.resume( clockTimer ) end if started == false then button1:setLabel( "Pause" ) runMode = true secondText = "0" minuteText = "0" clockTimer = timer.performWithDelay( 1000, updateTime, -1 ) started = true end end -- Cancel timer local buttonHandler2 = function( event ) local result, result1 result, result1 = timer.cancel( clockTimer ) button1:setLabel( "Start" ) started = false secondText = "0" minuteText = "0" end button1 = widget.newButton { id = "button1", defaultFile = "buttonBlue\_100.png", overFile = "buttonBlueOver\_100.png", label = "Pause", font = native.systemFontBold, fontSize = 22, emboss = true, onRelease = buttonHandler1, } button2 = widget.newButton { id = "button2", defaultFile = "buttonBlue\_100.png", overFile = "buttonBlueOver\_100.png", label = "Cancel", font = native.systemFontBold, fontSize = 22, emboss = true, onRelease = buttonHandler2, } button1.x = display.contentCenterX; button1.y = 360 button2.x = display.contentCenterX; button2.y = 430 --Update the time once immediately to display the correct time updateTime() -- Update the clock once per second clockTimer = timer.performWithDelay( 1000, updateTime, -1 )
I have some problems that i’m trying to resolve, but if the experts can give me an hand helping me would be great!
The problems are:
1 - I need to make the timer at 0 at the start, and make the timer change after the user chose the time to countdown. How can i do for make the user able to select time? I saw that selectbox doesn’t exist in lua. Should i use PickerWeel? The current numbers of the timers are only indicative.
2- This timer now run automatically when i open the simulator. I want a stopped timer that run only after i tap on the start button. I saw that if i change to false " started " or " runmode " the timer still run at the start. What should i change?
3- With this timer, if i tap on the " Pause " button, the Timer go on pause, but only aesthetically and not logically! Then if i tap on the Resume button, the Timer will resume jumping some number, like if the countdown never paused! How can i do for make the Timer totally in pause?
4-If i tap on the cancel button, the timer doesn’t cancel, but the timer goes on pause, but a pause like the pause of the third problem, of course! How can i do for makin the Timer totally cancelled after the tap?
I know that these can seems stupid problems, is my first project and i’m trying to do it better as possible, but i’m new with this programming code, and any help with those problems would be very great!
I’m sorry for the incovenience and for the not-perfect written post! Really thanks to everyone who will answer this post!