【发布时间】:2015-02-04 10:18:21
【问题描述】:
我正在制作一个 2d 横向卷轴游戏,我正在尝试使用 2 维或所谓的瓷砖集。我的朋友给了我一些他一直用于他的游戏的代码,但他的代码只支持一维。
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 像素 i思考)。我几乎不明白我朋友给我的代码,这可能就是问题所在。如果有人知道如何使绘制方法支持 2 维,以便我可以使用图块集,那么尺寸(例如 512x512)而不是 64x4992(我当前的图块集所在)。
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 中编辑我的地图,我意识到 tiled 计数为 1-2-3-4-5,而不是 0-1-2-3-4-5。因此,如果您查看我的图块集(在绘图方法 imgur 链接中注释掉),您会看到我在第一个图块之前有一个空白区域,但这意味着每个图块在 xna 中的位置值 + 1,这可能就是为什么我有绘制它时出现问题,因为我尝试绘制的图块在图块集中不存在。
【问题讨论】:
-
首先我不好切换 x 和 y 顺序,使用 map[y,x] 而不是 map[x, y] 以避免 OutOfRangeException,其次我没有看到任何图像链接!