📋
"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-Twistblock.
3. Use the Block
Go back to the My Blocks palette. You will see your new
Jump-Twistblock 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?
When making the block, click "Add an input (number or text)."
Label it something like
height.Inside your Define script, drag the little circular
heightvariable into yourchange y byblock.Now, when you use your
Jumpblock 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 件のコメント:
コメントを投稿