From Test-Scratch-Wiki

There are many ways to jump depending on how you want a 角色 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.

简单的跳跃方式

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

當 @greenflag 被點擊
重複無限次 
  如果 <[向上 v] 鍵被按下?> 那麼 
    y 改變 (50)
    等待 (0.5) 秒
    y 改變 (-50)
  end // typical jumping key
end

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

當 @greenflag 被點擊
重複無限次 
  如果 <[向上 v] 鍵被按下?> 那麼 
    y 改變 (5)
  end // typical jumping key
end

It was used in the Scrolling Demo 专案 by SampleProjectsTeam.

坠落的方式

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

當 @greenflag 被點擊
重複無限次 
  如果 <[向上 v] 鍵被按下?> 那麼 
    y 改變 (50)
    重複直到 <碰到顏色 [#000000] ?> 
      y 改變 (-5)
    end // color of the ground
  end // typical jumping key
end

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

當 @greenflag 被點擊
重複無限次 
  如果 <[向上 v] 鍵被按下?> 那麼 
    重複 (10) 次 
      y 改變 (5)
    end
    重複直到 <碰到顏色 [#000000] ?> 
      y 改變 (-5)
    end // color of the ground
  end // typical jumping key
end

有限的跳跃

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.

當 @greenflag 被點擊
重複無限次 
  如果 <<[向上 v] 鍵被按下?> 且 <碰到顏色 [#000000] ?>> 那麼 
    y 改變 (5)
    jump //  category=custom
  end //  typical jumping key, color of the ground
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.

當 @greenflag 被點擊
重複無限次 
  如果 <碰到顏色 [#000000] ?> 那麼 
    變數 [jumps v] 設為 [0]
  end //  color of the ground
end

當 @greenflag 被點擊
重複無限次 
  如果 <<[向上 v] 鍵被按下?> 且 <碰到顏色 [#000000] ?>> 那麼 
    y 改變 (5) //  this will keep the first script for resetting the jump count
    變數 [jumps v] 改變 (1)
    jump //  category=custom
  
    如果 <<[向上 v] 鍵被按下?> 且 <(jumps) < [2]>> 那麼 
      變數 [jumps v] 改變 (1)
      jump //  category=custom
    end //  This will keep you from jumping more than twice
  end //  typical jumping key
end

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

物理上的精确跳跃

带地面侦侧

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

當 @greenflag 被點擊
變數 [y speed v] 設為 [0]
重複無限次 
  y 改變 (y speed)
end

當 @greenflag 被點擊
重複無限次 
  如果 <<[向上 v] 鍵被按下?> 且 <(y speed) = [0]>> 那麼 
    變數 [y speed v] 設為 (9.9)
    重複直到 <碰到顏色 [#000000] ?> 
      y 改變 (y speed)
      變數 [y speed v] 改變 (-0.5)
    end // the color of the platforms
    變數 [y speed v] 設為 (0)
  end // typical jumping key
end

不带地面侦测

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:

當 @greenflag 被點擊
重複無限次 
  如果 <[向上 v] 鍵被按下?> 那麼 
    變數 [y vel v] 設為 (10)
    重複 (20) 次 
      y 改變 (y vel)
      變數 [y vel v] 改變 (-1) // simulates gravity
    end
  end
end

进阶的跳跃方式

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":

當 @greenflag 被點擊
重複無限次 
  變數 [yvel v] 改變 (-0.2) //  simulates the force of gravity
  y 改變 (yvel)
  如果 <碰到 [platforms v] ?> 那麼 
    變數 [yvel v] 設為 ((-0.34) * (yvel)) //  bounce off of it
    y 改變 (yvel)
    如果 <碰到 [platforms v] ?> 那麼 
      y 改變 (yvel)
      如果 <碰到 [platforms v] ?> 那麼 
        y 改變 (yvel)
      end
    end
    如果 <<(yvel) > (0)> 且 <[向上 v] 鍵被按下?>> 那麼 
      變數 [yvel v] 設為 (4) //  jump magnitude (a higher number results in a higher jump)
    end //  jumping script
  end //  if the sprite hits an object
end

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

参见

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