From Test-Scratch-Wiki

"Touching" redirects here. For the block that senses if a sprite is touching a color, see Touching Color ()? (block).
Touching ()?
2.0 Touching ().png
Category Sensing
Type Boolean

The Touching ()? block is a Sensing block and a Boolean block. The block checks if its sprite is touching the mouse-pointer, edge, or another sprite (a Reporter block holding the sprite's name can be used). If the sprite is touching the selected object, the block returns true; if it is not, it returns false.

This block behaves differently when the sprite is hidden. Every check for collisions with other sprites return false, however it still senses the mouse and the edges.

Example Uses

As the block checks if its sprite is touching an object, it is widely used in detecting collisions.

Some common uses for the Touching ()? block:

  • Moving a sprite until it touches the edge of the screen
repeat until <touching [edge v]?>
   move (3) steps
  • Having a sprite chase another sprite until the chaser touches the runner
repeat until <touching [Sprite2 v]?>
   point towards [Sprite2 v]
   move (3) steps
say [I caught you!] for (2) secs
  • Stopping bullets when they hit walls
if <touching [wall v]?> then
  • In a maze, sensing if a sprite has hit a dead-end
if <touching [maze walls v]?> then
say [Dead end!] for (1) secs
  • Checking if a sprite is touching the mouse pointer
when flag clicked
if <touching [mouse-pointer v]?> then
switch to costume [highlighted v]
switch to costume [normal v]
  • Checking if the player has touched an enemy in a game
if <touching [Enemy v]?> then
say [Game over!]
stop [all v]

Related Suggestion

Some Scratchers want a block that detects if two different sprites are touching.[1] The advantage of such a block is that it grants a 3rd sprite the ability to sense if another two are touching. Such a block would look similar to this:

<[sprite1 v] is touching [sprite2 v]?>//category=sensing

This can be replicated with a global variable, with one of the touching sprites checking, and setting the value, like so:

// In Sprite1
when I receive [Check for Touching v]
 if <touching [Sprite2 v]?> then
  set [Sprite1 Touching Sprite2? v] to [yes]
  set [Sprite1 Touching Sprite2? v] to [no]

// Anywhere you want to check for the touching
broadcast [Check for Touching v]
if <(Sprite1 Touching Sprite2?) = [yes]> then
// put what you want if they're touching here
// put what you want if they aren't here


Cookies help us deliver our services. By using our services, you agree to our use of cookies.