首页 > 解决方案 > 我的碰撞检测器让我的 Sprite 夹穿过一些墙壁并与其他墙壁发生碰撞,我不知道为什么

问题描述

我正在 LIGBDX 中制作 Zelda/Pokemon-esque 游戏,我正在尝试找出碰撞检测。我正在使用平铺地图并有一个用于碰撞的墙层。每当玩家试图移动时,我都会使用一个 for 循环来检查他们是否会与墙壁发生碰撞。出于某种原因,玩家可能会完美地停在某些墙壁上,但可能会穿过其他墙壁。

球员运动

public void MoveLeft() {


        for (float i = 0; i < movespeed; i++) {

            if (!M.iswallat(getPosition().getX() - 1, getPosition().getY()) ) {
                getPosition().moveLeft(1, M);
                UpdatePos();
            } else {
                break;
            }
        }
    }

每种运动类型实际上有 4 种方法(左、右、上和下)。它们在功能上都是相同的,唯一的区别是正在修改什么变量。getPosition 是存储位置数据的位置。UpdatePos 移动 Sprite 并更新相机。每当布尔值 IsMovingLeft 处于活动状态时,MoveLeft 本身就会由 Timer 调用。这个布尔值由 InputProcessor 中的 KeyDown 和 KeyUp 方法打开和关闭。

是墙在

public boolean iswallat(float x, float y) {

        int xr =  (int) (x / collisionLayer.getTileWidth());
        int yr =  (int) ( y / collisionLayer.getTileHeight());
        if (collisionLayer.getCell(xr, yr) != null) {

            System.out.println("Cell found at : "+ xr + "," + yr);


            if (collisionLayer.getCell(xr, yr).getTile().getProperties().containsKey("blocked")) {


                return true;
            }

            else {
                System.out.println("Found but not blocked!");
                return false;
            }

        }

        else {

            return false;
        }
    }

我希望玩家实际上会与墙壁发生碰撞并停在墙壁上,而不是穿过墙壁。

标签: javalibgdxcollision

解决方案


推荐阅读