Discuss Scratch

CodeCat90
Scratcher
68 posts

How do you make something repeat for a certain amount of time?

In a game I am making, I need a script that repeats for a amount of time. Like this:

repeat ((for 1 second))
Do something
end
Thank you!
awesome-llama
Scratcher
1000+ posts

How do you make something repeat for a certain amount of time?

You could naïvely assume that the loop will run at 30 times per second since Scratch runs at approximately that framerate, which means you could just repeat 30 times.

repeat (30)
...
end

This is of course not going to be accurate since the speed the loop runs can vary quite a bit due to many factors so the proper way to do this is to make use of Scratch's various time-keeping blocks:

(timer)
reset timer

There's also the days since 2000 block which is very accurate even though it doesn't seem like it. If you need to keep multiple times, this is the block to use (there's only one timer so it really only can be used for one purpose at a time)
(days since 2000)

With the timer, you can do this:

reset timer
repeat until <(timer) > [1]>
...
end

Here's a solution using the days since 2000 block, too:
set [start time v] to ((days since 2000) / (86400)) // there are 86,400 seconds in a day
repeat until <((days since 2000) / (86400)) > ((start time) + (1))> // the +1 is to make it wait 1 second
...
end
bSpyder
Scratcher
100+ posts

How do you make something repeat for a certain amount of time?

This could help.
set [last v] to ((timer) + (1)) //1 second
repeat until <(last) < (timer)>
do something
end
CodeCat90
Scratcher
68 posts

How do you make something repeat for a certain amount of time?

Thanks guys!

Powered by DjangoBB