(Redirected from Repeat for a Set Amount of Time)

There are several methods to make a script perform an action for a set amount of time. This tutorial will cover the simplest of them. Technically, this may not work if you have wait blocks or other blocks that take up time. "Repeat until" loops only check if their condition is true between blocks, therefore adding some imperfection to this method. For example, the glide () secs to x: () y: () block will continue to run even if the condition is met true because the loop only checks between individual blocks.

Some Scratchers want a block that repeats for a set amount of time:[1]

repeat for (1) secs {
} :: control

However, this suggestion has not yet been accepted.

Methods

This script uses the timer and the Repeat Until block. It will repeat the action until the timer is greater than the set limit.

These timer-based repeater scripts can be used in many different ways. The one pictured is for a sprite that will continuously move to the right for the set amount of time.

when green flag clicked
go to x: (x location) y: (y location)
reset timer
repeat until <(timer) > (limit)>
  change x by (1)
end
  • x location::variables reporter is the x of where the sprite goes to before it starts.
  • y location::variables reporter is the y of where the sprite goes to before it starts.
  • limit::variables reporter is how long the action should be repeated for (e.g. if the limit were 10, the sprite would drift to the right for 10 seconds).

If the timer is already being used and cannot be reset without ruining the project, a variable can be used instead of the timer:

when gf clicked
set [seconds v] to [0] //enter amount of time to repeat for
repeat until <(seconds) = [0]>
 wait (1) secs
 change [seconds v] by (-1)
end

when gf clicked
repeat until <(seconds) = [0]>
 . . .
end

This method is less accurate than the method below, as all computers take time to process each block.

when gf clicked
set [old timer v] to (timer)
repeat until <(timer) > ((old timer) +  (limit))>
  . . .
end

This method is also imprecise, because the Days Since 2000 block only changes in value every 3.5 seconds. However, unlike the variable, it will run at the correct speed.

set [time v] to ((days since 2000)*(86400))
repeat until <not<(((days since 2000)*(86400))-(time))<(. . .::grey)>>
. . .
end

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.