From Test-Scratch-Wiki

Translate: - English - Türkçe - 中文 

Custom blocks allow for one to make his or her own programming 积木. Using custom blocks, large, similar scripts can be combined into one reusable block. In some other 编程语言, custom blocks are called "procedures," "functions," or "methods." This block is useful for reusing the same sequence of blocks in different parts of code without repeating it.

Recursive factorial procedure — Scratch 2.0 alpha.

Custom Blocks in Scratch

Custom blocks are found in the "更多积木" palette.[1] This is an example of a "jump" block:

定義 jump (how far)
y 改變 (how far)
等待 (.5) 秒
y 改變 ((0) - (how far))

當 @greenflag 被點擊
jump (10) :: custom
移動 (20) 點
jump (20) :: custom

The palette starts off empty and just shows a "Make a Block" button (similar to the "Make a 变量/清单" buttons in the 数据类积木 palette). For each Define block (whether from the make a block button, or by dragging in from the 背包 or another sprite), a 函式积木 is shown in the palette. The custom block is only available to the sprite that has the Define block.[2]

Custom blocks can only be 堆叠式积木, not 填报式积木 or 布林值积木 (though they may be added in the future).[3] They support 递归, so blocks can call themselves (This does not stop the current block executing, unlike 广播). This allows for things like 创建分形 and 寻找一个数的分形.

Creating a Custom Block

To create a custom block, open the "更多积木" 积木类别 and press the "Make a Block" button. A new block menu will open, where you can name your custom block. By pressing "Options", you can add 字串, number, and 布林值 输入. You can also add more label text to the block, or make it run without screen refresh. After naming and adding arguments, a 定义 block is created. A custom block's settings can later be changed by right-clicking on its Define block or the custom block itself and selecting "edit". You can define what your custom block does by snapping a script to its Define block, as shown below.

spin right (360) degrees :: custom

定義 spin right (amount) degrees
重複 (amount) 次 
  右轉 ? (1) 度
end

Scratch 1.x 应用

當 @greenflag 被點擊
變數 [how far v] 設為 [50]
廣播訊息 [jump v] 並等待

當收到訊息 [jump v]
y 改變 (how far)
等待 (.5) 秒
y 改變 ((0) - (how far))

Procedures in BYOB/Snap!

Snap!, previously called BYOB (Build Your Own Blocks), is a Scratch 修改版 that allows procedures. Snap!'s procedures allow all 3 types of blocks, with arguments, and also recursion.

参见

参考

  1. http://blog.scratch.mit.edu/2012/02/scratch-20-project-page-and-project.html
  2. http://scratchforums.blob8108.net/forums/viewtopic.php?id=59483&p=4#p719181
  3. http://scratched.media.mit.edu/discussions/scratch-20/custom-block-types