ok robert so i got it so far. now i’m trying to insert buttons to start, stop, and reset. i know i have to use the ui.lua and i tied to copy the code from the button code example.
i got the button to display but i’m sure the buttonHandler is wrong. care to help? THANKS!!!
here’s my code. (i also managed to display a “0” before the single digit seconds)
[lua]local ui = require(“ui”)
local buttonHandler = function( event )
timer.cancel(countdownTimer)
end
local button1 = ui.newButton{
default = “buttonBlue.png”,
over = “buttonBlueOver.png”,
onEvent = buttonHandler,
id = “button1”,
text = “Stop”,
font = “Trebuchet-BoldItalic”,
textColor = { 51, 51, 51, 255 },
size = 22,
emboss = true
}
–Local variables for minutes, seconds, and tenth of seconds
local minutes = 1
local seconds = 30
local tenseconds = 0
–Countdown timer variable
local countdownTimer
–Our display that will get updated showing the time
local timeDisplay = display.newText(“1:30.0”,100,100,native.systemFont,30)
–Function that gets fired every tenth second.
local function time()
–The actual thing
–Nothing will be updated unless a second is up, in the which case there won’t be any tenth seconds
if (tenseconds == 0) then
–If there’s 0 ts (tenth seconds), change it to 9
tenseconds = 9
–Now that the ts are taken care of, if there’s 0 seconds…
if (seconds == 0) then
–Change it to 59 seconds and subtract a minute
seconds = 59
minutes = minutes - 1
else
–If there’s not 59 seconds, just subtract a second
seconds = seconds - 1
end
else
–This is if the ts is not at 0, just subtract 1 ts
tenseconds = tenseconds - 1
end
–Update the text display
timeDisplay.text = minutes…":"…seconds…"."…tenseconds
if (minutes < 10) then timeDisplay.text = “0”…minutes…":"…seconds…"."…tenseconds end
if (seconds < 10) then timeDisplay.text = minutes…":"…“0”…seconds…"."…tenseconds end
–If there’s no longer any minutes, seconds, or tenth seconds, stop the timer and show an alert
if (minutes == 0 and seconds == 0 and tenseconds == 0) then
timer.cancel(countdownTimer)
native.showAlert("",“Time’s up!”,{“OK”})
end
end
–Run the timer infinitely (it stops when time runs out), every 100ms which is 1/10th of a second.
countdownTimer = timer.performWithDelay(100,time,0)
button1.x = 160; button1.y = 160[/lua] [import]uid: 41923 topic_id: 7486 reply_id: 26705[/import]