0

我目前正在开发一个相对简单的平台游戏,它有一个奇怪的错误。游戏开始时你会掉到地上(你会在离地面几格的地方生成),但是当你落地时,你的脚会卡在世界里面,直到你跳起来才能移动。这就是我的意思:

http://i.imgur.com/IKLZY.png

玩家的脚在地面以下几个像素。然而,这个问题只出现在整个地图的 3 个地方,并且只出现在这 3 个选定的地方。我假设问题出在我的碰撞检测代码中,但我不完全确定,因为当它发生时我没有收到错误。

public boolean isCollidingWithBlock(Point pt1, Point pt2) { 
//Checks x  
    for(int x = (int) (this.x / Tile.tileSize); x < (int) (this.x / Tile.tileSize + 4); x++) {
//Checks y
        for(int y = (int) (this.y / Tile.tileSize); y < (int) (this.y / Tile.tileSize + 4); y++) {
            if(x >= 0 && y >= 0 && x < Component.dungeon.block.length && y < Component.dungeon.block[0].length) {
//If the block is not air
                if(Component.dungeon.block[x][y].id != Tile.air) {
                    //If the player is in contact with point one or two on the block 
                    if(Component.dungeon.block[x][y].contains(pt1) || Component.dungeon.block[x][y].contains(pt2)) {
//Checks for specific blocks 
                        if(Component.dungeon.block[x][y].id == Tile.portalBlock) {
                            Component.isLevelDone = true;
                        } 
                        if(Component.dungeon.block[x][y].id == Tile.spike) {
                            Health.health -= 1;
                            Component.isJumping = true;

                            if(Health.health == 0) {
                                Component.isDead = true;
                            }
                        }
                        return true;
                    }
                } 
            }
        }
    }

    return false;
}

我要问的是我将如何解决这个问题。我已经查看了我的代码很长一段时间,但我不确定它有什么问题。另外,如果有更有效的方法来进行碰撞检查,请告诉我!

我希望这是足够的信息,如果它不只是告诉我你需要什么,我一定会添加它。

谢谢!

4

1 回答 1

0

问题可能不是您的碰撞检查,而是您在碰撞时要做什么的逻辑。你的角色掉进了方块,一旦进去,它总是会与方块发生碰撞。所以它不能跳跃(因为你在跳跃时检查碰撞我猜)。当您检查碰撞时,您必须通过预先检查和调整来确保您的角色不会落入块中。

if (will collide) {
    put beside block
}

你可能正在做类似的事情

if (colliding) {
    stop moving
}

但是,当放在旁边时,您必须检查您正在移动的方式以及您没有移动到块中。

于 2012-10-12T18:47:22.333 回答