简单的 2d Java 游戏问题

Simple 2d Java game issue

这似乎是一个简单的解决方案,但我似乎无法理解,xMove 代表在 x 轴上移动,y 代表 y 轴。目前,当我的播放器对象向下与瓷砖(站在地面上)碰撞时,精灵将面向右侧。如果我在那之前一直向左移动,我希望我的球员面向左边。所以基本上我需要一种方法来记住我的角色面对的方向以及 return 我代码的 yMove == 0 部分。谁能给我任何建议?

private BufferedImage getCurrentAnimationFrame(){ //set Player animations when moving


    if(xMove > 0){
        facingRight = true;
        facingLeft = false;
        return animRight.getCurrentFrame();

    }
    if(xMove < 0){
        facingLeft = true;
        facingRight = false;
        return animLeft.getCurrentFrame();
    }
    if(yMove > 0){
        return Assets.playerFall;
    }

    if(yMove < 0){
        return Assets.playerFall;
    }
    if(yMove == 0){
        return Assets.playerFacingRight;
    }

        return null;
}

编辑:我试着把布尔值弄乱到 return 不同的精灵,例如if(facing Left){return Assets.playerFacingLeft} 但是这样做根本 return 根本不是图像。

假设您是这样考虑 x 轴和 y 轴的:-

if(xMove > 0){
    facingRight = true;
    facingLeft = false;
    return animRight.getCurrentFrame();

}
if(xMove < 0){
    facingLeft = true;
    facingRight = false;
    return Assets.playerFacingLeft; // here make the player turn left
}
if(yMove > 0){
    return Assets.playerFall
}

if(yMove == 0){
    return Assets.playerFacingRight;
}
if(yMove < 0){
   // fall down or do nothing if you need it to do nothing you can avoid this check or set ymov back to 0
  yMove = 0;
}

    return null;

你只需要重新安排你的代码:首先处理y轴移动。如果没有垂直运动,则检查水平运动。我添加了最后的 if(facingLeft) 语句来处理玩家既没有跌倒也没有站着不动的情况:

private BufferedImage getCurrentAnimationFrame(){ //set Player animations when moving

    if(yMove > 0){
        return Assets.playerFall;
    }

    if(yMove < 0){
        return Assets.playerFall;
    }
    if(xMove > 0){
        facingRight = true;
        facingLeft = false;
        return animRight.getCurrentFrame();
    }
    if(xMove < 0){
        facingLeft = true;
        facingRight = false;
        return animLeft.getCurrentFrame();
    }

    if(facingLeft){
        return animLeft.getCurrentFrame();
    } else {
        return animRight.getCurrentFrame();
    }
}