From Test-Scratch-Wiki

There are many ways to jump depending on how you want a sprite to jump. This article contains a number of them, categorized from least-realistic to most realistic. The key is usually used for jumping in games, but sometimes Space, Z,X,W, or even the mouse is used for jumping.

Simple jumping

The following is commonly used in animations and results in a sprite effectively teleporting upwards, and then downwards again.

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    change y by (50)
    wait (0.5) secs
    change y by (-50)
  end

The below script results in the sprite moving up if the up arrow is pressed.

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    change y by (5)
  end

It was used in the Scrolling Demo project by SampleProjectsTeam.

Falling

The following script "teleports" the sprite upwards, and then has it fall back down at a constant rate, until it lands on a platform.

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    change y by (50)
    repeat until <touching color [#000000]?> //color of the ground
      change y by (-5)
    end
  end
end

The next script makes the sprite rise up at a constant rate and fall back down at a constant rate.

when gf clicked
forever
if <key [up arrow v] pressed?> then //typical jumping key
repeat (10)
change y by (5)
end
repeat until <touching color [#000000]?> //color of the ground
change y by (-5)
end
end

Limited Jumps

In most video games, you cannot jump more than a certain number of times, and you cannot jump in midair. This can be implemented in Scratch.

The purple blocks labeled "jump" represent a compatible jump script.

To allow you to only jump on the ground, use the following script.

when gf clicked
forever
if <<key [up arrow v] pressed?> and <touching color [#000000]>> then // typical jumping key, color of the ground
change y by (5)
jump // category=custom
end

Double jumping is a common element in video games. Although not possible in real life, these scripts can make you jump up to two times.

when gf clicked
forever
if <touching color [#000000]?> then // color of the ground
set [jumps v] to [0]
end

when gf clicked
forever
if <<key [up arrow v] pressed?> and <touching color [#000000]>> then // typical jumping key
change y by (5) // this will keep the first script for resetting the jump count
change [jumps v] by (1)
jump // category=custom
else
if <<key [up arrow v] pressed?> and <(jumps) < [2]>> then // This will keep you from jumping more than twice
change [jumps v] by (1)
jump // category=custom
end
end

To make the sprite jump up to n times, change the 2 to an n.

Physically Accurate Jumping

With Ground Detection

A more realistic effect for jumping commonly used in games is the following, using a variable to control the vertical speed of a sprite (simulating gravity) is this:

when green flag clicked
set [y speed v] to [0] 
forever
  change y by (y speed)

when green flag clicked
forever
  if <<key [up arrow v] pressed?> and <(y speed) = [0]>> then //typical jumping key
    set [y speed v] to (9.9)
    repeat until <touching color [#000000]?> //the color of the platforms
      change y by (y speed)
      change [y speed v] by (-0.5)
    end
    set [y speed v] to (0)
  end

Without Ground Detection

If one does not need to sense the ground and simply want a realistic, gravitational jump, that will end at the starting point, the following script can replicate the jump:

when gf clicked
forever
if <key [up arrow v] pressed?> then
set [y vel v] to (10) 
repeat (20)
change y by (y vel)
change [y vel v] by (-1) //simulates gravity
end
end

Advanced Jumping

The following script includes velocity-based jumping and advanced landing. The sprite is constantly acted upon by the force of gravity and cannot pass through sprites. The sprite may appear to "bounce" slightly when "at rest":

when gf clicked
forever
change [yvel v] by (-0.2) // simulates the force of gravity
change y by (yvel)
if <touching [platforms v]?> then// if the sprite hits an object
set [yvel v] to ((-0.34)*(yvel)) // bounce off of it
change y by (yvel)
if <touching [platforms v]?> then
change y  by (yvel)
if <touching [platforms v]?> then
change y by (yvel)
end
end
if <<(yvel) > (0)>and<key [up arrow v] pressed?>> then// jumping script
set [yvel v] to (4) // jump magnitude (a higher number results in a higher jump)
end

In the above script, gravity was added to create a realistic effect; you can read more about simulating gravity here.

See Also

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