如何修复卡在碰撞中的物体

时间:2020-10-20 05:19:29

标签: java collision-detection

在我正在制作的Java游戏中,我有一个玩家和一个墙砖。当玩家向前移动时,它会检查是否与以下代码中的墙壁碰撞:

player.moveForward2(playerSpeed); //moves player forward
            
for (int i = 1; i <=100; i++) {
    for (int j = 1; j <=50; j++) {
        map1.map[(int) (i-1)][(int) (j-1)].getRect(); //gets rectangle from object on screen
        player.getRect();
        
        if (player.r.intersects(map1.map[(int) (i-1)][(int) (j-1)].r)) { //checks if player is intersecting rectangle after moving
            if (map1.map[(int) (i-1)][(int) (j-1)].id == 3) { //checks that player isn't able to pass through this tile
                player.moveBackward2(playerSpeed); // moves back if space cannot be moved into
            }
        }
        
    }
}

问题是当它移入矩形时,它会卡住并且不能向任何方向移动。该方法旨在允许其移动到矩形中并向矩形滑动而不会卡在矩形上。播放器位于屏幕中央,如果有任何其他作用,则是其他图块在移动。

0 个答案:

没有答案
相关问题