(Redirected from Stack Blocks)
A stack block is a rectangular block that is shaped to fit above and below other blocks. Stack blocks make up the majority of the blocks available in Scratch, being available in every non-extension category except operators.
Execution Process
When 2 stack blocks are connected to form a script their commands will execute in the order from top to bottom. The entire collective stack executes in a single frame. For example, take the following script:
reset timer move (5) steps move (4) steps move (3) steps move (2) steps move (1) steps set [elapsed time v] to (timer)
The entire execution process for this stack of stack blocks will take approximately 0 seconds. Opposed to visualizing the sprite move 5 steps, then 4, etc. the entire motion will be seen in one unified step; the end user will witness what appears to be the sprite move 15 steps, even though it has 5 separate blocks making up the motion. The elapsed time variable will be 0 when the script finishes executing.
| Extremely long and intensive scripts could take a long time to process and may actually have a notable enough elapsed time. Do not rely on this, as it depends on the computer's speed. |
Take the following code, now. While it appears to be the same, just using a loop, it actually is not all executed in a single frame this time.
reset timer set [step move v] to (5) repeat (5) move (step move) steps change [step move v] by (-1) end set [elapsed time v] to (timer)
This scenario should have 5 separate motions and should take up 5 frames of the 30-frame-per-second Scratch project. It should take approximately 0.166 seconds to execute. Initially, the sprite moves 5 steps. Then there is a pause because of how the repeat loop works. Then, the sprite moves 4 steps, and so forth. The only method of using a repeat loop without the delay between each cycle is to place the repeat loop inside a custom block that has the "run without screen refresh" option enabled.
Stack Blocks With Delay
Some stack blocks do execute with a delay, meaning there may be a pause between it and the next block executing. The following blocks have this behavior:
switch backdrop to ( v) and waitplay sound ( v) until donewait () secondswait until <>ask () and wait
Blocks
| Click on a block for more information. |
Motion
There are 15 Motion stack blocks.
move () stepsturn cw () degreesturn ccw () degreesgo to ( v)go to x: () y: ()glide () secs to ( v)glide () secs to x: () y: ()point in direction ()point towards ( v)change x by ()set x to ()change y by ()set y to ()if on edge, bounceset rotation style [ v]
Looks
There are 17 Looks stack blocks.
say [] for () secondssay []think [] for () secondsthink []switch costume to ( v)switch backdrop to ( v)switch backdrop to ( v) and waitnext costumenext backdropchange size by ()set size to ()%change [ v] effect by ()set [ v] effect to ()clear graphic effectsshowhidego to [ v] layergo [ v] () layers
Sound
There are eight Sound stack blocks.
play sound ( v) until donestart sound ( v)stop all soundschange [ v] effect by ()::soundset [ v] effect to ()::soundclear sound effectschange volume by ()set volume to () %
Events
There are two Events stack blocks.
Control
There are four Control stack blocks.
wait () secondswait until <>stop [other scripts in sprite v] // If "all" or "this script" is selected in the drop-down menu, this block becomes a Cap block.create clone of [ v]
Sensing
There are three Sensing stack blocks.
Variables
There are four Variables stack blocks.
List
There are seven List stack blocks.
add [] to [ v]delete () of [ v]delete all of [ v]insert [] at () of [ v]replace item () of [ v] with ()show list [ v]hide list [ v]
My Blocks
The My Blocks area allows the user to make their own stack blocks.
custom block:: custom
Music Extension
There are six stack blocks in the Music Extension.
play drum ( v) for () beatsrest for () beatsplay note () for () beatsset instrument to ( v)set tempo to ()change tempo by ()
Pen Extension
There are nine blocks in the Pen Extension, and all of them are stack blocks. This is the most stack blocks for any extension.
erase allstamppen uppen downset pen color to ()change pen ( v) by ()set pen ( v) to ()change pen size by ()set pen size to ()
Video Sensing Extension
There are two stack blocks in the Video Sensing Extension.
Face Sensing Extension
There are three stack blocks in the Face Sensing Extension.
go to [nose v]:: extensionpoint in direction of face tilt:: extensionset size to face size:: extension
Text to Speech Extension
There are three stack blocks in the Text to Speech Extension.
Micro:bit Extension
There are three stack blocks in the micro:bit Extension.
LEGO MINDSTORMS EV3 Extension
There are four stack blocks in the LEGO MINDSTORMS EV3 Extension.
motor ( v) turn this way for () seconds:: ev3motor ( v) turn that way for () seconds:: ev3motor ( v) set power ()%beep note () for () secs
LEGO BOOST Extension
There are seven stack blocks in the LEGO BOOST Extension.
turn motor ( v) for () secondsturn motor ( v) for () rotationsturn motor ( v) onturn motor ( v) offset motor ( v) speed to ()%set motor ( v) direction ( v)set light color to ()::boost
LEGO Education WeDo 2.0
There are six stack blocks in the LEGO Education WeDo 2.0 Extension.
turn ( v) on for () secondsturn ( v) onturn ( v) offset ( v) power to ()set ( v) direction to ( v)set light color to ():: wedo
Shape
Stack blocks are fitted with a puzzle-piece like shape; the top has a notch and the bottom has a bump. Because of this shape, scripts can stretch on and on — the block tessellates.
Their shape allows them to be placed in the following areas:
- After Hat blocks
when gf clicked say [Hi.] for (2) seconds
- Before/after other Stack blocks
go to (Sprite2 v) play sound (meow v) until done point towards (Sprite3 v)
- Before Cap blocks
say [Bye.] for (2) seconds stop [this script v]
- Inside C blocks
forever if <(loudness) > (30)> then say [No noises.] for (2) seconds
Use
As Stack blocks are shaped to allow blocks above and below them, they are used almost everywhere in a script; most scripts have a Stack block in them. An example script:
when flag clicked
repeat until <(do_Stop) = [1]>
move (10) steps
change [color v] effect by (25)
play sound (meow v) until done
if <touching (edge v)?> then
say [Done!] for (2) seconds
stop [this script v]
end
end
Note how the Stack blocks are used in the script — they make up all the commands.