0

I was trying to make a Game-Engine when i got an error in my level class (class for level creation)

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

Error:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in 2dgame.exe

André
  • 111
  • 1
  • 4

2 Answers2

2

Your for loop for the Y check is wrong:

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

Should be:

for(int y = 0; y < LEVEL_HEIGHT; y++)
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
1

You have a typo in your inner (y) loop.

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

should be:

for(int y = 0; y < LEVEL_HEIGHT; y++)
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113