Java LibGDX-Box2D播放器移动的代码效率
我使用LibGDX中的Box2D来移动我的播放器。世界重力设置为(0f,0f)。然而,我的代码感觉又长又钝,我觉得我可以对它进行重构以更有效地实现它,但我不确定如何实现。有什么我可以改进的吗Java LibGDX-Box2D播放器移动的代码效率,java,libgdx,Java,Libgdx,我使用LibGDX中的Box2D来移动我的播放器。世界重力设置为(0f,0f)。然而,我的代码感觉又长又钝,我觉得我可以对它进行重构以更有效地实现它,但我不确定如何实现。有什么我可以改进的吗 private void playerMovement() { if(Gdx.input.isKeyPressed(Input.Keys.W)){ body.setLinearVelocity(new Vector2(0f,30f)); }if(Gdx.input.isKey
private void playerMovement() {
if(Gdx.input.isKeyPressed(Input.Keys.W)){
body.setLinearVelocity(new Vector2(0f,30f));
}if(Gdx.input.isKeyPressed(Input.Keys.S)){
body.setLinearVelocity(new Vector2(0f, -30f));
}if(Gdx.input.isKeyPressed(Input.Keys.A)){
body.setLinearVelocity(new Vector2(-30f, 0f));
}if(Gdx.input.isKeyPressed(Input.Keys.D)){
body.setLinearVelocity(new Vector2(30f,0f));
}if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A)){
body.setLinearVelocity(new Vector2(-30f, 30f));
}if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D)){
body.setLinearVelocity(new Vector2(30f, 30f));
}if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A)){
body.setLinearVelocity(new Vector2(-30f, -30f));
}if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D)){
body.setLinearVelocity(new Vector2(30f, -30f));
}else if(!Gdx.input.isKeyPressed(Input.Keys.ANY_KEY)){
body.setLinearVelocity(new Vector2(0f,0f));
}
}
此方法是否适用于使用Box2D进行平滑移动?世界重力设置为(0f,0f)。不确定我是否可以写更少的代码。更有效的方法
static float speed = 30; // move speed outside method to not create it each frame.
private void playerMovement() {
if(Gdx.input.isKeyPressed(Input.Keys.W)){
body.setLinearVelocity(0f, speed); // removed Vector2(), it's not a good idea to cteate it each frame.
}if(Gdx.input.isKeyPressed(Input.Keys.S)){
body.setLinearVelocity(0f, -speed);
}if(Gdx.input.isKeyPressed(Input.Keys.A)){
body.setLinearVelocity(-speed, 0f);
}if(Gdx.input.isKeyPressed(Input.Keys.D)){
body.setLinearVelocity(speed,0f);
}if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A)){
body.setLinearVelocity(-speed, speed);
}if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D)){
body.setLinearVelocity(speed, speed);
}if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A)){
body.setLinearVelocity(-speed, -speed);
}if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D)){
body.setLinearVelocity(speed, -speed);
}else {
body.setLinearVelocity(0f,0f);
}
}
玩家移动的最终代码,如果有人想使用它。由于一些错误,我不得不修改答案的代码才能工作,但现在它工作正常了
private void playerMovement() {
float speed = 30f;
if(Gdx.input.isKeyPressed(Input.Keys.W) || Gdx.input.isKeyPressed(Input.Keys.UP)) {
if(Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
body.setLinearVelocity(-speed,speed);
}else if(Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
body.setLinearVelocity(speed, speed);
}else
body.setLinearVelocity(0f, speed);
}else if(Gdx.input.isKeyPressed(Input.Keys.S) || Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
if(Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
body.setLinearVelocity(-speed, -speed);
}else if(Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
body.setLinearVelocity(speed, -speed);
}else
body.setLinearVelocity(0f, -speed);
}else if(Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
body.setLinearVelocity(-speed, 0f);
}else if(Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
body.setLinearVelocity(speed, 0f);
}else
body.setLinearVelocity(0f, 0f);
}
谢谢你的编辑谢谢,我会做类似的事情:)你做得比第一次更糟。改用我的版本。如果我的代码有错误,请写信给我,我会修复它们。你的W+A和S+A不起作用。我现在又改了一次,应该更好。我当时重写了我的答案。很抱歉,我没有尝过。谢谢你的新答案和解释,但我会继续使用当前的代码,因为我也在使用左,右。。。它需要更多的代码。也谢谢你提醒我变量。