[MWForum]Re: when and if
Mike Sandy
mwforum@lists.mathcats.com
Sat, 23 Nov 2002 22:23:15 -0000
Wendy wrote:
"I created a typing race game which would display words to be typed,
along with a textbox in which the user would type. I wanted a timer to
start the moment a user began to type in the empty user textbox, and I
wanted the timer to stop after 2 minutes and then announce the user's
number of words per minute."
..
to start.typing
resett
usertextbox, ct
when [not empty? usertextbox] [resett]
when [timer > 1200]
[announce [Time's up!]
score
comment
stopall]
end
Both of the "when" commands run constantly until the "stopall" command
is reached.
..."
Some further points about WHEN
Using Wendy's typing example (modified):
to aa
dolist[i [text1 text2]][tto :i ct]
when [not empty? text1]] [start.timer stopme]
end
to start.timer
resett
when [0 = remainder timer 5]
[settext2 count parse text1]
when [timer > 1200]
[settext2 (count parse text1) / timer * 600
announce [STOP TYPING]
stopall]
end
Though not important in this example, the larger the number
of independent processes running, the slower
the overall response times. In the modified program the WHENs only run
when required.
There can be some advantage in converting the WHEN conditions
to global variables particularly if the conditions are complicated
and CANCEL is used. This also has the advantage that, being in one place,
the conditions can be readily changed.
Thus:
to aa
dolist[i [text1 text2]][tto :i ct]
make "c1 [not empty? text1]
make "c2 [0 = remainder timer 5]
make "c3 [timer > 300]
when :c1 [start.timer stopme]
end
to start.timer
resett
when :c2[settext2 count parse text1]
when :c3[settext2 (count parse text1) / timer * 600
announce [STOP TYPING]
stopall]
end
Mike