Page 1 of 1
timer/counter - new question
Posted: Fri Aug 03, 2007 6:20 pm
by homer
ok, I know I'm not there yet. Kind of feel dump, but I'll try again.
let's say I have a button for "start"/"stop"
I have a field I'll call "result"
when I click "start" I want to add 1 to the field "result"
1
wait 1 second
2
wait 1 second
3
and so forth.
I want this to go on forever, until I click "stop"
so, I tried to do something like
on mouseUp
repeat with i = 60
wait 1 second
put i into field "result"
end repeat
end mouse up
this works, however, while the script is adding 1 to the field, I can't click on "stop" when the number gets to say 34, or whatever.
how do I create something that I can start and stop at will? and of course I have a follow up. suppose I have three such buttons and corresponding result fields: start/stop 1, start/stop 2, start/stop 3... each with a different frequency, and have them all running at the same time, or have 1 and 3 running and 2 'idle", or 3 running and 1 and 2 "idle"
well, first things first... get one to work.
suggestions apprecitated.
homer
Posted: Fri Aug 03, 2007 10:38 pm
by Lynn P.
Hi homer ~
One approach to the first question:
Code: Select all
local lastNumber=1
on mouseUp
if lastNumber is not empty
then
repeat with i=lastNumber to 60
if the mouseClick is true
then
put fld "result" into lastNumber
exit mouseUp
else
put i into field "result"
wait 1 second
if i = 60
then
put 1 into lastNumber
end if
end if
end repeat
end if
end mouseUp
Posted: Sat Aug 04, 2007 4:46 am
by homer
thanks, but that didn't work.
I copied the script into a button. I created a field named "result"
when i clicked on the button, nothing happened.
more assistance would be appreciated.
thanks
homer
Re: timer/counter - new question
Posted: Sat Aug 04, 2007 9:29 am
by Mark Smith
Code: Select all
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter"
send "incFld" to me in 1 second
end if
end incFld
Best,
Mark
Posted: Sat Aug 04, 2007 1:55 pm
by Lynn P.
homer wrote:thanks, but that didn't work.
...
````````````````
Oops.

Forgot to initialize the local variable. It's fixed now and should work.
My solution only counts to 60 as you indicated in your own code, but Mark's suggestion will go on forever as you also stated you wanted it to do.
Posted: Sat Aug 04, 2007 7:15 pm
by homer
ok, I'm making progress... and learning
Thanks Mark for the help.
next thing is this.
I created a button with a label "start1"
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
set the backgroundcolor of me to "0,255,0"
set the label of me to "start1"
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter01"
send "incFld" to me in 1 second
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
end if
end incFld
This all works, however, when the button is clicked and the counter begins, there is a momentary delay before the button changes from label "start1" to "stop1" and from color green to red.
do I have the "set" in the wrong places?
Thanks again for your help.
Homer
Posted: Sun Aug 05, 2007 7:00 pm
by Mark Smith
Homer, I'd probably factor the colour changing etc. into a separate handler:
Code: Select all
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
updateMe
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter01"
send "incFld" to me in 1 second
end if
end incFld
on updateMe
if sRunning then
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
else
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
end if
end updateMe
best,
Mark
Posted: Mon Aug 06, 2007 6:46 pm
by homer
Mark,
Thanks for the suggestion. I will try that tonite. I had actually thought along those line -- a separate handler to "toggle" the state, but I wasn't quite sure where in the "mouseUp" it should go.
I'll let you know how it works out.
Thanks.
Homer
Possible Solution I Have Used
Posted: Mon Aug 06, 2007 9:02 pm
by deeverd
Hi Homer,
I'm still fairly new at programming so feel free to take my suggestion with a grain of salt, but I have created similar timers before.
If you want the timer to run until you click on something, why not just do:
repeat until mouseClick
Offhand, I can't remember if you need to put "the" in there as well, which would simply be:
repeat until the mouseClick
With such a script, your repeat will stop instantly the moment you click the mouse after the timer starts, and it doesn't matter what you click on.
Cheers, deeverd
Posted: Mon Aug 06, 2007 9:38 pm
by Janschenkel
Hi Homer and Deeverd,
While this 'repeat until the mouseClick' construct works and does stop the timer regardless of where the user clicks, this is an approach that eats up the CPU time as the engine is constantly evaluating the 'mouseClick' function, which will only give a result upon click.
This means that it will ask the operating system hundreds or even thousands of times per second if the user clicked the mouse by any chance.
It is more efficient to wait for the operating system to figure it out and send an event to the engine, which in turn dispatches a message that you handle.
See also this excellent article by Jacque Gay entitled 'Polling the Mouse in MetaCard and Revolution' :
http://www.hyperactivesw.com/polling.html
Hope this helped,
Jan Schenkel.