|Color () is Touching ()?|
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.
|This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.|
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 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. 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.
- 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