(Redirected from Touching ()? (block))

"Touching" redirects here. For the block that senses if a sprite is touching a color, see Touching Color ()?.
Touching ()?
touching ( v)?
Category Sensing
Type Boolean
Introduced in 11Jun04 (0.x)

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 depending on the block's argument (a reporter block which returns the sprite's name can be inserted into the block's argument). If the sprite is touching the selected object, the block returns true; if the sprite is not, the block 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.

Detection

Whether or not a sprite is touching another depends on whether or not a part of the sprite's visual texture (defined by its Costume) is touching that of another. For example, if a sprite's Costume is that of a circle, it will have a circular hitbox when detecting if it is touching other sprites.

Note Note: The collision detection data of this block can be finicky and inaccurate with large or moving sprites, particularly when applied to physics engines.

Example Uses

Because this block checks if its sprite is touching another sprite or an edge, it is widely used in detecting collisions.

Some common uses for the Touching ()? block include:

when I receive [move sprite v]
move (3) steps
if <touching (collision v)?> then
    move (-3) steps
end
  • Moving a sprite until it touches the edge of the screen
repeat until <touching (edge v)?>
   move (3) steps
end
  • Checking if a sprite is touching the mouse pointer
when flag clicked
forever
    if <touching (mouse-pointer v)?> then
        switch costume to (highlighted v)
    else
        switch costume to (normal v)
    end
end
repeat until <touching (mouse-pointer v)?>
   point towards (mouse-pointer v)
   move (3) steps
end
say [I got you!] for (2) seconds
  • Checking if the player has touched an enemy in a game
forever
    if <touching (Enemy v)?> then
        say [Game over!]
        stop [all v]
    end
end

Clones Touching Clones

Normally, most blocks targeting other sprites will not act on one of the sprite’s clones, but for the parent sprite. However, with this block, if <touching (sprite2 v)?> is used in a clone of sprite1, not only will it report true when touching the parent sprite, but also when touching any clone.[1]

Sprite touching itself

This can be done by dragging a if <touching ( v) ?> block from another sprite into this sprite. For example, if sprite 1 wants to detect if it is touching itself, then you can drag a if <touching ( sprite1) ?> block from sprite 2 into sprite 1.

Related Suggestion

Some Scratchers want a block that detects if two different sprites are touching.[2] 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 likely look like this:

<(sprite1 v) is touching (sprite2 v)? ::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]
 else
  set [Sprite1 Touching Sprite2? v] to [no]
 end


// Place this in any sprite to check for the touching
broadcast (Check for Touching v) and wait
if <(Sprite1 Touching Sprite2?) = [yes]> then
    ...// put the desired output if touching here
else
    ...// put what you want if they aren't here
end

Note that this workaround adds a delay every time whenever it is used due to the use of the Broadcast () and Wait block.

References

  1. projects:171158267
  2. Ask_Pinkamena. (19/7/2015). “Sprite touching anything.[title]” topic:135012

See Also

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