# 如何使一个对象不止一次反弹（Java和LWJGL）？How to make an object bounce off the ground more than once (Java and LWJGL)?

``````       if (y <= 48) { // if player is on the ground
yspeed = 0;
y = 48;

// Friction on ground

if ((!Keyboard.isKeyDown(Keyboard.KEY_LEFT)) && xspeed < 0) xspeed = xspeed * 0.925;
if ((!Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) && xspeed > 0) xspeed = xspeed * 0.925;

if (jumped == true)
{
yspeed += 4.2;
jumped = false;
}

// Jump

if (Keyboard.isKeyDown(Keyboard.KEY_UP))
{
yspeed = 10;
jumped = true;

}
``````

## 1 个回复1

### ===============>>#1 票数：1 已采纳

``````boolean airborne = false;

while (true) // the main game loop
{
if (y <= 48 && airborne) // if player just hit the ground
{
//  Perform the "bounce" by changing their vertical direction and decreasing its magnitude
ySpeed = -ySpeed/2.0;

//  This will stop them from bouncing infinitely.  After the bounce gets too small,
//  they will finally land on the ground.  I suggest you play around with this number to find one that works well
if (Math.abs(ySpeed) <= 0.5)
{
//  Stop the bouncing
ySpeed = 0;

//  Place them on the ground
airborne = false;
y = 48;
}
}
//  Apply friction if they are on the ground
if (!airborne)
{
// Friction on ground
if ((!Keyboard.isKeyDown(Keyboard.KEY_LEFT)) && xspeed < 0) xspeed = xspeed * 0.925;
if ((!Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) && xspeed > 0) xspeed = xspeed * 0.925;
}
//  Apply a jump action if they're pressing the jump button while on the ground
if (Keyboard.isKeyDown(Keyboard.KEY_UP) && !airborne)
{
airborne = true;
yspeed = 10;
}
}
``````

1回复

1回复

2回复

1回复

1回复

1回复

1回复

2回复

1回复

1回复