如何处理同时按下和释放键?

时间:2019-01-26 20:28:07

标签: java javafx

我写了一个小程序,相机在其中移动。当我同时向左和向右按下并释放导航键时,相机会减速一秒钟,然后才开始朝着所按键的方向移动。如何避免这种制动?

public class ControlCameraV2 {
PerspectiveCamera ourcamera;
int accelerationX = 0;
int accelerationY = 0;
int speedX = 0;
int speedY = 0;
int frictionForceX = 0;
int frictionForceY = 0;
public ControlCameraV2(PerspectiveCamera camera){ourcamera = camera;}
public void speedX(int acceleration){ accelerationX = acceleration;}
public void speedY(int acceleration){accelerationY = acceleration;}
public void brakingX(){accelerationX = 0;}
public void brakingY(){accelerationY = 0;}
public void Demonstration(){
    if(speedX > 0) frictionForceX = -1;
    if(speedX < 0) frictionForceX = 1;
    if (speedY > 0) frictionForceY = -1;
    if (speedY < 0) frictionForceY = 1;
    if (speedY == 0) frictionForceY = 0;
    if(speedX < 14 && speedX > -14){speedX = speedX + accelerationX;}
    if(speedY < 14 && speedY > -14){speedY = speedY + accelerationY;}
    speedX = speedX + frictionForceX;
    speedY = speedY + frictionForceY;
    if (speedX == 0) frictionForceX = 0;
    if (speedY == 0) frictionForceY = 0;
    ourcamera.setTranslateZ(ourcamera.getTranslateZ()+speedY);
    ourcamera.setTranslateX(ourcamera.getTranslateX()+speedX);
}

}

ourscene.setOnKeyReleased(e -> {
        if(e.getCode() == KeyCode.UP || e.getCode() == KeyCode.DOWN){ ourcontrol.brakingY(); codes.remove(KeyCode.UP); codes.remove(KeyCode.DOWN);}
        if(e.getCode() == KeyCode.RIGHT || e.getCode() == KeyCode.LEFT){ ourcontrol.brakingX();codes.remove(KeyCode.RIGHT);codes.remove(KeyCode.LEFT);}
        });

    ourscene.setOnKeyPressed(e -> {
        if(acceptedKey.contains(e.getCode()))
        {
            codes.add(e.getCode());
            if(codes.contains(KeyCode.UP))
            {
                ourcontrol.speedY(2);
            }
            if(codes.contains(KeyCode.DOWN))
            {
                ourcontrol.speedY(-2);
            }
            if(codes.contains(KeyCode.RIGHT))
            {
                ourcontrol.speedX(2);
            }
            if(codes.contains(KeyCode.LEFT))
            {
                ourcontrol.speedX(-2);
            }
        }
    });

这是到存储库的链接。有三个文件。 2个文件,其中包含对象的代码和纹理。 BumP

0 个答案:

没有答案