2016-07-27 16 views
1

私はプラタフォームゲームを作っています。今はプレイヤーの動きを作っています。だから私が 'A'を押すと、プレーヤーは左に移動します(player.moveLeft())。 'D'を押すと、プレーヤーはrigth(player.moveRigth())に移動します。 'W'を押すと、プレーヤーはジャンプします(player.jump())。libGDX(Box2D)で体の衝動と力を止め重力を止める方法は?

public void moveLeft() { 
    if(Gdx.input.isKeyPressed(Keys.A) && 
     !Gdx.input.isKeyPressed(Keys.D) && 
     body.getLinearVelocity().x > -MAXIMUM_VELOCITY){ 
     left = true; 
     body.applyLinearImpulse(-3, 0, body.getPosition().x, body.getPosition().y, true); 
    }else if(Gdx.input.isKeyPressed(Keys.D) && 
      Gdx.input.isKeyPressed(Keys.A) && 
      !inTheAir){ 
     stop(); 
    }else if(!Gdx.input.isKeyPressed(Keys.A) && 
      !Gdx.input.isKeyPressed(Keys.D) && 
      !inTheAir){ 
     stop(); 
    } 
} 

public void moveRigth() { 
    if(Gdx.input.isKeyPressed(Keys.D) && 
     !Gdx.input.isKeyPressed(Keys.A) && 
     body.getLinearVelocity().x < MAXIMUM_VELOCITY){ 
     rigth = true; 
     body.applyLinearImpulse(3, 0, body.getPosition().x, body.getPosition().y, true); 
    }else if(Gdx.input.isKeyPressed(Keys.D) && 
      Gdx.input.isKeyPressed(Keys.A) && 
      !inTheAir){ 
     stop(); 
    }else if(!Gdx.input.isKeyPressed(Keys.D) && 
      !Gdx.input.isKeyPressed(Keys.A) && 
      !inTheAir){ 
     stop(); 
    } 
} 

public void stop(){ 
    body.setLinearVelocity(0, 0); 
    body.setAngularVelocity(0); 
} 

public void jump(){ 
    if(!inTheAir && Gdx.input.isKeyPressed(Keys.W)){ 
     inTheAir = true; 
     body.setLinearVelocity(0, 0); 
     body.setAngularVelocity(0); 
     body.applyLinearImpulse(0, 7, body.getPosition().x, body.getPosition().y, true); 
    } 
} 

それは動作しますが、私は問題を持っている:私はジャンプする前に「A」または「D」を押すと、プレイヤーがジャンプしていると私は、キーを離すと、プレイヤーは動き続けます。どうすれば修正できますか?私を助けてください!!

答えて

1

あなたはX軸速度を操作する必要があります。

Vector2 vel = body.getLinearVelocity(); 
vel.x = 0f; 
body.setLinearVelocity(vel); 

をこのように、Y軸速度は同じままですが、あなたのプレイヤーが横に移動しません。

+0

それは働いて、ありがとう! :D – Lordeblader

関連する問題