【发布时间】:2013-06-09 08:22:35
【问题描述】:
我需要帮助检查我正在创建的游戏中的交叉点:当角色与屏幕上的单个障碍物发生碰撞时,一切都会按预期进行,但当屏幕上添加多个障碍物时,角色只能跳下正在检查碰撞的最后一个障碍物。角色仍然可以正确地与其他障碍物发生碰撞(不会跌落,无法通过),但无法跳下它们。 Obs 是障碍物的数组列表。 Ground 是确定是否允许角色跳跃的布尔值。
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);
}
if(quote.isVisible()){
quote.movedrawProtag(win, keys);
quote.checkIntersect(obs);
}
【问题讨论】:
-
欢迎堆栈溢出。请您编辑您的帖子以澄清您正在寻求帮助的问题。您希望您的代码会发生什么以及正在发生什么?如有必要,您可能会考虑为您担心的案例添加一些示例数据。祝你好运!
-
1) 您已经描述了一个问题以及如何解决问题,但到目前为止还没有提出任何问题(更不用说具体的、可回答的问题了)。您的问题是什么? 2)查看Shape based collision detection。 3) 为了尽快获得更好的帮助,请发帖SSCCE。
标签: java game-physics