From Test-Scratch-Wiki

Wall-Jumping is a technique used in Scratch to make 角色s bounce off walls. This allows a character in a project, for instance, to scale up massive heights of walls rather than jumping on individualized sections like stairs. It is commonly used in 游戏平台.

Method

This is just one of the ways for wall jumping to work.

Three variables are needed for this method to operate properly:

  • L-vel

— The 速度 of the player going left

  • R-vel

— The velocity of the player going right

  • Y-vel

— The velocity of the player on the Y 座标

Afterwards, a player sprite should be made. This is the sprite that will perform all motions and be physically wall-jumping. An example of a player sprite is shown below:

Wall Jumping 4.png

The colours on each side of the square are the sensors that will use to "sense" which side the wall and floor are relative to the character. Using more complicated coding, it wouldn't require colours.

Two separate scripts are needed then, one for moving and one for sensing walls:

當 @greenflag 被點擊
變數 [R-vel v] 設為 [0]
變數 [L-vel v] 設為 [0]
重複無限次 
  如果 <[向右 v] 鍵被按下?> 那麼 
    變數 [R-vel v] 改變 (1)
  end
  如果 <[向左 v] 鍵被按下?> 那麼 
    變數 [L-vel v] 改變 (1)
  end
  如果 <[向上 v] 鍵被按下?> 那麼 
    如果 <顏色 [#0000FF] 碰到顏色 [#000000] ?> 那麼 
      變數 [Y-vel v] 設為 [20]
      廣播訊息 [Jump v]
    end
  end
  如果 <<顏色 [#0000FF] 碰到顏色 [#000000] ?> 不成立> 那麼 
    y 改變 (-5)
  end
  如果 <顏色 [#FF0000] 碰到顏色 [#000000] ?> 那麼 
    變數 [Y-vel v] 設為 [0]
    變數 [R-vel v] 設為 [0]
    如果 <<<顏色 [#0000FF] 碰到顏色 [#000000] ?> 或 <[向上 v] 鍵被按下?>> 不成立> 那麼 
      等待 (0.5) 秒
      如果 <[向上 v] 鍵被按下?> 那麼 
        變數 [Y-vel v] 設為 [20]
        廣播訊息 [Jump v]
      end
      變數 [L-vel v] 設為 [10]
    end
  end
  如果 <顏色 [#00FF00] 碰到顏色 [#000000] ?> 那麼 
    變數 [Y-vel v] 設為 [0]
    變數 [R-vel v] 設為 [0]
    如果 <<<顏色 [#0000FF] 碰到顏色 [#000000] ?> 或 <[向上 v] 鍵被按下?>> 不成立> 那麼 
      等待 (0.5) 秒
      如果 <[向上 v] 鍵被按下?> 那麼 
        變數 [Y-vel v] 設為 [20]
        廣播訊息 [Jump v]
      end
      變數 [R-vel v] 設為 [10]
    end
  end
  x 改變 (R-vel)
  x 改變 ((0) - (L-vel))
  變數 [R-vel v] 改變 ((R-vel) * (-0.2))
  變數 [L-vel v] 改變 ((L-vel) * (-0.2))
end


Note Note: The color black can be changed depending on the color of the wall and floor sprites, but the floor and walls must be the same color.

And one for jumping:

當收到訊息 [Jump v]
重複 (10) 次 
  如果 <顏色 [#FF0000] 碰到顏色 [#000000] ?> 那麼 
    變數 [Y-vel v] 設為 [0]
    stop script
  end
  y 改變 (Y-vel)
  變數 [Y-vel v] 改變 (-1)
end
變數 [Y-vel v] 設為 [0]

There is another way of doing it. One variable is needed

  • velocity

— The velocity of the player's horizontal movement.

當 @greenflag 被點擊
重複無限次 
  如果 <[向左 v] 鍵被按下?> 那麼 
    變數 [velocity v] 改變 (-1)
  end
  如果 <[向右 v] 鍵被按下?> 那麼 
    變數 [velocity v] 改變 (1)
  end
  如果 <<碰到 [ground v] ?> 不成立> 那麼 
    y 改變 (-10)
  end
  如果 <[向上 v] 鍵被按下?> 那麼 
    y 改變 (60)
  end
  y 改變 (1)
  x 改變 (velocity)
  如果 <碰到 [ground v] ?> 那麼 
    x 改變 ((0) - (velocity))
    如果 <[向上 v] 鍵被按下?> 那麼 
      y 改變 (60)
      x 改變 ((0) - (velocity))
    end
    y 改變 (-1)
    變數 [velocity v] 設為 ((velocity) * (0.75))
  end
end

示例

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