From Test-Scratch-Wiki

()
自订积木 :: custom
类别 更多积木
形狀 条形积木

() is a custom 积木, or procedure, all of which are 条形积木 in the 更多积木 palette. A version of it is created for every custom block, much like with the 变数积木. Custom blocks are usually used to make long or frequently repeated scripts in projects accessible in a single block.

A procedure is created by clicking "Make a block" in the 更多积木 palette. This creates a dialogue, where the block can be named, and 参数 defined. (These can be edited via the context menu.) Clicking OK will create a 定义 () block with the procedure inside. Blocks can then be stacked beneath it. When the procedure is run, it will run the blocks under the Define hat.

用法示例

  • Jumping
定義 jump
重複 (20) 次 
  y 改變 (1) // upward motion
end
重複 (20) 次 
  y 改變 (-1) // downward motion
end
當 @greenflag 被點擊
下筆
重複 (40) 次 
  draw :: custom
end

定義 draw
重複 (4) 次 
  變數 [i v] 改變 (0.2)
  移動 ((i) * (5)) 點
  右轉 @turnright ((120) + ((i) / (20))) 度
  筆跡顏色設為 (i)
  筆跡亮度設為 (i)
end
  • Making a sprite visually pointing in direction "A" but moving in direction "B"
define point in direction (A) move direction (B)
point in direction (B) //since the procedure is atomic, this will go unseen
move (3) steps //motion is in the correct direction
point in direction (A)

An example use of booleans in custom blocks.

  • When Boolean in true, script activates if the block is put in a forever loop
定義 say (string) when <boolean>
如果 <boolean> 那麼 
  說出 (string)
end

应用

Main article: List of Block Workarounds

Prior to Scratch 2.0, people simulated procedures with 广播, using the following script:

廣播訊息 [procedure v] 並等待

當收到訊息 [procedure v]
移動 (10) 點

This can still be done in Scratch 2.0. 背景 events can also be used.

This is not a perfect workaround, as procedures are recursive, while 广播 are just tail-recursive. Furthermore, broadcast scripts cannot run without screen refresh, which is one benefit of using custom blocks. Inputs, while possible, require separate 变量s.

Bugs

It is possible to insert inputs other than numbers, strings, and booleans in custom blocks.

Example of the "insertable input" bug with custom blocks

Blocks with boolean inputs will not respond to changes in the boolean's state.[1]

Using the "Run without screen refresh" option may cause audible errors in sounds.

参见

参考

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