1
我正在製作一個平臺遊戲,現在我正在製作玩家動作。所以當我按下'A'時,播放器向左移動(player.moveLeft());當我按下'D'時,玩家將移動到最接近的位置(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」,而當玩家跳躍,我鬆開按鍵,玩家不斷前進。我該如何解決?請幫幫我!!
它的工作,非常感謝你! :d – Lordeblader