C# 带有for循环的不均匀栅格

C# 带有for循环的不均匀栅格,c#,for-loop,xna-4.0,C#,For Loop,Xna 4.0,这就是我的问题: 我有一个RPG清单,通过两个for循环在网格(7x4)中绘制项目: [Update Method] index = 0; for (int x = 0; x < 7; x++) { for (int y = 0; y < ItemList.Count / 7; y++) { ItemList[index].gridLocation = new Point(x, y); index++; } } 但它是这样做

这就是我的问题:

我有一个RPG清单,通过两个for循环在网格(7x4)中绘制项目:

[Update Method]

index = 0;
for (int x = 0; x < 7; x++)
{
    for (int y = 0; y < ItemList.Count / 7; y++)
    {
        ItemList[index].gridLocation = new Point(x, y);
        index++;
    }
}
但它是这样做的:

X X X X X X X
X X X X X X X
X X X X X X X
它将截断不大于或等于7的结束行

现在,当我使用一个项目(使用一个项目将其从列表中删除)时,它会将下一个隐藏的项目添加到第三行的末尾

我非常感谢你的帮助。 谢谢

编辑:感谢Nico的帮助,这就是我的代码现在的样子:

                if (ItemList.Count > 6)
                {
                    for (int index = 0; index < ItemList.Count; ++index)
                    {
                        ItemList[index].gridLocation = new Point(index % 7, (int)(index / 7));
                        ItemList[index].UpdateValues(ScreenLocation, itemSize, LocY);
                    }
                }
                else if (ItemList.Count < 7)
                {
                    for (int index = 0; index < ItemList.Count; ++index)
                    {
                        ItemList[index].gridLocation = new Point(index, 0);
                        ItemList[index].UpdateValues(ScreenLocation, itemSize, LocY);
                    }
                }
if(ItemList.Count>6)
{
for(int index=0;index

我发现有一个问题,如果itemList低于7,它将不会显示任何项目。上面的代码修复了它。再次感谢

您可以使用索引进行迭代:

for(int index = 0; index < ItemList.Count; ++index)
    ItemList[index].gridLocation = new Point(index % 7, (int)(index / 7));
for(int index=0;index

请注意,我已经翻转了项目顺序。您的是列方式,现在是行方式。我想,你是这个意思,因为你有一个固定的网格宽度7和一个可变的行号。如果不正确,请留下评论。

首先感谢您的快速回复,其次,它立即起作用。非常感谢你!这个问题在我的代码中已经存在了相当长一段时间。
for(int index = 0; index < ItemList.Count; ++index)
    ItemList[index].gridLocation = new Point(index % 7, (int)(index / 7));