Java游戏:检查十字路口和跳跃
我需要在我正在创建的游戏中检查交叉点的帮助:当一个角色与屏幕上的单个障碍物碰撞时,所有操作都按预期进行,但是当多个障碍物添加到屏幕上时,角色只能跳出正在检查碰撞的最后一个障碍物。角色仍然会与其他障碍物发生适当碰撞(无法穿过、无法穿过),但无法跳出障碍物。Obs是一个障碍列表。Ground是确定是否允许角色跳转的布尔值Java游戏:检查十字路口和跳跃,java,game-physics,Java,Game Physics,我需要在我正在创建的游戏中检查交叉点的帮助:当一个角色与屏幕上的单个障碍物碰撞时,所有操作都按预期进行,但是当多个障碍物添加到屏幕上时,角色只能跳出正在检查碰撞的最后一个障碍物。角色仍然会与其他障碍物发生适当碰撞(无法穿过、无法穿过),但无法跳出障碍物。Obs是一个障碍列表。Ground是确定是否允许角色跳转的布尔值 public void checkIntersect(ArrayList<Obstacle> obs){ for(Obstacle a: obs){
public void checkIntersect(ArrayList<Obstacle> obs){
for(Obstacle a: obs){
if(a.getLeft().intersects(this.getRight())){
if(getDx()>0){
sidecollision=true;
setDx(0);
this.x-=1.5f;
}
} if (a.getRight().intersects(this.getLeft())){
sidecollision = true;
setDx(0);
this.x+=1.5f;
} if((a.getTop()).intersects(this.getBottom())){
ground=true;
setDy(0);
this.y-=.10f;
} else if(!(a.getTop()).intersects(this.getBottom())){
ground = false;
//return ground;
} if(a.getBottom().intersects(this.getTop())){
ground=false;
setDy(0);
this.y+=.1f;
}
}
}
导致错误的最可能原因是后续调用
checkIntersect()
覆盖ground
的值。这导致ground
的值仅反映上次调用checkIntersect()
。要修复此错误,在调用checkIntersect()
之前,应将ground
设置为默认值true
或false
。然后,修改checkIntersect()
方法,使其只能将ground
设置为非默认值。当ground
应为其默认值时,无需进行设置
像这样做。我使用的默认值是false
public void checkIntersect(ArrayList<Obstacle> obs){
for(Obstacle a: obs){
if(a.getLeft().intersects(this.getRight())){
if(getDx()>0){
sidecollision=true;
setDx(0);
this.x-=1.5f;
}
} if (a.getRight().intersects(this.getLeft())){
sidecollision = true;
setDx(0);
this.x+=1.5f;
} if((a.getTop()).intersects(this.getBottom())){
ground=true;
setDy(0);
this.y-=.10f;
} else if(!(a.getTop()).intersects(this.getBottom())){
// ground = false;
//return ground;
} if(a.getBottom().intersects(this.getTop())){
// ground=false;
// setDy(0);
// this.y+=.1f;
}
}
}
欢迎来到堆栈溢出。请编辑您的帖子,澄清您寻求帮助的问题。您希望您的代码会发生什么,以及正在发生什么?如果有必要,您可能会考虑添加一些示例数据来解决令您担忧的情况。祝你好运1) 你已经描述了一个问题以及你怎么做不到,但到目前为止还没有提出一个问题(更不用说一个具体的、可回答的问题了)。你的问题是什么?2) 看。3) 为了更快地获得更好的帮助,请发布一个。
public void checkIntersect(ArrayList<Obstacle> obs){
for(Obstacle a: obs){
if(a.getLeft().intersects(this.getRight())){
if(getDx()>0){
sidecollision=true;
setDx(0);
this.x-=1.5f;
}
} if (a.getRight().intersects(this.getLeft())){
sidecollision = true;
setDx(0);
this.x+=1.5f;
} if((a.getTop()).intersects(this.getBottom())){
ground=true;
setDy(0);
this.y-=.10f;
} else if(!(a.getTop()).intersects(this.getBottom())){
// ground = false;
//return ground;
} if(a.getBottom().intersects(this.getTop())){
// ground=false;
// setDy(0);
// this.y+=.1f;
}
}
}
bg.update(quote);
win.fill(clear);
bg.drawBG(win);
for(Obstacle o: obs){
o.draw(win);
o.update(bg);
}
// Set ground to its default value
quote.ground = false;
if(quote.isVisible()){
quote.movedrawProtag(win, keys);
quote.checkIntersect(obs);
}
// If ground has not been set to true, do the default action
if (quote.ground == false) {
setDy(0);
quote.y += .1f;
}