There are 13 Boolean blocks in Scratch 2.0, as follows:
- Touching ()?
- Touching Color ()?
- Color () is Touching ()?
- Mouse Down?
- Key () Pressed?
- Sensor ()?
- () < ()
- () = ()
- () > ()
- <> and <>
- <> or <>
- Not <>
- () Contains ()
Boolean blocks are conditions that can either be true or false. They have a hexagonal shape and fit in the corresponding hexagonal slot on other blocks.
The condition gap can be filled with any Boolean block:
if <key [space v] pressed?> then broadcast [Jump! v] end
Despite their shapes, Boolean blocks also fit in string inputs:
when gf clicked forever say <touching [Sprite1 v]?>
and also number ones!
when gf clicked forever say ((0) + <touching [Sprite1 v]?>) end
They still report true or false, but in the example, the sprite will interpret this as a string and just say the word.
Scratch has two types of Booleans: Booleans that check a specific condition, and comparative Booleans.
As Boolean blocks are conditions (and report if they are true or false), they are used whenever a condition is needed. Conditions are used with some C blocks and some Control Stack blocks. A common use for conditions is the If () Then block — if the condition is true, the blocks held inside the C block will activate.
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) secs stop [this script v] end end
Boolean variables can be compared to non-boolean variables. For example, the following script, two booleans are compared to each other directly, and the if statement will execute the code inside if both have the same value (i.e. both true or both false).
if <<mouse down> = <touching color [#00A]>> ... end
Storage in Variables
Booleans can be stored in variables as well. The following script stores the current mouse state in the variable "bool".
set [bool v] to <mouse down?>
Later, the variable can be compared to another boolean with the stored variable:
if (<mouse down?> = (bool)) ... end
In this case, the script will check whether the mouse currently has the same state as it did when the variable was stored.