简体   繁体   English

简单的2D Java游戏问题

[英]Simple 2d Java game issue

This seems like an easy-fix however I just can't seem to get it, xMove represent moving on the x-axis, y for the y-axis. 这似乎很容易解决,但是我似乎无法理解,xMove代表在x轴上移动,y代表y轴。 Currently while my player object is colliding downwards with a tile (standing on the ground) the sprite will be facing right. 目前,当我的播放器对象与瓷砖(位于地面上)向下碰撞时,子画面将朝右。 I want my player to face left if I have been moving left just before that. 如果我在此之前一直向左移动,我希望我的玩家面对左。 So basically I need a way to remember what way my character is facing and return that to the yMove == 0 part of my code. 因此,基本上我需要一种方法来记住角色面对的方式,并将其返回给代码的yMove == 0部分。 Could anyone give me any advice? 谁能给我任何建议?

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

Edit: I tried messing around with booleans to return different sprites eg if(facing Left){return Assets.playerFacingLeft} however doing this somehow doesn't return an image at all. 编辑:我试着用布尔值弄乱返回不同的精灵,例如if(面对左){return Assets.playerFacingLeft},但是以某种方式这样做根本不会返回图像。

Assuming that you considered x and y axis like this :- 假设您这样考虑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;

You just need to rearrange your code: Handle the y-axis movement first. 您只需要重新排列代码:首先处理y轴移动。 If there's no vertical movement, then check horizontal movement. 如果没有垂直运动,则检查水平运动。 I added the final if(facingLeft) statement to handle the situation when the player is neither falling nor standing still: 我添加了最后的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();
    }
}

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

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