C# 尝试绘制平铺贴图时发生OutOfMemoryException

C# 尝试绘制平铺贴图时发生OutOfMemoryException,c#,xna,out-of-memory,C#,Xna,Out Of Memory,我正试图用C#绘制一个平铺图,我认为我遇到的问题很奇怪 我有一个int数组,用来保持x坐标和y坐标,在屏幕上绘制瓷砖。(其中不只有0的箭头是X,另一个是Y) 下面是我如何使用for循环将一部分磁贴渲染到屏幕中的,在这里,我在一行上得到一个“OutOfMemoryException”,我将注释掉: public void DrawTest(SpriteBatch spriteBatch) { for (int x = 0;; x++) {

我正试图用C#绘制一个平铺图,我认为我遇到的问题很奇怪

我有一个int数组,用来保持x坐标和y坐标,在屏幕上绘制瓷砖。(其中不只有0的箭头是X,另一个是Y)

下面是我如何使用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;
                }
            }
        }
    }
当我想调用这个render方法时,我会在主类render方法中调用它

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);
                }
            }
        }
        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);
            }
        }
    }
public void DrawTest(SpriteBatch-SpriteBatch)
{
对于(int x=0;x<5;x++)
{
对于(int y=0;y<5;y++)
{
x=level1[x,0];
y=level1[0,y];
绘制新矩形(x,y,32,32),新矩形(0,0,32,32),颜色为白色;
}
}
}
更新2:

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);
                }
            }
        }
        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);
            }
        }
    }
public void DrawTest(SpriteBatch-SpriteBatch)
{
对于(int x=0;x<5;x++)
{
对于(int y=0;y<5;y++)
{
int tileXCord=level1[x,0];
int tileYCord=level1[0,y];
spriteBatch.Draw(tileSheet,新矩形(tileXCord,tileYCord,32,32),新矩形(0,0,32,32),Color.White);
}
}
}

我想你陷入了一个无限循环。您需要以某种方式退出以避免出现内存不足异常。

我发现您的代码中有几个问题:

  • 代码中有一个无限循环。嵌套循环的终止条件是什么
  • (重要!)
    spriteBatch.Draw()
    method不绘制任何东西,它只是安排精灵的绘制。此方法调用之前应先调用
    spriteBatch.Begin()
    (以开始计划绘图),最后必须调用
    spriteBatch.End()
    以将计划的Spit刷新到设备中。无限循环会导致精灵图形的无限调度,直到内存已满且面临内存不足异常
  • (注意!)在您的条件
    (x>=5 | y>=5)
    中,您使用的是位或比较,您不应该这样做(除非是故意的,我在这里没有看到,而是使用布尔或:
    (x>=5 | y>=5)
  • 在循环本身中修改循环计数器是一个非常坏的习惯。不仅容易出错,而且很难理解和支持以这种方式编写的代码
  • 我会用这种方式重新编写代码

        spriteBatch.Begin();
        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; 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);
            }
        }
        spriteBatch.End();
    
    spriteBatch.Begin();
    对于(int x=0;x<5;x++)
    {
    对于(int y=0;y<5;y++)
    {
    x=level1[x,0];
    y=level1[0,y];
    //下面这行代码表示OutOfMemoryException
    绘制新矩形(x,y,32,32),新矩形(0,0,32,32),颜色为白色;
    }
    }
    spriteBatch.End();
    

    它将在主gameloop的每个
    draw()
    事件上重新绘制您的所有平铺(前提是您仍然从
    draw()调用此方法)
    你的
    游戏的方法
    类。XNA根据你电脑的性能和每帧必须进行的计算量来改变FPS速率。

    你可能试图批量处理无限数量的绘图调用。你想用这个奇怪的循环做什么?我正在尝试创建一个类来绘制地图对于我正在制作的游戏..我如何仍然制作要绘制的瓷砖?它不需要使用spriteBatch.Begin()和End(),我对player类做了同样的思考,它工作得非常完美..但这里有点不同。如果我在这里制作一个更新方法,它将有很多不同的spriteBatch.Draw()我还计划在多个级别上使用spageti代码。不,这是必需的(但最好每个Draw()事件只执行一次),请阅读。您可以在
    Draw()中使用
    Begin()
    End()
    一次
    method,就在绘图的开始和结尾,调用中间的所有绘图方法。+1回答得好。学究:XNA的默认设置使用固定帧速率。如果连续绘图也使用相同的纹理,则将内容保留在一个开始/结束块中只是性能优势()@AndrewRussell:XNA是否会跳过一些调用
    Draw()
    事件的操作,如果
    Update()
    花费的时间太长?我同意SpriteBatch的说法,我只是在回答中简化了它的作用。是的,这是真的。我想你可以说,像这样丢弃帧会改变FPS。但是XNA确实包括一个真正的可变帧速率模式-这不是默认模式。