Touching Color ()? (block)
|Touching Color ()?|
This block is widely used in collision detecting. Platformers often use this block.
The color to be detected can be entered by either:
- 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.
- 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.
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.
Scratchers considering using the Touching Color ()? 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 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]
Touching ()? • Touching Color ()? • Color () is Touching ()? • Distance to () • Ask () and Wait • Answer • Key () Pressed? • Mouse Down? • Mouse X • Mouse Y • Loudness • Video () on () • Turn Video () • Set Video Transparency to ()% • Timer • Reset Timer • () of () • Current () • Days Since 2000 • UsernameMore blocks...