define custom block
define custom block
My Blocks (known as More Blocks in Scratch 2.0) is one of the ten categories of Scratch blocks. It holds procedures for the selected sprite. They are color-coded pink. Before any blocks are created, it is empty, except for a "Make a Block" button.
Clicking Make a Block brings up a dialogue allowing the user to make a procedure. Once OK is pressed, the new block appears in the palette and an empty definition appears in the code area. When the procedure runs, Scratch will run the blocks below the corresponding Define block.
Uses of My Blocks
Apart from variables, procedures are the main kind of abstraction used in procedural programming; they let Scratchers write scripts without knowing or thinking about the details of what each script does. This is also true of Scratch's built-in blocks, like
move () steps. For example, suppose one wants a sprite to vanish and reappear. To vanish, the sprite should play a sound and repeatedly increase the ghost effect; to reappear, it should repeatedly decrease the ghost effect. This should happen at several points in the project. Without procedures, the Scratcher would have to duplicate the same sequence of blocks at each point, but by making "vanish" and "appear" blocks, they can write scripts in a way which matches how they think of them:
. . . think [Hmm...] for (2) secs vanish . . . appear play sound (Horse v) until done . . . define vanish start sound (Ripples v) repeat (10) change [ghost v] effect by (10) end define appear repeat (10) change [ghost v] effect by (-10) end
Now, if the user changes their mind about how the sprite should vanish or appear, they need only change the block definitions.
Custom blocks also help by reducing project file size and allowing the user to disable screen refresh. Number, String, and Boolean inputs can be added to custom blocks. For example, a Scratcher could make a
jump () :: custom block with a "height" input, and
(height :: custom-arg) will appear in the Define block. These blocks can be dragged out and used in the definition just like variables.
If a user has a long string of code they know they will be using a lot, it makes sense to use a custom block. That way, the user doesn't have to repeat making that code multiple times, and this reduces lag as there are now less blocks.
Run without screen refresh
Using custom blocks is the only way to run scripts without screen refresh. Screen refresh is the very minuscule wait which occurs at the end of each iteration of a loop block or when a block which waits is used. The Make a Block dialog offers to disable screen refresh (by default it is enabled.) This can make it less obvious what is happening but is useful if one wants to make changes without the user seeing the intermediate steps or waiting a long time. For example, if one uses this script:
set [i v] to  pen down repeat (200) move (i) steps turn cw (92) degrees change [i v] by (1) end pen up
the user may watch the sprite drawing the picture. But if one places it in a custom block with screen refresh disabled, the user will only see the complete picture, and it will finish much sooner. This feature is commonly used in platformers and other games requiring faster computer processing. It is also used to fix timing issues that are caused by built in pauses in the script, which intends to make on screen motion more visible, but it can sometimes get in the way.
If a custom block with a label saying either %b or %n is placed, then set to a boolean or number/text, it displaces the block and changes it to boolean for %b or number/text for %n.
If a custom block has a label of "%", you will not be able to confirm and press Okay (left). The only options are Cancel and exiting. If the block palette is scrolled up after that, the blocks will be bunched up together (right).
- Main article: List of Block Workarounds
In most simple cases, custom blocks can be worked around by using broadcasts and variables. However, this does not allow recursion, disabling screen refresh, or for multiple scripts to use the same procedure at the same time.
when gf clicked broadcast (jump v) and wait when I receive [jump v] repeat (5) change y by (6) end repeat (5) change y by (-6) end
Procedures in Snap!
Snap!, previously called BYOB, is a graphical programming language based on Scratch. Among other things (the most important of which are first-class procedures and first-class lists) Snap! allows making reporters and predicates as well as stack blocks.
|Please expand this article or section. You can help by talk page. (April 2019)if you are an editor. More information might be found in a section of the|
Many Scratchers would like the ability to make custom reporters and boolean blocks. Additionally, dropdown inputs as arguments have also been requested, to the extent that some people used JSON hacking to insert them into custom blocks (though this does not work in Scratch 3.0.)
- AonymousGuy. (10/11/2013). "Better Custom Blocks " topic:20661