Java 做一个精灵移动,需要精灵停留在他上次移动的位置
我正在制作一个java游戏,当用户按下一些键时,精灵会朝那个方向移动,它会改变精灵以匹配用户输入的方向。 如果您想观看当前游戏,请访问以下网站: 以下是我正在使用的代码:Java 做一个精灵移动,需要精灵停留在他上次移动的位置,java,applet,Java,Applet,我正在制作一个java游戏,当用户按下一些键时,精灵会朝那个方向移动,它会改变精灵以匹配用户输入的方向。 如果您想观看当前游戏,请访问以下网站: 以下是我正在使用的代码: int x_posI = (int) x_pos; int y_posI = (int) y_pos; if (downPressed && leftPressed) { g.drawImage(hero225, x_posI, y_posI, this);
int x_posI = (int) x_pos;
int y_posI = (int) y_pos;
if (downPressed && leftPressed) {
g.drawImage(hero225, x_posI, y_posI, this);
spr270 = false;
} else if (downPressed && rightPressed) {
spr270 = false;
g.drawImage(hero135, x_posI, y_posI, this);
} else if (upPressed && rightPressed) {
spr270 = false;
g.drawImage(hero45, x_posI, y_posI, this);
} else if (upPressed && leftPressed) {
g.drawImage(hero315, x_posI, y_posI, this);
spr270 = false;
} else if (leftPressed == true) {
g.drawImage(hero270, x_posI, y_posI, this);
spr270 = true;
} else if (rightPressed == true) {
g.drawImage(hero90, x_posI, y_posI, this);
spr270 = false;
} else if (upPressed == true) {
g.drawImage(hero, x_posI, y_posI, this);
spr270 = false;
} else if (downPressed == true) {
g.drawImage(hero180, x_posI, y_posI, this);
spr270 = false;
}
else{
g.drawImage(hero, x_posI, y_posI, this);
}
if(spr270) {
g.drawImage(hero270, x_posI, y_posI, this);
}
当我按左键时,会发生以下情况:
i、 stack.imgur[dot]com/owT3z.png
当我放手时,我会这样做:
i、 stack.imgur.com/2Wrjr[dot]png
如何使角色保持左向?这是内部绘制(图形g)方法,对吗
将volatile Image字段sprite添加到类(“受保护的volatile Image sprite;”)。
将逻辑更改为:
int x_posI = (int) x_pos;
int y_posI = (int) y_pos;
if (downPressed && leftPressed) {
this.sprite = hero225;
} else if (downPressed && rightPressed) {
this.sprite = hero135;
} else if (upPressed && rightPressed) {
this.sprite = hero45;
} else if (upPressed && leftPressed) {
this.sprite = hero315;
} else if (leftPressed == true) {
this.sprite = hero270;
} else if (rightPressed == true) {
this.sprite = hero90;
} else if (upPressed == true) {
this.sprite = hero;
} else if (downPressed == true) {
this.sprite = hero180;
}
// this.sprite will contain value set on last "movement"
g.drawImage(this.sprite, x_posI, y_posI, this);
与您的问题无关但与可读性相关:为什么有
if(foo==true){
?为什么不if(foo){
?我没有想到这一点,谢谢。至于你目前的问题,我个人对你的问题是什么感到困惑。你可能想在你的原始帖子上写一两段描述,描述你的问题的细节,以及当你试图解决它时,什么不起作用。如果你在游戏中玩游戏,你可以看到问题是什么@迪扎:我不同意,但我想我最想说的是你应该努力向我们解释。是你要求免费的建议,不是吗?太棒了。谢谢:)