2013-12-15 112 views
2

我目前正在研究庫存系統,但是我遇到問題,應該如何繪製它。繪製矩形多維數組

我有矩形看起來像這樣的陣列:

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

現在我要繪製槽像6 * 4列,6個時隙中的寬度和4個時隙中的高度。

我畫他們這樣直到我想通了,我應該怎麼吸引他們Y上還有:

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); 
} 

所以不知何故,我想移動Y33下來的時候[i]達到6以及重置x位置。

我確定這很簡單,但我無法弄清楚,所以任何幫助都會爲我節省很多時間。

回答

2

首先開始通過創建一個多維數組,不斷

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

,那麼你就具有雙重嵌套初始化循環

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

那麼你會for循環迭代做一個雙重嵌套通過他們

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

至少我認爲這就是你問的,讓我知道這是如何工作的。該常量可用於移動整個矩形陣列(如果要單獨操縱X和Y,則使用vector2)

+0

基本上,x * 33將成爲您在x上的位置並且y * 33是將成爲你在y上的位置,如果你看不到你的盒子,嘗試添加一個常數,如:(x * 33)+ 100 –

+1

感謝您的快速響應,它的工作完美!我不知道你可以創建多維數組。再次感謝! :D – Iskalder

+0

沒問題!我試圖找到一份工作,所以請給我一個upvote,如果你可以;) –