gpt4 book ai didi

java - 简单的 2d Java 游戏问题

转载 作者:行者123 更新时间:2023-12-02 03:55:39 25 4
gpt4 key购买 nike

这看起来很容易修复,但我似乎无法理解它,xMove 表示在 x 轴上移动,y 表示 y 轴上移动。目前,当我的玩家对象与瓷砖(站在地面上)向下碰撞时, Sprite 将面向右侧。如果我在此之前一直向左移动,我希望我的球员面朝左侧。所以基本上我需要一种方法来记住我的角色面对的方向并将其返回到我的代码的 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;
}

编辑:我尝试用 boolean 值来返回不同的 Sprite ,例如if(faceing Left){return Assets.playerFacingLeft} 但是这样做根本不会返回图像。

最佳答案

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

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

关于java - 简单的 2d Java 游戏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35494111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com