From Test-Scratch-Wiki

Translate: - English - 中文 

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 角色. 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 速度 to enhance realism of movement (including gravitational velocity) before making wall sensors.

  1. Duplicate the player sprite.
  2. Erase all 程式s and 造型 (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:
重複無限次 
  如果 <顏色 [#000000] 碰到顏色 [#FFFFFF] ?> 那麼 
    廣播訊息 [() bump v]
  end
end

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:

當收到訊息 [left bump v]
變數 [x velocity v] 設為 (10)

當收到訊息 [right bump v]
變數 [x velocity v] 設為 (-10)

當收到訊息 [up bump v]
變數 [y velocity v] 設為 (10)

當收到訊息 [down bump v]
變數 [gravity v] 設為 (0)

Another simple method without 广播 would be to simply move the

變數 [gravity v] 設為 (0)

to replace the broadcast 积木. This only works if the 变量s are global and can be access 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 be used for all sensing.

當 @greenflag 被點擊
變數 [y-vel v] 設為 (0)
重複無限次 
  如果 <[向右 v] 鍵被按下?> 那麼 
    x 改變 (3)
    如果 <碰到 [wall v] ?> 那麼 
      x 改變 (-3)
    end
  end
  如果 <[向左 v] 鍵被按下?> 那麼 
    x 改變 (-3)
    如果 <碰到 [wall v] ?> 那麼 
      x 改變 (3)
    end
  end
  變數 [y-vel v] 改變 (-0.1) //  gravity
  y 改變 (y-vel)
  如果 <碰到 [wall v] ?> 那麼 
    變數 [y-vel v] 設為 ((-0.5) * (y-vel))
    y 改變 (y-vel)
    如果 <碰到 [wall v] ?> 那麼 
      y 改變 (y-vel)
    end
    如果 <<(y-vel) > (0)> 且 <[向上 v] 鍵被按下?>> 那麼 
      變數 [y-vel v] 設為 (5)
    end //  jump
  end //  vertical collision
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 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 xvelocity variable by a negative number, and that your right-moving one changes it by a positive number.
  • Your jumping script changes the yvelocity 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.