xna游戏工作室中的延迟c#代码问题

imp*_*yre 4 c# xna

我的代码似乎编译好了,但是当我尝试运行它时,它的挂起非常糟糕.

我已经与Riemers XNA教程一直跟随一起在这里.

我对C#很熟悉,但绝不是专家.到目前为止,我没有遇到任何问题,并且没有任何错误或异常被抛出......它只是挂断了.我在他的相关论坛上看过,用户讨论过其他问题,通常与拼写错误或代码错误有关,但那里没有这样的东西......每个人似乎都可以运行得很好.

有没有我做错了?底部的嵌套for循环对我来说似乎有点笨拙.screenWidth和screenHeight分别为500和500.

BTW:这是从LoadContent重写方法运行的,所以它应该只运行一次,据我所知.

    private void GenerateTerrainContour()
    {
        terrainContour = new int[screenWidth];

        for (int x = 0; x < screenWidth; x++)
            terrainContour[x] = screenHeight / 2;
    }

    private void CreateForeground()
    {
        Color[] foregroundColors = new Color[screenWidth * screenHeight];

        for (int x = 0; x < screenWidth; x++)
        {
            for (int y = 0; y < screenHeight; y++)
            {
                if (y > terrainContour[x])
                    foregroundColors[x + y * screenWidth] = Color.Green;
                else
                    foregroundColors[x + y * screenWidth] = Color.Transparent;
                fgTexture = new Texture2D(device, screenWidth, screenHeight, false, SurfaceFormat.Color);
                fgTexture.SetData(foregroundColors);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mat*_*vey 6

可能与你正在创造250,000个屏幕大小的纹理(圣洁的moly)这一事实有关!

资源分配总是很重 - 特别是当您处理声音和图像等媒体时.

看起来你真的只需要一个纹理,尝试移动fgTexture = new Texture2D(device, screenWidth, screenHeight, false, SurfaceFormat.Color);到循环之外.然后尝试移出fgTexture.SetData(foregroundColors);循环.

private void CreateForeground()
{
    Color[] foregroundColors = new Color[screenWidth * screenHeight];

    fgTexture = new Texture2D(device, screenWidth, screenHeight, false, SurfaceFormat.Color);

    for (int x = 0; x < screenWidth; x++)
    {
        for (int y = 0; y < screenHeight; y++)
        {
            if (y > terrainContour[x])
                foregroundColors[x + y * screenWidth] = Color.Green;
            else
                foregroundColors[x + y * screenWidth] = Color.Transparent;
        }
    }

    fgTexture.SetData(foregroundColors);
}
Run Code Online (Sandbox Code Playgroud)