XNA tilemap tileset

时间:2015-02-04 10:18:22

标签: c# xna tile

我正在制作一个二维侧面游戏并且我试图使用一个二维或所谓的拼图。我的朋友给了我一些他用于游戏的代码,但他的代码只支持1维。

   public void DrawMap(Texture2D tilesheet, SpriteBatch spriteBatch)
    {
        for (int x = 0; x < map.GetLength(1); x++)
        {
            for (int y = 0; y < map.GetLength(0); y++)
            {
                spriteBatch.Draw(tilesheet, new Vector2((x * 64) , (y * 64)), new Rectangle(map[y, x] * 64, 0, 64, 64), Color.White);
            }
        }
    }

这就是出现问题的地方,因为它是一个小组项目,我们收到图形设计师的图形,我们有太多的图块(尺寸为64x64),以适合xna支持的一行纹理(我认为4096像素)。我几乎不理解我的朋友给我的代码,这可能是问题的来源。如果有人知道如何使绘制方法支持2维,以便我可以使用tilesets,则尺寸(例如512x512)而不是64x4992(我当前的tileset所在)。

    public Level()
    {
        map = new int[14, 50]
        {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,18,19,20,21,22,23,24,17,18,19,20,21,22,23,24,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,26,27,28,29,30,31,32,25,26,27,28,29,30,31,32,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,39,40,33,34,35,36,37,38,39,40,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,42,43,44,45,46,47,48,41,42,43,44,45,46,47,48,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,5,0,0,0,0,49,50,51,52,53,54,55,56,49,50,51,52,53,54,55,56,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,58,59,60,61,62,63,64,57,58,59,60,61,62,63,64,2},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,11,0,0,0,0,0,0,0,0,0,65,66,67,68,69,70,71,72,65,66,67,68,69,70,71,72,2},
        {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,6,7,2,12,13,3,3,3,3,3,3,3,3,3,73,74,75,76,77,78,79,80,73,74,75,76,77,78,79,80,2},
        {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}};
        overlay = new int[14, 50];
        background = new int[14,50];
    }
    public void DrawMap(Texture2D tilesheet, SpriteBatch spriteBatch)
    {
        for (int x = 0; x < map.GetLength(1); x++)
        {
            for (int y = 0; y < map.GetLength(0); y++)
            {
                //http://imgur.com/1r9VUUN
                spriteBatch.Draw(tilesheet, new Vector2((x * 64), (y * 64)), new Rectangle((map[x, y] % 8) * 64, (map[x, y] / 8) * 64, 64, 64), Color.White);
            }
        }
    }

所以我在Tiled编辑我的地图,我意识到平铺计数1-2-3-4-5,而不是0-1-2-3-4-5。因此,如果您查看我的tileset(在draw方法imgur链接中注释掉),您会看到我在第一个tile之前有一个空格,但这意味着每个tile在xna中的positionvalue + 1,这可能就是为什么我有绘制它的问题,因为我试图绘制的图块不存在于图块集中。

1 个答案:

答案 0 :(得分:2)

由于我不确切知道你的瓷砖地图是怎么样的,所以我会简单地逐步解释一下这个例子:

首先让我们说我们有以下的瓷砖地图表

enter image description here

这个地图是8x3并且假设每个图块是64X64 px,要参考其中一个图块你可以在MapSheet中使用它的坐标(x,y)(例如最后一个图块有坐标(7,2)基于-0的索引 - ),或者更巧妙的是你可以简单地通过从第一个图块计数将其称为一个数字(例如,前一个图块(7,2)代表图块23)

enter image description here

第二次定义您的地图矩阵

 map = new int[,]
 {                            
     {8,8,1,8,8,1},
     {1,8,0,8,8,8},
     {1,8,8,8,8,8},
     {1,7,8,8,8,8},
     {1,8,0,23,4,4},
     {1,8,8,1,8,8}
 };

上面Matrix中的每个元素都是TileMap中图块的坐标,

第三绘图部分,

public void DrawMap(Texture2D tilesheet, SpriteBatch spriteBatch)
{
    for (int x = 0; x < map.GetLength(1); x++)
    {
        for (int y = 0; y < map.GetLength(0); y++)
        {
            spriteBatch.Draw(tilesheet, new Vector2((x * 64) , (y * 64)), new Rectangle((map[x,y]%8) * 64, (map[x,y]/8)*64, 64, 64), Color.White);
        }
    }
}
上面的代码new Rectangle((map[x,y]%8) * 64, (map[x,y]/8)*64, 64, 64),中的

表示所谓的来源Rectangle,该Rect用于在Rectangle

中找到相应的图块TileMap