简体   繁体   English

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

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

I'm currently trying to code my first GUI game, where the player jumps from obstacle to obstacle.我目前正在尝试编写我的第一个 GUI 游戏,其中玩家从一个障碍跳到另一个障碍。 The player is supposed to do a simple jump when the space bar is pressed (without actually moving on the x axis), but when it's pressed, the player will only go up and not come down.当按下空格键时,玩家应该做一个简单的跳跃(实际上并没有在 x 轴上移动),但是当按下空格键时,玩家只会上升而不会下降。

Here are the variables in my code:这是我的代码中的变量:

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

Here is my code to make the player jump:这是我让玩家跳跃的代码:

    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 ){}

Does anyone have an idea on why it is not working and how I can fix it?有没有人知道为什么它不起作用以及我如何解决它?

I don't know how the rest of your code looks or how your game works, but this is an Idea I have.我不知道你的其余代码看起来如何或你的游戏如何工作,但这是我的一个想法。

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