Wowsers... as Craig says please DO use the code tags!
For starters, you've gone the long way about ensuing the numbers have a leading 0 where needed.
LiveCode has you covered with the
format function (you can read up on it in the dictionary).
the long and short of it is if you want to pad a single integer with a leading 0 for a 2-digit number, set that number to:
Code: Select all
format("%02u",<the number in question>)
This means that if the number in quest is for example 7, the following will return 07:
Code: Select all
format("%02u",7) -- returns 07
-- or --
format("%02u",7 - 1) -- returns 06
Your code struggles bit if there isn't an initial value set in the TIMER field. But more than that, you are making your life more difficult by trying to do everything in a single mouseDown handler 'just because you can'.
Think about it: if you want things to happen while the user is actually clicking the mouseDown button then yeah that's fine. But in your case, what you want is to respond to a mouseUp and respond differently to a
second mouseUp, depending on whether the timer is counting down.
Everything is much easier if you just track what the field is doing and act accordingly. This is very easy to do with a
script local variable, in other words a variable that is available to ALL the handler in a particular script (In this case the button script) and persists while the app is running, so it remembers what you put in it for subsequent operations.
So if the flag is true (ie timer is running) you want to stop it and reset it; and if not, you want to start it. But you also want perhaps a different outcome if the timer runs out (for example change the label of the button to "TIME"S UP" or do some other action).
In the script below the flag that tracks if the timer is running is sRunning. I also put the default initial mins and secs script local variables so you can change them in one place.
The mouseUp handler just checks if the timer is running (in which case it calls resetCounter to stop/reset) and if not then it calls countDown, to start the countDown. the code below shortens the script you used to pad with a leading 0 if needed as above but is more verbose and easier to change if needed. This is the script for button "Avvio" and assumes there is a field called "Timer"
Code: Select all
local sRunning
local sMinutes = 01
local sSeconds = 20
on mouseUp
if sRunning then
resetCounter
else
countDown
end if
end mouseUp
command resetCounter
beep
put false into sRunning -- ie mark the timer as not running
set the label of button "Avvio" to "START"
put format("%02u",sMinutes) & ":" & format("%02u",sSeconds) into field "Timer"
exit to top -- stops everything
end resetCounter
command countDown
local tMinuti, tSecondi
put true into sRunning -- mark the timer as running
set the itemDelimiter to ":"
put item 1 of field "timer" into tMinuti
put item 2 of field "timer" into tSecondi
set the label of button "Avvio" to "STOP"
repeat
wait 1 second with messages // 'with messages' ensures interface updates
if tSecondi <> 0 then
put format("%02u", tSecondi -1) into tSecondi // decrememnt seconds and format with padding 0 if needed
put tSecondi into item 2 of field "timer"
else
if tMinuti = 0 then
set the label of button "Avvio" to "TIME'S UP" -- or trigger some other action
exit to top // stops everything
else
put 59 into tSecondi
put tSecondi into item 2 of field "timer"
put format("%02u", tMinuti -1) into tMinuti // decrememnt minutes and format with padding 0 if needed
put tMinuti into item 1 of field "timer"
end if
end if
end repeat
end countDown
Hope that makes sense
Stam