Color () is Touching ()? | |
<color (#7b68ee) is touching (#b8860b)?> | |
Category | Sensing |
Type | Boolean |
The Color () is Touching ()? block is a Sensing block and a Boolean block. The block checks whether the first input, a color on its sprite, is touching another color. If it is, the block reports true, and if does not it reports false.
This block is widely used for collision detection.
In versions of Scratch before 1.2, this block was called Color () is Over ()?.[1]
Caveats
The Color () is Touching ()? block can produce unexpected/unwanted results.
Built-in Limitation
Significantly, the Color () is Touching ()? block does not always return false if the first color is not touching the other color. This is because color sensing in Scratch has a built-in limitation. Whilst the stage can display more than 16 million colors, Scratch's sensing blocks only differentiate a much smaller number of colors.[2] This means that the Color () is Touching ()? block will often return a false positive when sensing a color which is different from (but similar to) the specified one.[3] Many Scratchers may never notice this phenomenon, but those wanting absolute precision in color detection should bear the Scratch limitation in mind.
Anti-Aliasing
Some graphics, even if they appear to have crisp edges in the Scratch Paint Editor, are subject to anti-aliasing when on the stage. This means that, when selecting color by clicking, care must be taken not to select a translucent edge pixel by mistake.
Performance
Scratchers considering using the Color () is Touching ()? block should be aware that it performs slower than the Touching ()? block.[4]
Example Uses
- Moving a sprite until a color on its sprite touches a color.
repeat until <color [#ea235c] is touching [#cba81a]?> point towards (Sprite2 v) move (10) steps end
- Making a sprite do something if a color on its sprite touches a color, e.g., if the sprite's (white) mouth touches (blue) water, it drinks some water.
forever if <color [#ffffff] is touching [#0000ff]?> then switch costume to (drinking water v) wait (1) secs switch costume to (not drinking water v) end end
- Stopping bullets if they hit walls of a specific color.
repeat until <color [#ffd700] is touching [#f321a5]?> move (10) steps end delete this clone
- Sensing whether a sprite has hit a dead-end in a maze.
forever if <color [#000000] is touching [#007f00]?> then say [Uh-oh, I hit a dead end!] for (2) seconds end end