Stopping a forever loop with buttons

瀏覽次數:14 次
跳到第一則未讀訊息

Lauren Rennison

未讀,
2020年11月9日 晚上11:08:312020/11/9
收件者:Pencil Code
I would like my students to create virtual alarms with Pencil Code.

I am stuck on a problem where I have various buttons: 
one button is to confirm everything is okay;
the second button sets off the alarm in a forever loop (I don't want it to stop until another button or key is pressed);
the third button resets the alarm (with the hope of overriding the forever loop)

I have tried various different loop options and also tried the stop() code but I can't get anything to work the way I have in mind.

My code so far is this: 

start = ->
  button 'Click this if everything is okay!', ->
    wear 'smile'
  button 'Set off alarm', ->
    forever 1, ->
      dot green, 30
      pause 0.1
      dot red, 30
      pause 0.1
  button 'Reset alarm', -> 
    wear 'turtle'
    cs()
    do start
 
do start

Is there a way that I can add or change something something in either of the the 'Reset alarm' or the 'Set off alarm' buttons to make it so that if I press a key on the keyboard or click the third button, the whole code clears the screen and goes back to the starting point? Otherwise I have to use the usual PencilCode stop/play buttons or refresh the page entirely.

Also please let me know if this might also be affected by any sound effects (tone or play).

Thanks,

Lauren
回覆所有人
回覆作者
轉寄
0 則新訊息