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