In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Touching Color ()? (block)

Touching Color ()?
2.0 Touching Color ().png
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 in collision detecting. Platformers often use this block.

Argument

The color to be detected can be entered by either:

  1. clicking on the block's color swatch, then moving the mouse pointer to the desired colour, and clicking. As the mouse is moved, the color swatch continuously updates to reflect the color under the mouse pointer. When the desired color is located, a click will set the color swatch. (Note that the swatch will cease to react if the pointer strays beyond the edges of the Scratch project.) For example, if the mouse pointer is over the color blue when the mouse is clicked, the color swatch will turn blue.
  2. placing Operators blocks into the color swatch, in the following format:
<touching color (((r) * (65536)) + (((g) * (256)) + (b))) ?>
where "r", "g", and "b" are the standard values for Red, Green, and Blue, respectively.

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 performs slower than the Touching ()? (block)[2]. Its performance improves if placed in a () Custom block set to run without screen refresh, but in the same Custom Block, a Touching ()? (block) will out-perform it by an even greater margin.

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
  • Preventing a sprite from passing through walls.
forever
if <touching color [#ffff7f]?> then
set [x velocity v] to (0)
  • 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]

See Also

References

  1. https://scratch.mit.edu/projects/10941245/
  2. https://scratch.mit.edu/projects/139152265/
  • This page was last modified on 9 January 2017, at 16:51.