In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

If on Edge, Bounce (block)

If on Edge, Bounce
2.0 If On Edge, Bounce.png
Category Motion
Type Stack

The If on Edge, Bounce block is a Motion Block and a Stack Block. The block checks to see if its sprite is touching the edge of the screen with the move steps block — and if it is, the sprite will point in a direction that mirrors the direction from which it is coming. It uses a line perpendicular to the edge to determine the reflection angle.

Example Uses

  • Preventing a sprite from partially leaving the screen (often, this is followed by the Point in Direction block to stop reflection)
  • Reflection
  • Games such as "Pong"
  • Allowing sprites to bounce off the screen:
when gf clicked
forever
if on edge, bounce
move (5) steps
  • It also works for games such as Fish.
forever
turn cw (pick random (-10) to (10)) degrees
move (3) steps
if on edge, bounce
wait (0.01) secs

Workaround

Main article: List of Block Workarounds

The If on Edge, Bounce block can be replicated with the following code, which uses some trigonometry:

if <touching [edge v]?> then
change x by (([sin v] of (direction)) * (-1))
if <touching [edge v]?> then
point in direction ((180) - (direction))
else
point in direction ((0) - (direction))
end
change x by ([sin v] of (direction))
end
  • This page was last modified on 25 March 2014, at 12:06.