XNA将精灵捕捉到平铺贴图

时间:2011-02-01 16:24:48

标签: xna 2d tiles

我正在寻找关于如何处理拼贴画到tilemap的最佳选择。我正在尝试制作一个楚楚火箭克隆。如果你不懂游戏。这是一个基于图标的游戏,你可以在一个特定的瓷砖上放置箭头来指导地图周围的unts。因此,我需要始终将精灵拍打到瓷砖的中心,然后用箭头检测碰撞,该箭头占据整块瓷砖或墙壁或其他障碍物。关于检测这些东西的基本方法的任何想法,因为它需要不同类型的碰撞检测我相信。

1 个答案:

答案 0 :(得分:0)

将精灵捕捉到拼贴的最简单方法是将它们绘制在拼贴中心。通过检查您的关卡对象,可以在更新功能中完成碰撞检测。

class Mouse
{
  public int XTile;
  public int YTile;
  public int XDelta;
  public int YDelta;
} 

//in Update
if (Level[mouse.YTile][mouse.XTile] == Tiles.Arrow)
{
  //change mouse.XDelta and mouse.YDelta based on the direction of the arrow
}
if (Level[mouse.YTile + YDelta][mouse.XTile + XDelta] == Tiles.Wall)
{
  //change mouse.XDelta and mouse.YDelta based on wall rules
}

//in Draw
int tileSize = 32; //or whatever size tile you are using
spriteBatch.Draw(mouseSprite, new Vector2(mouse.XTile * tileSize, 
    mouse.YTile * tileSize), Color.White);

//or, if the mouseSprite doesn't take up the whole tile
int sizeDifference = tileSize - mouseSprite.Width;
spriteBatch.Draw(mouseSprite, new Vector2(mouse.XTile * tileSize + sizeDifference / 2f, 
    mouse.YTile * tileSize + sizeDifference / 2f), Color.White);