I'm having an issue with C# where I keep getting an IndexOutOfRangeException whenever I run the Draw method in one of my classes.
I've been trying to figure out the problem for a couple of hours now, but I haven't been able to find the exact problem and I haven't been able to find any solutions on StackOverflow.
I'm new to C# (I've only been learning for 3 or 4 days), so the solution's probably going to be pretty obvious.
Level.cs:
using Microsoft.Xna.Framework.Graphics;
namespace Game1 {
class Level {
public Tile[,] Tiles;
public void Draw(SpriteBatch sb) {
for (int x = 0; x < Tiles.GetLength(0); x++)
for (int y = 0; x < Tiles.GetLength(1); y++)
if (Tiles[x, y] != null)
Tiles[x, y].Draw(sb, x, y);
}
public Level(int size) {
Tiles = new Tile[size, size];
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
Tiles[x, y] = Tile.Tiles[0];
}
}
}
Tile.cs:
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Game1 {
class Tile {
public static List<Tile> Tiles = new List<Tile>();
Texture2D Image;
public void Draw(SpriteBatch sb, int x, int y) {
sb.Draw(this.Image, new Vector2(x * this.Image.Width, y * this.Image.Height), Color.White);
}
public Tile(Texture2D image) {
this.Image = image;
Tiles.Add(this);
}
}
}