Wall sensors are commonly used in platform games. They consist of four lines, two vertical and two horizontal, fixed to the left, right, top, and bottom of the player sprite. When a sensor detects contact with a wall, a message broadcasted will tell the player sprite to invert its current velocity, making the wall deflect the player sprite.

Programming a Sensor Set

It is vital that the player sprite uses velocity to enhance the realism of movement (including gravitational velocity) before making wall sensors.

  1. Duplicate the player sprite.
  2. Erase all scripts and costumes (except the idle costume).
  3. Edit the idle costume. Draw four lines, each a different color, on each side (left, right, top, and bottom) of the costume.
  4. Erase the original costume. The result should look like four colored lines.
  5. Program the following scripts on the wall sensor sprite:
forever
if <color [#000000] is touching [#FFFFFF]?> then
broadcast ( bump v)

The white should be the color of the sensor(left, right, top, or bottom).

The black should be the color of the wall, or whatever the player can not move through.

The parentheses() should be replaced with whichever direction it is detecting. (up, down, left or right)

Do this four times, once for each sensor.

Program the following scripts on the player sprite:

when I receive [left bump v]
set [x velocity v] to (10)

when I receive [right bump v]
set [x velocity v] to (-10)

when I receive [up bump v]
set [y velocity v] to (10)

when I receive [down bump v]
set [gravity v] to (0)

Another simple method without broadcasts would be to simply move the

set [gravity v] to (0)

to replace the broadcast blocks. This only works if the variables are global and can be accessed by any sprite.

If done correctly, the player sprite should bounce off walls and stop when it jumps and hits a platform's bottom.

How it Works

This method uses colors to detect if a sprite is hitting a wall, and from which direction, and then reacts accordingly. It also makes use of the fact that the rendering speed is not instantaneous, allowing for the sprite to switch to its "sensing costume" and back with not noticeable effects to the user.

Without the Sensor Set

This third method does not use a "sensing costume", but demands that a constant costume is used for all sensing.

when gf clicked
set [y-vel v] to (0)
forever
if <key (right arrow v) pressed?> then
change x by (3)
if <touching (wall v)?> then
change x by (-3)
end
end
if <key (left arrow v) pressed?> then
change x by (-3)
if <touching (wall v)?> then
change x by (3)
end
end
change [y-vel v] by (-0.1) // gravity
change y by (y-vel)
if <touching (wall v)?> then // vertical collision
set [y-vel v] to ((-0.5)*(y-vel))
change y by (y-vel)
if <touching (wall v)?> then
change y by (y-vel)
end
if <<(y-vel) > (0)> and <key (up arrow v) pressed?>> then // jump
set [y-vel v] to (5)
end
end

How it Works

This method works around color sensing by checking if it is touching a wall after essentially every movement along one dimension. For instance, if the sprite moves right, it then checks if it is touching a wall. If it is touching a wall, it knows that it must be a wall on its right.

Troubleshooting

You may have problems with sensing walls properly. If you do, make sure that:

  • You have all of the proper variables.
  • Your left-moving script changes the x velocity variable by a negative number, and that your right-moving one changes it by a positive number.
  • Your jumping script changes the y velocity variable by a positive number.
  • If you have multiple ground colors (only in the top layer of pixels, where the player sprite lands), repeat step 5 and program four multiples of the following script, each one representing each sensor.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.