Typical delay in a While loop is used to create a digital and an analog stopwatch with three buttons: Start (or restart after a Pause), Pause and Reset. It is a simple use case of a While loop.
Recently I posted a sample project showing two stopwatches:
Although this feature appeared with the refurbished Advanced Actions dialog post in CP2017, I rarely see projects using the While loop except some games. Such a loop can be eternal or limited to a a specific number or repeating commands. It is always based on a condition, but the result is different from the older ‘IF’ condition.
- Eternal loop: uses the condition ‘IF 1 is equal to 1‘ or something similar, which results always in a positive result. The specified commands will repeat continuously. However! Contrary to the default behavior in advanced actions, where all commands run immediately and in sequence (top-down and left to right through the decisions), the commands in a While loop will be delayed for 1 second before the next run. It is this feature which I have used in this use case.
- Limited loop: will need at least one variable. The condition ‘IF var is less than 6′ is an example. The variable ‘var’ needs to start with a number which you define in the variable definition or by another action. The variable will also need to be changed inside the commands. In this case it will probably be done with ‘Increment’. If var starts at 0, the commands will be run 6 times. In this case as well there is a delay of 1 second after each run.
Breaking a loop?
To break an eternal (or limited) loop, you need a combination of two conditions with the AND operator. The second condition will probably refer to a variable. I used a Boolean variable as you’ll see below. That variable needs to be changed by another action. Example:
IF 1 is equal to 1 AND
v_stop is equal to 0
As long as the variable v_stop remains equal to 0, the loop will continue. But if the variable is toggled to 1, the loop will be broken.
No system variables were used. You need to create 3 user variables:
- v_time will store the number of elapsed seconds. It starts with a value of 0, also after Reset.
- v_stop (Boolean) has a default value of 0 and is used in the combined condition of the While loop. It will be toggled to 1 when the Pause button is pressed.
- v_start: stores the frame number of the first frame of the slide. It is necessary for the Reset functionality, where I used the workflow described in this recent post ‘Replay or Reset‘.
Events and Actions
EnterAct triggered by the On Enter event
Both v_time and v_stop are reset to their default values (0). The frame number in v_start is needed for the Reset action.
Start actions triggered by the Success event of the Start buttons
These actions are slightly different for both stopwatches. This is the digital version:
The button used in this version has a custom state to replace the Normal state when the Pause button is used. It has the text Restart. The variable v_stop needs to be reset to 0, because it could have been toggled to 1 when the Pause button was used.
The combined condition for the While loop has been explained before.
The version for the Analog stopwatch:
Needle is a shape with a transparent part to be able to apply a rotation effect. The technique has been described in this blog ‘Reference point in Captivate‘.
The rotation effect was defined as a custom effect. If you want more information, have a look at:
ResetAct, triggered by the Reset buttons
As explained in the Replay/Reset blog, this action needs only one command:
In this use case the Continue command is superfluous, since Captivate is waiting from the first frame for a click on a button.