0

当我的关卡类(用于创建关卡的类)出错时,我正在尝试制作游戏引擎

级别.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2dgame
{
    class Level
    {

        public const int LEVEL_WIDTH = 12;
        public const int LEVEL_HEIGHT = 8;


        private static TextureID[,] blocks = new TextureID[LEVEL_WIDTH, LEVEL_HEIGHT];

        public static TextureID[,] Blocks
        {
            get { return blocks; }
            set { blocks = value; }
        }

        public static void initLevel()
        {
            for (int x = 0; x < LEVEL_WIDTH; x++)
            {
                for(int y = 0; x < LEVEL_HEIGHT; y++)
                {
                    if (y >= 12)
                    {
                        blocks[x, y] = TextureID.dirt; //ERROR
                    }
                    else
                    {
                        blocks[x, y] = TextureID.air;
                    }
                }
            }
        }
    }
}

错误:

2dgame.exe 中发生了“System.IndexOutOfRangeException”类型的未处理异常

4

2 回答 2

2

您的 Y 检查 for 循环是错误的:

for(int y = 0; x < LEVEL_HEIGHT; y++)

应该:

for(int y = 0; y < LEVEL_HEIGHT; y++)
于 2015-12-04T15:23:44.797 回答
1

您的内部 ( y) 循环中有错字。

for(int y = 0; x < LEVEL_HEIGHT; y++)

应该:

for(int y = 0; y < LEVEL_HEIGHT; y++)
于 2015-12-04T15:23:48.360 回答