2026年1月25日日曜日

What is My Block and how to create your own "My Blocks" (Functions) ?

 📋

"My Blocks" is Scratch’s version of what professional programmers call Functions or Procedures.

Imagine you have a specific dance move for your sprite that requires 10 different blocks. If you want the sprite to do that dance five times during a game, your script area will become a cluttered mess. Instead, you can "wrap" those 10 blocks into one single custom block named "Dance."


Why use "My Blocks"?

  • Organization: It keeps your workspace clean.

  • Reusability: You write the code once and use it as many times as you want.

  • Readability: It makes it easier for others (and you!) to understand what the code is doing. Instead of seeing 20 motion blocks, they just see a block labeled Jump.


How to Create a "My Block"

Follow these steps to build your first custom function:

1. The Setup

  • Click on the pink My Blocks category at the bottom of the list.

  • Click the "Make a Block" button.

2. Name and Define

  • Give it a name: Type something descriptive (e.g., Jump-Twist).

  • The "Define" Hat: A new pink "Define" block will appear in your workspace. This is where you tell Scratch what the block actually does.

  • Snap blocks underneath: Drag your regular motion or look blocks and snap them under the Define Jump-Twist block.

3. Use the Block

  • Go back to the My Blocks palette. You will see your new Jump-Twist block sitting there.

  • Drag that single pink block into your main script (like under a When Green Flag Clicked).


Advanced: Adding "Inputs" (Parameters)

Sometimes you want a custom block to be flexible. For example, what if you want a Jump block where you can decide how high the sprite jumps each time?

  1. When making the block, click "Add an input (number or text)."

  2. Label it something like height.

  3. Inside your Define script, drag the little circular height variable into your change y by block.

  4. Now, when you use your Jump block in your main script, you can type different numbers ($10, 50, 100$) into the hole!


When should you use them?

The "Rule of Three": If you find yourself copying and pasting the same group of blocks three or more times, it’s time to make a My Block.

Would you like to try building a "Draw Square" custom block using the Pen extension?

📋

0 件のコメント:

コメントを投稿