(Redirected from Stack)
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 category except Operators and PicoBoard Blocks.
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.
|Note:||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:
Click on a block to find out more!
There are 15 motion stack blocks.
There are 17 looks stack blocks.
switch costume to [ v]/
switch backdrop to [ v]
switch backdrop to [ v] and wait
There are eight sound stack blocks.
Events has only two stack blocks:
There are four control stack blocks.
wait () seconds
wait until <>
create clone of [ v]
stop [other scripts in sprite v] // If "all" or "this script" is selected in the drop-down menu, this block becomes a Cap block.
There are three sensing stack blocks.
There are 11 variable and list blocks.
The My Blocks area allows the user to make their own stack blocks.
define custom block
custom block :: custom
There are six stack blocks in the Music Extension.
play drum [ v] for () beats
rest for () beats
play note () for () beats
set instrument to [ v]
set tempo to ()
change tempo by ()
There are nine blocks in the Pen Extension, and all of them are stack blocks. This is the most stack blocks for any extension.
set 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.
There are no stack blocks in the Translate 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 :: ev3
motor [ v] turn that way for () seconds :: ev3
motor [ v] set power ()%::ev3
beep note () for () secs::ev3
LEGO Education WeDo 2.0
There are six stack blocks in the LEGO Education WeDo 2.0 Extension.
turn [ v] on for () seconds :: wedo
turn [ v] on :: wedo/
turn [ v] off :: wedo
set [ v] power to () :: wedo
set [ v] direction to [ v] :: wedo
set light color to () :: wedo
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
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) = > 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.