How do you make an event last for 5 seconds?

I'm trying to create a Pacman app and I'm adding my own twist. I am adding "powerups" to help Pacman. Two of my powerups I would like to only have for a certain duration. (5 seconds) How do I make the event last for only five seconds and then returning back to normal. Ex: I am making a "Freeze" powerup which is supposed to make the ghosts move slower I want it to last 5 seconds, how do I do that?

Welcome Liz.

Probably by using a Clock ([Guide] How to use the CLOCK and If..then control statements. Programming Your App to Make Decisions

.

Here is a screenshot of the code, when the button is pressed the ghosts move slower, but they stay slower. They don't return to normal. That is the issue I'm having.

It's possible to have multiple single purpose Clocks, if you rename them as to their purpose, like FreezeClock. In that case, you start the thing that should last for 5 seconds, and enable the otherwise disabled FreezeClock. When the FreezeClock Timer fires, it disables itself and undoes whatever thing started it.

If you find yourself with too many single purpose Clocks, you could switch to a collection og global deadline variables, that you would have to check each regular Clock tick, to see if they were set or not and if so, has their deadline passed yet.

In Clock.Timer event block disable the Clock and set the blocks relative to the normal speed. When you enable the timer, once the interval elapses, this block is triggered.

Herewith the basic logic required:

image

  • In this example the button colour starts as green.

  • When the button is clicked, the background colour is changed to red and the clock is started, with an interval of 5000ms.

  • After 5000ms the clock timer fires, the clock is stopped (from firing again) and the button background colour is changed back to green.