(Redirected from Boolean)
There are 13 Boolean blocks in Scratch 3.0. Click on a block for more information.
<touching ( v)?>
<touching color (#ff0000)?>
<color (#00ff00) is touching (#0000ff)?>
<key ( v) pressed?>
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 and number inputs:
when gf clicked forever say <touching (Sprite1 v)?>
In general, Booleans return string values of "true" or "false", but when inserted into a reporter input, the Boolean instead will return "1" or "0", allowing mathematical operations to be performed. For example, consider the script below:
when gf clicked forever say ((3) + <touching (Sprite1 v)?>) end
If the Boolean condition is true, the sprite will say the number "4". A true condition is an equivalent to "1". If the Boolean condition is false, the sprite will say the number "3". A false condition is equivalent to "0". Whether the numbers "1" and "0" are strings or numbers is not important in Scratch since the specification of variables types is unnecessary, unlike some other programming languages.
If the () + () block is ommitted entirely, the Boolean will then return "true" or "false" since the Boolean block would lie in a string input instead of a reporter input.
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 wait until <touching (edge v)?> say [Done!] for (2) secs stop [this script v]
The Wait Until () block pauses the script until the Boolean value, here
<touching (edge v)?>, is true. While the sprite is not touching the edge,
<touching (edge v)?> is false, and the block waits for it to become true. When the sprite touches the edge,
<touching (edge v)?> becomes true, and the script below it is run.
A Boolean block can be used in a string input. If a Boolean in a String input is true, it reports "true". If the Boolean is false, it returns "false". For example,
say <touching (mouse-pointer v)?> makes the sprite say "true" if the sprite is touching the mouse pointer, and "false" otherwise.
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)>> then ... end
when gf clicked forever say <touching (mouse-pointer v)?>
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)> then ... end
In this case, the script will check whether the mouse currently has the same state as it did when the variable was stored.