简体   繁体   English

Java切换案例延迟

[英]Java switch case delay

I'm programming a little 2d game. 我正在编写一个2D小游戏。 Acutally I'm busy with the jumping-function. 我有点忙于跳跃功能。 I want my player to jump, when i press the up-arrow key. 当我按向上箭头键时,我希望播放器跳起来。 I managed to make him jumping, but there is a delay of at least one second. 我设法让他跳了起来,但至少延迟了一秒钟。 This dealy is not existing for other actions like moving left or right. 对于其他操作(例如向左或向右移动)不存在此协议。 Do you know where it comes from? 你知道它从哪里来吗?

The code for jumping is in the keyReaction method. 跳转的代码在keyReaction方法中。

public class John<I> extends AbstractGame<I> {
//lists
List<ImageObject<I>> floor = new ArrayList<>();

//variables
double jumpHeight = 0;

//boolean
boolean jump = false;

//fix values
int blockHeight = 40;
int blockWidth = 40;

public John(double width, double height) {
    //getPlayer()
    super(new ImageObject<>("player.png", new Vertex(120, height - 200), new Vertex(0, 0)), width, height);

    //floor objects
    for(int i=0; i<getWidth(); i+=blockWidth)
        floor.add(new ImageObject<>("brick.png", new Vertex(i, getHeight() - blockHeight)));

    //add arrays to canvas
    getGOss().add(floor);
}

@Override
public void doChecks() {
    for(ImageObject<I> f:floor) {
        //gravity
        if(!(jump) && !(getPlayer().touches(f)))
            getPlayer().getMotion().y++;

        //stop y-motion on ground
        if(getPlayer().touches(f)){
            getPlayer().getPos().y = getHeight() - blockHeight - getPlayer().getHeight();
            getPlayer().getMotion().y = 0;}
    }
}

@Override
public boolean isStopped(){
    return false;
}

@Override
public void keyReaction(KeyCode keycode){
    switch (keycode) {
        //move left
        case LEFT_ARROW:
            getPlayer().getMotion().x = -2;
            break;
        case VK_A:
            getPlayer().getMotion().x = -2;
            break;
        //move right
        case RIGHT_ARROW:
            getPlayer().getMotion().x = 2;
            break;
        case VK_D:
            getPlayer().getMotion().x = 2;
            break;
        //jump
        case UP_ARROW:
            jump = true;
            if(jumpHeight < 100){
                jumpHeight += 5;
                getPlayer().getPos().y -=5;

            } else if (jumpHeight < 200){
                jumpHeight +=5;
                getPlayer().getPos().y +=5;
            }
            break;
        case VK_W:
            break;
        case SPACE:
            break;
    }
}

@Override
public void keyReactionReleased(KeyCode keycode){
    switch (keycode) {
        //move left
        case LEFT_ARROW:
            getPlayer().getMotion().x = 0;
            break;
        case VK_A:
            getPlayer().getMotion().x = 0;
            break;
        //move right
        case RIGHT_ARROW:
            getPlayer().getMotion().x = 0;
            break;
        case VK_D:
            getPlayer().getMotion().x = 0;
            break;
        //jump
        case UP_ARROW:
            jump = false;
            jumpHeight = 0;
            break;
        case VK_W:
            jump = false;
            break;
        case SPACE:
            break;
    }
}

//test main
public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new SwingScreen(new John<>(1800, 600)));
    f.pack();
    f.setVisible(true);
}
}

Thank you for your help. 谢谢您的帮助。

Code with the if: 使用if进行编码:

case UP_ARROW:
    if(jumpHeight < 20){
        jumpHeight += 5;
        getPlayer().getPos().y = -5;

    } else if (jumpHeight < 40){
        jumpHeight +=5;
        getPlayer().getPos().y = 5;
    }
    break;

Code without the if: 不含if的代码:

case UP_ARROW:
   getPlayer().getPos().y = -5;
   break;

KeyReaction: KeyReaction:

setOnKeyPressed((ev)->{
    logic.keyReaction(KeyCode.fromCode(ev.getCode().impl_getCode()));
    ev.consume();
        });

setOnKeyReleased((ev)->{
    logic.keyReactionReleased(KeyCode.fromCode(ev.getCode().impl_getCode()));
    ev.consume();
        });

In your jumpheight method you have 在您的jumpheight方法中

jump = true;
if(jumpHeight < 100){
  jumpHeight += 5;
  getPlayer().getPos().y -=5;
} else if (jumpHeight < 200){
  jumpHeight +=5;
  getPlayer().getPos().y +=5;
}
break;

On the 4th line there, you have a -=5 for the pos. 在第四行,您的位置为-= 5。 There is likely another method that prevents the ypos from going under 0. What happens then is you press the up arrow, it gets sent 40 times(200/5), eventually the jumpheight is greater than 200, and only then does the player start moving up. 可能还有另一种方法可以防止ypos低于0。然后发生的事情是您按下向上箭头,它被发送了40次(200/5),最终jumpheight大于200,然后玩家才开始向上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM