Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Jumping

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.

Contents

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

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

  • This page was last modified on 22 April 2014, at 10:00.