From Test-Scratch-Wiki
This tutorial explains how to make a custom timer that can be paused and restarted.
Programming
Three variables are needed:
- "(myTimer)"
— the custom timer
- "(timeStopped)"
— the timer to be incremented while the custom timer is paused
- "(isStopped?)"
— a boolean that determines whether the custom timer is paused
Three broadcasts are used:
- "reset"
— resets all counters
- "stop"
— pauses the custom timer
- "start"
— starts the custom timer
when gf clicked broadcast [reset v] //only include this if you would like the timer to be reset upon the green flag being clicked forever if <(isStopped?) = [1]> then //if the timer is stopped set [timeStopped v] to ((timer) - (myTimer)) //the time stopped is equal to the value of the timer minus the value of the custom timer else //if the timer is not stopped set [myTimer v] to ((timer) - (timeStopped)) //the custom timer's value is equal to the timer's value minus the time stopped end end when I receive [reset v] //reset all counters without disturbing "isStopped?" reset timer set [myTimer v] to [0] set [timeStopped v] to [0] when I receive [stop v] set [isStopped? v] to [1] //have the timer be stopped when I receive [start v] set [isStopped? v] to [0] //have the timer be unstopped
Example Uses
A few possible uses for a stoppable timer include:
- A racing game
- Timed events
- Pausing while using a timer