0
我终于通过使用2个矩形之间的intersects()来实现碰撞检测,并且它似乎正在工作。然而,玩家只是卡在矩形中,无法移动。所以我现在试图在玩家移动之前检查碰撞。移动之前的Java碰撞检测
这是我曾尝试:
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();
}
}
我只是创建一个新的矩形,但在它是,如果玩家移动,并检查是否碰撞。如果有,请勿移动。
问题是,当玩家进入矩形时,它会减速。它仍然穿过墙壁,但由于某种原因以较慢的速度移动。
是什么导致了这个问题?
啊,谢谢你,我现在看到我的错误。 – joey942