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:
when clicked set R-vel to 0 set L-vel to 0 forever if key right arrow pressed? then change R-vel by 1 end if key left arrow pressed? then change L-vel by 1 end if key up arrow pressed? then if color is touching ? then set Y-vel to 20 broadcast Jump end end if not color is touching ? then change y by -5 end if color is touching ? then set Y-vel to 0 set R-vel to 0 if not color is touching ? or key up arrow pressed? then wait 0.5 secs if key up arrow pressed? then set Y-vel to 20 broadcast Jump end set L-vel to 10 end end if color is touching ? then set Y-vel to 0 set L-vel to 0 if not color is touching ? or key up arrow pressed? then wait 0.5 secs if key up arrow pressed? then set Y-vel to 20 broadcast Jump end set R-vel to 10 end end change x by R-vel change x by 0 - L-vel change R-vel by R-vel * -0.2 change L-vel by L-vel * -0.2
Шаблон:Note
And one for jumping:
when I receive Jump repeat 10 if color is touching ? then set Y-vel to 0 stop this script end change y by Y-vel change Y-vel by -1 end set Y-vel to 0
There is another way of doing it.
One variable is needed
velocity — The velocity of the player's horizontal movement.
when clicked forever if key left arrow pressed? then change velocity by -1 end if key right arrow pressed? then change velocity by 1 end if not touching ground ? then change y by -10 end if key up arrow pressed? then change y by 60 end change y by 1 change x by velocity if touching ground ? then change x by 0 - velocity if key up arrow pressed? then change y by 60 change x by 0 - velocity end change y by -1 set velocity to velocity * 0.75 end
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.
when clicked set wall jump to 0 forever if not touching color ? and touching color ? and key space pressed? or key up arrow pressed? then if not wall jump = 1 then set wall jump to 1 repeat 10 change y by 15 end wait until color is touching ? set wall jump to 0 end
A separate code is needed to make movement.
when clicked forever if key left arrow pressed? then change x by -4 end if key right arrow pressed? then change x by 4 end if key space pressed? and color is touching ? then repeat 10 change y by 15 end end if not color is touching ? then change y by -7 end if color is touching ? then change y by -8
Examples
See Also
ja:壁キックジャンプ