我正在制作自上而下的RPG。我几乎已经完成了移动系统,但是我似乎无法弄清楚如何检测玩家是否有瓷砖挡住了玩家。我的tmx文件中有两个对象层,播放器应在任何地图上与之交互:NPC层和碰撞层。我尝试过NPC是图块对象(以RectangleMapObjects的形式出现),通常高1.5个图块,而碰撞对象是多边形,我也有一些图块,我只想从一侧访问,所以我需要知道是否碰撞中取决于方向的三个独立点。几个小时后,这就是我所拥有的:
public boolean canWalk(float xc, float yc, String dir)
{
int i = 0;
while (true)
{
try
{
RectangleMapObject temp = (RectangleMapObject) ents.get(i);
if (!temp.equals(player))
{
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
return !(temp.getRectangle().contains(cx + 6, cy + 8));
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx + 6, cy) && temp.getRectangle().contains(cx + 12, cy));
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12));
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
return !(temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12));
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
versionid = "" + i + ",";
i = 0;
while (true)
{
try
{
PolygonMapObject temp = (PolygonMapObject) cols.get(i);
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy));
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy));
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12));
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
return !(temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12));
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
versionid = versionid + i;
return true;
}
它总是返回true
解决了我自己的愚蠢错误,每张支票都有保证的退货,而不是仅在必要时退货
答案 0 :(得分:0)
我发现这里是任何人想要的功能代码
public boolean canWalk(String dir)
{
int i = 0;
while (true)
{
try
{
RectangleMapObject temp = (RectangleMapObject) ents.get(i);
if (!temp.equals(player))
{
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
if (temp.getRectangle().contains(cx + 6, cy + 8))
{
return false;
}
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx + 6, cy) && temp.getRectangle().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12))
{
return false;
}
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
if (temp.getRectangle().contains(cx, cy) && temp.getRectangle().contains(cx, cy + 6) && temp.getRectangle().contains(cx, cy + 12))
{
return false;
}
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
i = 0;
while (true)
{
try
{
PolygonMapObject temp = (PolygonMapObject) cols.get(i);
if (dir.equals("s"))
{
float cy = ((y) * 16) - 2;
float cx = ((x) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("n"))
{
float cy = ((y + 1) * 16) + 2;
float cx = ((x) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx + 6, cy) && temp.getPolygon().contains(cx + 12, cy))
{
return false;
}
}
else if (dir.equals("e"))
{
float cy = ((y) * 16) + 2;
float cx = ((x + 1) * 16) + 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12))
{
return false;
}
}
else if (dir.equals("w"))
{
float cy = ((y) * 16) + 2;
float cx = ((x) * 16) - 2;
if (temp.getPolygon().contains(cx, cy) && temp.getPolygon().contains(cx, cy + 6) && temp.getPolygon().contains(cx, cy + 12))
{
return false;
}
}
}
catch (IndexOutOfBoundsException e)
{
break;
}
i++;
}
return true;
}
请注意,我回答了此问题以将其关闭