Stopping a forever loop with buttons

8 views
Skip to first unread message

Lauren Rennison

unread,
Nov 9, 2020, 11:08:31 PM11/9/20
to 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
Reply all
Reply to author
Forward
0 new messages