简单的2d Java游戏问题

时间:2016-02-18 22:53:48

标签: java sprite

这看似简单易行,但我似乎无法得到它,xMove表示在x轴上移动,y表示y轴。目前,当我的玩家对象向下碰撞时,一块瓷砖(站在地面上),精灵将朝右。如果我在此之前一直向左移动,我希望我的玩家面朝左。所以基本上我需要一种方法来记住我的角色面对的方式并将其返回到我的代码的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 Assets.playerFacingLeft}但是以某种方式执行此操作根本不会返回图像。

2 个答案:

答案 0 :(得分:1)

假设你认为x和y轴是这样的: -

enter image description here

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;

答案 1 :(得分:1)

您只需要重新排列代码:首先处理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();
    }
}