繁体   English   中英

为什么我的播放器在跳跃后不下来?

[英]Why is my player not coming down after jumping?

我目前正在尝试编写我的第一个 GUI 游戏,其中玩家从一个障碍跳到另一个障碍。 当按下空格键时,玩家应该做一个简单的跳跃(实际上并没有在 x 轴上移动),但是当按下空格键时,玩家只会上升而不会下降。

这是我的代码中的变量:

private int playerY = 415, playerX = 100, score=0, maxHeight = 350;
private float speed=3, jumpStrength, weight=1;

这是我让玩家跳跃的代码:

    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_SPACE && playerY>=maxHeight) {
        jumpStrength = 24;
        playerY -= jumpStrength;
        jumpStrength -= weight;
    }
    if (playerY>=maxHeight){
        playerY = maxHeight;
    }
}

public void keyTyped( KeyEvent e )   {}

public void keyReleased( KeyEvent e ){}

有没有人知道为什么它不起作用以及我如何解决它?

我不知道你的其余代码看起来如何或你的游戏如何工作,但这是我的一个想法。

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_SPACE) {
        jumpStrength = 24;
        jumpStrength += weight;
        playerY -= jumpStrength;
    }
    if (playerY>=maxHeight){
        playerY = maxHeight;
    }
}

public void keyTyped( KeyEvent e )   {}

public void keyReleased( KeyEvent e ){
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_SPACE) {
        jumpStrength = 24;
        jumpStrength += weight;
        playerY -= jumpStrength;
    }
    if (playerY<=0){
        playerY = 0;
    }
}

暂无
暂无

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

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