【发布时间】:2016-12-05 01:29:28
【问题描述】:
我终于通过在 2 个矩形之间使用 intersects() 来进行碰撞检测,它似乎正在工作。然而,玩家只是卡在矩形中而无法移动。所以我现在试图在玩家移动之前检查碰撞。
这是我尝试过的:
if(up == true){
Rectangle futurerect = new Rectangle(px,py-=5,81,150);
if(!futurerect.intersects(wallexample)){
py-=5;
repaint();
}
}
if(down == true){
Rectangle futurerect = new Rectangle(px,py+=5,81,150);
if(!futurerect.intersects(wallexample)){
py+=5;
repaint();
}
}
if(left == true){
Rectangle futurerect = new Rectangle(px-=5,py,81,150);
if(!futurerect.intersects(wallexample)){
px-=5;
repaint();
}
}
if(right == true){
Rectangle futurerect = new Rectangle(px+=5,py,81,150);
if(!futurerect.intersects(wallexample)){
px+=5;
repaint();
}
}
我只是创建了一个新的矩形,但如果玩家移动了它会在哪里,并检查它是否发生碰撞。如果是,请不要移动。
问题是,当玩家移动到矩形中时,它只会放慢速度。它仍然穿过墙壁,但出于某种原因只是以较慢的速度移动。
是什么导致了这个问题?
【问题讨论】:
标签: java collision-detection paint