m (Fixed link virus)
(Undo revision 231064 by TenType (talk) Nvm, I don’t think that’s correct)
 
(23 intermediate revisions by 16 users not shown)
Line 1: Line 1:
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: ()|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.
+
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: ()|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.
  
Many people want a block like [http://scratchblocks.github.io#repeat(1)secs::control%20cstart this].<ref>http://scratch.mit.edu/discuss/topic/20841</ref>
+
Many people want a block like <sb>repeat for (1) secs{} ::control </sb><ref>http://scratch.mit.edu/discuss/topic/20841</ref>
  
 
This script uses the [[timer (value)|timer]] and the [[Repeat Until () (block)|Repeat Until block]]. It will repeat the action until the timer is greater than the set limit.
 
This script uses the [[timer (value)|timer]] and the [[Repeat Until () (block)|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.
 
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.
<scratchblocks>
+
<scratchblocks>when green flag clicked
when green flag clicked
 
 
go to x: (x location) y: (y location)
 
go to x: (x location) y: (y location)
 
reset timer
 
reset timer
 
repeat until <(timer) > (limit)>
 
repeat until <(timer) > (limit)>
 
   change x by (1)
 
   change x by (1)
end
+
end</scratchblocks>
</scratchblocks>
+
* <sb>x location::variables reporter</sb> is the x of where the sprite goes to before it starts.
*''x location'' is the x of where the sprite goes to before it starts.
+
* <sb>y location::variables reporter</sb> is the y of where the sprite goes to before it starts.
*''y location'' is the y of where the sprite goes to before it starts.
+
* <sb>limit::variables reporter</sb> 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).
*''limit'' 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:
 
If the timer is already being used and cannot be reset without ruining the [[project]], a [[variable]] can be used instead of the timer:
  
<scratchblocks>
+
<scratchblocks>when gf clicked
when gf clicked
 
 
set [seconds v] to [0] //enter amount of time to repeat for
 
set [seconds v] to [0] //enter amount of time to repeat for
 
repeat until <(seconds) = [0]>
 
repeat until <(seconds) = [0]>
Line 31: Line 28:
 
repeat until <(seconds) = [0]>
 
repeat until <(seconds) = [0]>
 
  . . .
 
  . . .
end
+
end</scratchblocks>
</scratchblocks>
 
  
This method is less accurate than the method below, as [[List of misconceptions about Scratch#Workarounds|all computers take time to process each block]].
+
This method is less accurate than the method below, as [[List of Misconceptions about Scratch#Workarounds|all computers take time to process each block]].
  
 
<scratchblocks>when gf clicked
 
<scratchblocks>when gf clicked
Line 40: Line 36:
 
repeat until <(timer) > ((old timer) +  (limit))>
 
repeat until <(timer) > ((old timer) +  (limit))>
 
   . . .
 
   . . .
end
+
end</scratchblocks>
</scratchblocks>
 
 
This method is far more exact, because the timer is correct to ±3500 ms/day, whereas a variable is not always updated at the exact millisecond.
 
This method is far more exact, because the timer is correct to ±3500 ms/day, whereas a variable is not always updated at the exact millisecond.
  
Line 48: Line 43:
  
 
[[Category:Scripting Tutorials]]
 
[[Category:Scripting Tutorials]]
[[Category:How To Pages]]
+
[[ja:一定時間繰り返す]]

Latest revision as of 03:26, 2 July 2019

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.

Many people want a block like repeat for (1) secs{} ::control [1]

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 far more exact, because the timer is correct to ±3500 ms/day, whereas a variable is not always updated at the exact millisecond.

References

  1. http://scratch.mit.edu/discuss/topic/20841