XNA 瓦片地图瓦片集
XNA tilemap tileset
我正在制作 2d 横向卷轴游戏,我正在尝试使用 2dimensional 或所谓的 tileset。我的朋友给了我一些他在游戏中使用的代码,但他的代码只支持一维。
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。因此,如果您查看我的图块集(在绘制方法 imgur link 中注释掉),您会看到我在第一个图块之前有一个空白 space,但这意味着每个图块都有其位置值 + 1 xna,这可能就是我在绘制它时遇到问题的原因,因为我尝试绘制的图块在图块集中不存在。
因为我不知道你的 Tile Map 到底是什么样子,所以我将简单地给出一个逐步解释的例子:
首先 假设我们有以下 Tile Map Sheet
这张地图是 8x3,假设每个图块是 64X64 像素,要引用其中一个图块,您可以在地图中使用它的坐标 (x,y)Sheet(例如最后一个图块有基于坐标 (7,2) -0 的索引 -),或者更巧妙的是,您可以通过从第一个图块开始计数来简单地将其称为一个数字(例如,前一个图块 (7,2) 代表图块 23)
其次定义你的地图矩阵
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中tile的坐标,
第三部绘图部分,
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),
代表了所谓的SourceRectangle
,即Rect用来定位TileMap
[=18=中对应的tileRectangle
]
我正在制作 2d 横向卷轴游戏,我正在尝试使用 2dimensional 或所谓的 tileset。我的朋友给了我一些他在游戏中使用的代码,但他的代码只支持一维。
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。因此,如果您查看我的图块集(在绘制方法 imgur link 中注释掉),您会看到我在第一个图块之前有一个空白 space,但这意味着每个图块都有其位置值 + 1 xna,这可能就是我在绘制它时遇到问题的原因,因为我尝试绘制的图块在图块集中不存在。
因为我不知道你的 Tile Map 到底是什么样子,所以我将简单地给出一个逐步解释的例子:
首先 假设我们有以下 Tile Map Sheet
这张地图是 8x3,假设每个图块是 64X64 像素,要引用其中一个图块,您可以在地图中使用它的坐标 (x,y)Sheet(例如最后一个图块有基于坐标 (7,2) -0 的索引 -),或者更巧妙的是,您可以通过从第一个图块开始计数来简单地将其称为一个数字(例如,前一个图块 (7,2) 代表图块 23)
其次定义你的地图矩阵
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中tile的坐标,
第三部绘图部分,
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),
代表了所谓的SourceRectangle
,即Rect用来定位TileMap
[=18=中对应的tileRectangle
]