|Color () is Touching ()?|
This block is widely used for collision detection.
The color to be detected can be entered by either:
- clicking on one of the block's color swatches, then moving the mouse pointer to the desired color, 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.
- placing Operators blocks into the color swatches, in the following format:
<color (((r) * (65536)) + (((g) * (256)) + (b))) is touching (((r) * (65536)) + (((g) * (256)) + (b))) ?>
- where "r", "g", and "b" are the standard values for Red, Green, and Blue, respectively.
In versions of Scratch before 1.2, this block was called Color () is Over ()?.
The Color () is Touching ()? block can produce unexpected/unwanted results.
Significantly, the Color () is Touching ()? block does not always return "false" if its sprite color is not touching the other specified color. This is because color sensing in Scratch has a built-in limitation, which reduces processing time. Whilst the stage can display more than 16 million colors, Scratch only properly deals with a much smaller number of colors. 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.
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.
Scratchers considering using the Color () is Touching ()? block should be aware that it performs slower than the Touching ()? (block). 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.
- 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 to costume [drinking water v] :: looks wait (1) secs switch to costume [not drinking water v] :: looks
- 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