I'm creating a game with XNA, and having issues with Vector2 arrays. The code below will loop through each square on the grid for the game 40x40pixels. The first for loop getting the X coordinate and the second getting the Y coordinate. I'm getting the proper width and height (if you see the output compared to the console.writeline), but the only thing that isn't giving correct output is when I try to get it from the array. Am I making a mistake somewhere?

Problem Code:

private void SetUpBlocks(){
            blocks.Position = new Vector2[(screenWidth*screenHeight)/40];
            int i=0;
            for (int width = 0; width < screenWidth; width+=40){
                for (int height = 0; height < screenHeight; height+=40){
                    blocks.Position[i] = new Vector2(width, height);
                    i++;
                    Console.WriteLine("Block#"+i+" Width: "+blocks.Position[i].X+" Height: "+height);
                }
            }
        }

OUTPUT:

etc.etc.
Block#297 Width: 0 Height: 440
Block#298 Width: 0 Height: 480
Block#299 Width: 0 Height: 520
Block#300 Width: 0 Height: 560

You're incrementing 'i' before you print it out, so it's grabbing the width value of the next, uninitialized block.

blocks.Position[i] = new Vector2(width, height);
Console.WriteLine("Block#"+i+" Width: "+blocks.Position[i].X+" Height: "+height);
i++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.