【发布时间】:2011-12-04 04:38:23
【问题描述】:
我正在尝试用 C# 绘制平铺地图,而我遇到的问题在我看来很奇怪。
我有这个 int 数组,它应该保存 x 坐标和 y 坐标以在屏幕上绘制图块。 (不只有0的箭头是X,另一个是Y)
int[,] level1 = { { 0, 32, 64, 96 }, { 0, 0, 0, 0 } };
这是我如何使用 for 循环将图块的一部分渲染到屏幕上的方法,在这里我将在一行上得到一个“OutOfMemoryException”,我将对其进行注释:
public void DrawTest(SpriteBatch spriteBatch)
{
for (int x = 0;; x++)
{
for (int y = 0;; y++)
{
x = level1[x, 0];
y = level1[0, y];
//This line bellow is where it says OutOfMemoryException
spriteBatch.Draw(tileSheet, new Rectangle(x, y, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);
if (x >= 5 | y >= 5)
{
x = 0;
y = 0;
}
}
}
}
当我想调用这个渲染方法时,我会在主类渲染方法中执行它
levelLoader.DrawTest(this.spriteBatch);
在我使用此 DrawTest 方法尝试绘制瓷砖之前,它运行良好。但我完全不知道为什么这不能正常工作。
更新:
public void DrawTest(SpriteBatch spriteBatch)
{
for (int x = 0; x < 5 ; x++)
{
for (int y = 0; y < 5 ; y++)
{
x = level1[x, 0];
y = level1[0, y];
spriteBatch.Draw(tileSheet, new Rectangle(x, y, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);
}
}
}
更新 2:
public void DrawTest(SpriteBatch spriteBatch)
{
for (int x = 0; x < 5 ; x++)
{
for (int y = 0; y < 5 ; y++)
{
int tileXCord = level1[x, 0];
int tileYCord = level1[0, y];
spriteBatch.Draw(tileSheet, new Rectangle(tileXCord, tileYCord, 32, 32), new Rectangle(0, 0, 32, 32), Color.White);
}
}
}
【问题讨论】:
-
您可能尝试批量处理无限数量的绘图调用。你想用这个奇怪的循环做什么?
-
我正在尝试创建一个类来绘制我正在制作的游戏地图..
标签: c# xna out-of-memory