|Touching Color ()?|
This block is widely used for collision detecting. Platformers often use this block.
The Touching Color ()? block can produce unexpected/unwanted results.
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. 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 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.
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 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