java - 我的碰撞检测器让我的 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;
}
}
我希望玩家实际上会与墙壁发生碰撞并停在墙壁上,而不是穿过墙壁。
解决方案
推荐阅读
- php - wp_delete_post 仅适用于管理员?
- c# - 是否有可能通过 Wix C# 中的单个安装程序同时使用 32 位和 64 位 MS Office 插件?
- python - For 循环不拾取字符串中的特殊字符
- python - TFlite:`ValueError:'118' 的 Mmap 失败`
- android - 如何使键盘仅出现在 EditText 下方
- version-control - Rundeck 项目 SCM exportUuidBehavior“原始”覆盖我的工作 UUID
- mongodb - 来自多个 mongo 实例的 MongoDB 导出器指标
- linux-kernel - 每个进程并行运行时是否有自己的虚拟地址空间?
- python - 拒绝只包含空格的字符串
- php - 允许每个用户单击一次元素