我目前正在开发一个相对简单的平台游戏,它有一个奇怪的错误。游戏开始时你会掉到地上(你会在离地面几格的地方生成),但是当你落地时,你的脚会卡在世界里面,直到你跳起来才能移动。这就是我的意思:
玩家的脚在地面以下几个像素。然而,这个问题只出现在整个地图的 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;
}
我要问的是我将如何解决这个问题。我已经查看了我的代码很长一段时间,但我不确定它有什么问题。另外,如果有更有效的方法来进行碰撞检查,请告诉我!
我希望这是足够的信息,如果它不只是告诉我你需要什么,我一定会添加它。
谢谢!