Question

I'm currently working on an inventory system however I'm having problem figuring out how I should draw it.

I have an array of rectangles looking like this:

Rectangle[] Inventoryslots = new Rectangle[24]; // 24 slots

now I want to draw the slots like a 6*4 columns, 6 slots in width and 4 slots in height.

I'm drawing them like this until I have figured out how I should draw them on y as well:

for (int i = 0; i < Inventoryslots.Length; i++)
{      
    Inventoryslots[i] = new Rectangle(i * 33, 0, box.Width, box.Height);

    spriteBatch.Draw(box, Inventoryslots[i], Color.White);
}

So somehow I want to move y33 down when [i] reaches 6 and also reset x position.

I'm sure it's pretty simple but I just can't figure it out so any help would save me a lot of time.

Was it helpful?

Solution

first start by creating a multidimensional array and constant

const int offset = 100; 
Rectangle[,] Inventoryslots = new Rectangle[6, 4]; 

then you'll initialize with a double nested for loop

for (int x = 0; x < 6; x++)
{
    for (int y = 0; y < 4; y++)
    {
        Inventoryslots[x, y] = new Rectangle((x * Width) + offset, 
             (y * Height) + offset, Width, Height); 
    }
}

then you'll do a double nested for loop to iterate through them

for (int x = 0; x < 6; x++)
{
    for (int y = 0; y < 4; y++)
    {
       spritebatch.draw(texture, Inventoryslots[x, y], Color.White); 
    }
}

At least I think that's what you're asking, let me know how this works. The constant can be used to move the whole array of rectangles around (use a vector2 if you want to manipulate X and Y individually)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top