Spritebatch acting weird

I’m trying to render a few hundred tiles (400 to be exact), and for whatever reason, the spritebatch is only drawing about half of them, in the same weird pattern every time. I tried outputting the positions to the console and sure enough, those are all correct. I tried different overloads of the SpriteBatch.Draw method but I get the same results. Here is a screenshot:!
Capture|690x390

Here is the code:

public class TileMap
{
    public Vector2[] position;
    public int[] type;
    public int width, height, size;

    public TileMap(int width, int height)
    {
        position = new Vector2[width * height];
        type = new int[width * height];

        this.width = width;
        this.height = height;
        size = width * height;

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                position[x * y] = new Vector2(x, y);
                Console.WriteLine(x + " " + y);
            }
        }
    }

    public void Generate(int seed)
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < size; i++)
        {
            Point position16 = (position[i] * 16).ToPoint();
            spriteBatch.Draw(Main.tileTextures[type[i]], new Rectangle(position16, new Point(16, 16)), new Rectangle(0, 0, 16, 16), Color.White);
        }
    }
}

btw I’m passing 20, 20 into the constructor. If anyone knows what the problem is I would appreciate your feedback.

Why are you not using a 2 dimensional array?

You calculation for the position is wrong as the second time through your X loop your position will be 0 again…

1 * 0 = 0…

I would swap the X and Y loops as I think more of Across then Down…

for (int y = 0; y < height; y++)
{
        for (int x = 0; x < width; x++)
        {
                position[x + (y * width)] = new Vector2(x, y);
                Console.WriteLine(x + " " + y);
        }
}

I would define it this way:

public Vector2[,] position;
      position = new Vector2[width, height];
position[x, y] = new Vector2(x, y);

I would also question why you’re using a Vector2 for the tile? Normally (at least for me), I would be storing something like an Int or an Object in the position[] array as it would tell me what’s at position in the tile map.

Thanks that worked

If you need a 1d array index from 2d coordinates the formula is.

position[y * width + x] = new Vector2(x, y);

the width above is called stride * y moves you down a column.