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.

This block is widely used for collision detection.

History

Archive.png This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.
The block as seen in Scratch 1.1

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, which reduces processing time.[citation needed] Whilst the stage can display more than 16 million colors, Scratch only properly deals with 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. 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.[3]

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) secs
    end
end

See Also

References

  1. https://web.archive.org/web/20071125083132/http://scratch.mit.edu/pages/beta-release-notes
  2. https://scratch.mit.edu/projects/10941245/
  3. https://scratch.mit.edu/projects/139167694/