Drawing a rectangle multidimensional array

橙三吉。 提交于 2019-12-11 00:49:12

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/20597676/drawing-a-rectangle-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!