游戏引擎错误IndexOutOfRangeException

时间:2015-12-04 15:19:44

标签: c# game-engine indexoutofrangeexception

当我在关卡课程中遇到错误时,我试图制作一个游戏引擎(等级创建)

Level.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;
                    }
                }
            }
        }
    }
}

错误:

  

未处理的类型&#39; System.IndexOutOfRangeException&#39;发生在2dgame.exe

2 个答案:

答案 0 :(得分:2)

Y检查的for循环错误:

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

应该是:

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

答案 1 :(得分:1)

你的内部(y)循环中有一个拼写错误。

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

应该是:

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