Wall-Jumping

Материал из Поле цифровой дидактики

Wall-Jumping is a technique used in Scratch to make sprites 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 platformers.

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 velocity of the player going left
  • R-vel — The velocity of the player going right
  • Y-vel — The velocity of the player on the y axis

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 Sprite.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:

whenclickedsetR-velto0setL-velto0foreverifkeyrightarrowpressed?thenchangeR-velby1endifkeyleftarrowpressed?thenchangeL-velby1endifkeyuparrowpressed?thenifcoloristouching?thensetY-velto20broadcastJumpendendifnotcoloristouching?thenchangeyby-5endifcoloristouching?thensetY-velto0setR-velto0ifnotcoloristouching?orkeyuparrowpressed?thenwait0.5secsifkeyuparrowpressed?thensetY-velto20broadcastJumpendsetL-velto10endendifcoloristouching?thensetY-velto0setL-velto0ifnotcoloristouching?orkeyuparrowpressed?thenwait0.5secsifkeyuparrowpressed?thensetY-velto20broadcastJumpendsetR-velto10endendchangexbyR-velchangexby0-L-velchangeR-velbyR-vel*-0.2changeL-velbyL-vel*-0.2

Шаблон:Note

And one for jumping:

whenIreceiveJumprepeat10ifcoloristouching?thensetY-velto0stopthisscriptendchangeybyY-velchangeY-velby-1endsetY-velto0

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

  • velocity — The velocity of the player's horizontal movement.
whenclickedforeverifkeyleftarrowpressed?thenchangevelocityby-1endifkeyrightarrowpressed?thenchangevelocityby1endifnottouchingground?thenchangeyby-10endifkeyuparrowpressed?thenchangeyby60endchangeyby1changexbyvelocityiftouchingground?thenchangexby0-velocityifkeyuparrowpressed?thenchangeyby60changexby0-velocityendchangeyby-1setvelocitytovelocity*0.75end

Method 2

An easier way uses very simple coding. The sprite can be any color, and no velocity script is needed. Gray should be the color of the walls.

whenclickedsetwalljumpto0foreverifnottouchingcolor?andtouchingcolor?andkeyspacepressed?orkeyuparrowpressed?thenifnotwalljump=1thensetwalljumpto1repeat10changeyby15endwaituntilcoloristouching?setwalljumpto0end

A separate code is needed to make movement.

whenclickedforeverifkeyleftarrowpressed?thenchangexby-4endifkeyrightarrowpressed?thenchangexby4endifkeyspacepressed?andcoloristouching?thenrepeat10changeyby15endendifnotcoloristouching?thenchangeyby-7endifcoloristouching?thenchangeyby-8

Examples

See Also

ja:壁キックジャンプ