Touching Color ()?
<touching color [#d15fee] ?>
Category Sensing
Type Boolean

The Touching Color ()? block is a Sensing block and a Boolean block. The block checks whether its sprite is touching a specified color. If it is, the block returns true.

This block is widely used for collision detecting. Platformers often use this block.

Caveats

The Touching Color ()? block can produce unexpected/unwanted results.

Built-in Limitation

Significantly, the Touching Color ()? block does not always return false if its sprite is not touching the specified 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.[1] This means that the Touching Color ()? 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 Touching Color ()? block should be aware that it is slower than the Touching ()? block.[2]

Example Uses

  • Moving a sprite until it touches a color.
repeat until <touching color [#000000]?>
    move (10) steps
end
  • Making a sprite do something if it touches a color, e.g., if the sprite touches blue (water), it reacts in a specific way.
if <touching color [#0000ff]?> then
    say [I found water!] for (2) secs
end
  • Stopping bullets if they hit walls of a specific color.
repeat until <touching color [#cf4141] ?>
    move (10) steps
end
delete this clone
  • Sensing whether a sprite has hit a dead-end in a maze.
forever
    if <touching color [#007f00]?> then
        say [Dead end!] for (1) secs
    end
end
  • Preventing a sprite from passing through walls.
forever
    if <touching color [#ffff7f]?> then
        set [x velocity v] to (0)
    end
end
  • Making a game end when a color is touched.
forever
    if <touching color [#01efff]?> then
        say [Game over.] for (1) secs
        broadcast [game over v]
    end
end

See Also

References

  1. https://scratch.mit.edu/projects/10941245/
  2. https://scratch.mit.edu/projects/139152265/