Make "My Blocks" - Part 1
-
Prerequisite
Introduction
When you write a Scratch program, you can choose from many blocks provided by the playground. However, you can also make your own blocks. To do that, you need to take 3 steps:
- Define the new block: you need to tell the system what this new block looks like;
- Implement this new block: you need to tell the system what this block does;
- Use this new block: you will be able to drag this new block from the flyout into your program.
A Complete Example
Let’s go through these 3 steps in a complete but simple example.
Define a New Block
To define a new block, you can go to the “My Block” category, and click “Make a Block”. You can type any name for your block, such as “run”, then click “OK”. You will see a new “run” block in the flyout, and also a “define run” block in the coding area.
Implement the New Block
Now we need to add other blocks into the “define run” stack, just like how you would build any stack of code. For example, suppose we want to make the sprite move right 5 steps whenever we use the “run” block, then it will look like this:
Use the New Block
Now our block is defined and also implemented, we can start using it in other parts of the program. For example, whenever the sprite is clicked, we make the sprite run:
Whenever the system sees the “run” block, it will jump to the “define run” stack, and go through all the blocks in that stack.
That’s an example of making and using a very simple block. Below let’s look at some different ways to define and use new blocks.
Define a Block with Input Parameters
Many blocks provided in the playground can take input parameters. For example, the “say” block takes one input, so you can use it to say different things. You can make your own block take input parameters as well.
When you define the block, you can add 3 types of inputs:
- Number or Text: This input will be oval-shaped, and it accepts a number or a text.
- Boolean: This input will have triangle tips on the 2 sides, and it accepts any boolean block.
- Color: This input will be a color value.
Now let’s change the “run” block to take 3 inputs named “times”, “pen down” and “color”:
As shown, after changing the definition, the “define run” block now carries 3 input blocks for you to use inside this stack. Note that you can only use them inside this stack.
For example, we can use the “times” input to control how many steps we move, use the “pen down” input to control whether to put down the pen or not, and use the “color” input to control the color of the pen:
Use a Block with Input Parameters
You can use the new “run” block just like any other block. You just need to set the value of the 3 input parameters. For example, if you set the first input to 4, then the sprite will move 4 steps.
This is what you get when clicking on the sprite:
How to Delete My Blocks
To delete my block, you have to find where you are using this new block, and delete all of them, then you can delete the “define” stack. If the new block is still in use somewhere, then you can’t delete the “define” stack.
Change the Color of My Blocks
To change the color of my block, click on the “Set my block’s color” button in the “My Blocks” category.
A pop-up window will allow you to select the color of your block. Click “change” to set it.
Here is a demo:
See Also