From Test-Scratch-Wiki
重复 () 次 | |
重複 () 次 | |
类别 | 控制类积木 |
形狀 | 半框形积木 |
The Repeat () 积木 is a 控制类积木 and a 半框形积木. Blocks held inside this block will loop a given amount of times, before allowing the 程式 to continue.
If a decimal is put in, the number is 四舍五入.
This block has a slight delay, so for optimum speed of execution, single frame block stacks should be used.
用法示例
As this block loops pieces of code, it is widely used for saving space and time. Some common uses are:
- Repeating code
— using the Repeat () block (and the 重复直到 () block) can save a lot of scripting.
重複 (10) 次 變數 [variable v] 改變 (1) 造型換成下一個 如果 <碰到 [Wall v] ?> 那麼 筆跡清除 廣播訊息 [Reload v] 蓋章 end end
- Animation
— rather than coding each 造型 change and delay individually, the Repeat () block (with the 造型换成下一个 block) can be used. This saves a lot of scripting.
重複 (8) 次 等待 (0.05) 秒 造型換成下一個 end
- Continuous checks
— a 角色 can continuously move and sense, for example.
重複 (20) 次 y 改變 (3) 如果 <碰到 [邊緣 v] ?> 那麼 效果 [顏色 v] 改變 (75) end end
- Putting one repeat () block inside another is called "nesting". The total repetitions will be the two repeat () block inputs' product. In this case, the nested loops draw 10 times 10, or 100 dots.
筆跡寬度設為 (2) 定位到 x: (-100) y: (100) 重複 (10) 次 重複 (10) 次 下筆 停筆 x 改變 (20) end x 設為 (-100) y 改變 (-20) end
应用
- Main article: List of Block Workarounds
This block can be replicated with the following code:
變數 [counter v] 設為 (0) 重複直到 <(counter) = (number of times)> . . . 變數 [counter v] 改變 (1) end