m (Method 2: unfortunately, the velocity is kind of necessary to make the jumps realistic)
Line 113: Line 113:
 
end
 
end
 
</scratchblocks>
 
</scratchblocks>
== Method 2 ==
 
A simpler way requires no special velocity code or color sensing on your sprite. It also allows you to have specific walls that only can be jumped on.
 
<scratchblocks>
 
when backdrop switches to [level1 v]
 
if <<key [left arrow v] pressed>or<key [a v] pressed>> then
 
change x by (-4)
 
end
 
if <<key [right arrow v] pressed>or<key [d v] pressed>> then
 
change x by (4)
 
end
 
if <<<<key [space v] pressed>or<key [up arrow v] pressed>>or<key [w v] pressed>>and<touching color [#000000]>> then
 
repeat (10)
 
change y by (5)
 
end
 
 
</scratchblocks>
 
One variable is needed. Gray is the color of the walls for wall jumping.
 
<scratchblocks>
 
if <<<not<touching color [#000000]>>and<touching color [#444444]>and<key [up arrow v] pressed>>> then
 
end
 
</scratchblocks>
 
 
 
== Examples ==
 
== Examples ==
  

Revision as of 03:11, 12 January 2019

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 gf clicked
set [R-vel v] to [0]
set [L-vel v] to [0]
forever
if <key [right arrow v] pressed?> then
change [R-vel v] by (1)
end
if <key [left arrow v] pressed?> then
change [L-vel v] by (1)
end
if <key [up arrow v] pressed?> then
if <color [#0000FF] is touching [#000000]?> then
set [Y-vel v] to [20]
broadcast [Jump v]
end
end
if <not <color [#0000FF] is touching [#000000]?>> then
change y by (-5)
end
if <color [#FF0000] is touching [#000000]?> then
set [Y-vel v] to [0]
set [R-vel v] to [0]
if <not < <color [#0000FF] is touching [#000000]?> or <key [up arrow v] pressed?> > > then
wait (0.5) secs
if <key [up arrow v] pressed?> then
set [Y-vel v] to [20]
broadcast [Jump v]
end
set [L-vel v] to [10]
end
end
if <color [#00FF00] is touching [#000000]?> then
set [Y-vel v] to [0]
set [L-vel v] to [0]
if <not < <color [#0000FF] is touching [#000000]?> or <key [up arrow v] pressed?> > > then
wait (0.5) secs
if <key [up arrow v] pressed?> then
set [Y-vel v] to [20]
broadcast [Jump v]
end
set [R-vel v] to [10]
end
end
change x by (R-vel)
change x by ((0) - (L-vel))
change [R-vel v] by ((R-vel) * (-0.2))
change [L-vel v] by ((L-vel) * (-0.2))
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:

when I receive [Jump v]
repeat (10)
if <color [#FFFF00] is touching [#000000]?> then
set [Y-vel v] to [0]
stop [this script v]
end
change y by (Y-vel)
change [Y-vel v] by (-1)
end
set [Y-vel v] to [0]

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

  • velocity — The velocity of the player's horizontal movement.
when gf clicked
forever
if <key [left arrow v] pressed> then
change [velocity v] by (-1)
end
if <key [right arrow v] pressed> then
change [velocity v] by (1)
end
if <not <touching [ground v]>> then
change y by (-10)
end
if <key [up arrow v] pressed> then
change y by (60)
end
change y by (1)
change x by (velocity)
if <touching [ground v]> then
change x by ((0)-(velocity))
if <key [up arrow v] pressed> then
change y by (60)
change x by ((0)-(velocity))
end
change y by (-1)
set [velocity v] to ((velocity)*(0.75))
end

Examples

See Also