Touching Color ()?
<touching color [#d15fee]?>
Category Sensing
Type Boolean
Introduced in 22Dec04 (0.x)

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.[1]

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. 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 Touching Color ()? 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 Touching Color ()? block should be aware that it is slower than the Touching ()? block.[4]

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) seconds
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 < not<touching color [#007f00]?>> then
        move (2) steps
    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) seconds
        broadcast (game over v)
    end
end

See Also

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.