【发布时间】:2016-08-31 20:15:49
【问题描述】:
大家好,我最近开始制作自己的平台游戏,我已经成功地使用 2d 数组创建了地图,并且还创建了具有运动的玩家。 我现在陷入了如何处理矩形碰撞侧的问题,非常感谢任何帮助。
这是我创建地图的方法,现在我只需要弄清楚如何处理碰撞。
List<Texture2D> tileTextures = new List<Texture2D>();
private const int tileWidth = 64;
private const int tileHeight = 64;
public void Draw(SpriteBatch spriteBatch, Camera camera)
{
int tileMapWidth = tileMap.GetLength(1);
int tileMapHeight = tileMap.GetLength(0);
for (int x = 0; x < tileMapWidth; x++)
{
for (int y = 0; y < tileMapHeight; y++)
{
int textureIndex = tileMap[y, x];
Texture2D texture = tileTextures[textureIndex];
spriteBatch.Draw(
texture,
new Rectangle(x * tileWidth - (int)camera.cameraPosition.X,
y * tileHeight - (int)camera.cameraPosition.Y,
tileWidth,
tileHeight),
Color.White);
}
}
}
【问题讨论】:
-
我记得当我开始使用 XNA 时,你有一个带有 intersects 方法的矩形类,你可以用它来确定一个对象是否与另一个对象发生碰撞
-
你知道我会如何比较我的播放器矩形和我的图块吗?当我每次在平铺绘制函数中设置一个新矩形时,但我不知道如何实际调用它来检查它是否相交。
-
如果你在做 XNA,你可能应该有一个
update, 方法,你可以遍历你所有的玩家和瓷砖,创建一个新的矩形,并检查那里的碰撞 -
所以我为我的播放器创建了一个函数,如下所示: public Rectangle BoundingBox { get { return new Rectangle( (int)position.X, (int)position.Y, spriteWidth, spriteHeight);但我仍然不确定如何从上面发布的代码中检索平铺矩形,以便我可以使用 intersects 函数,您有什么建议吗?