2016-02-18 110 views
0

这似乎是一个简单的修复,但我似乎无法得到它,xMove代表在x轴上移动,y在y轴上移动。目前,当我的玩家对象正在向下碰撞时,一个贴图(站在地面上)时,精灵将面向右侧。如果在此之前我已经向左移动,我希望我的球员能够面朝左。所以基本上我需要一种方法来记住我的角色面对的方式,并将其返回到我的代码的yMove == 0部分。任何人都可以给我任何建议吗?简单的二维Java游戏问题

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; 
} 

编辑:我试图搞乱布尔人以返回不同的子画面,例如,如果(面向左){返回Assets.playerFacingLeft},但这样做不知何故根本不返回图像。

回答

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(); 
    } 
} 
+0

使用此代码后我的球员静止时不会出现图像 – Alex

+0

已修复!您不需要检查'yMove == 0',如果前两个if语句为false,则yMove必须等于零。 –

+0

我添加了最后的'if(facingLeft)'语句来处理当玩家既不坠落也不静止的情况。 –

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; 
+0

我基本上要我跳动画和动画的下降是相同的,除非Java不也是这样吗? – Alex

+0

@Alex设置y当它小于0时,将其移动到0,当他尝试下去时,将其重置为y轴的正常0位置 –

+0

问题是:当yMove == 0时,它返回playerFacingRight,我想让我的当玩家向左移动并停止转动并朝向右侧时,我面对左侧或右侧的运动员,我希望它仍然朝向左侧 – Alex