列表填充的问题

时间:2014-10-13 02:42:24

标签: c#

这不止一次发生在我身上,我无法弄清楚它可能是什么。我有一个制作配方列表,我这样添加:

        ItemRecipe firePitRecipe = new ItemRecipe();
        firePitRecipe.Items.Add(new Rock());
        foreach (Item item in firePitRecipe.Items)
        {
            item.ItemCount = 5;
        }
        firePitRecipe.Output = new FirePit();
        firePitRecipe.Name = "firepit";
        CraftingList.Add(firePitRecipe);

我目前在列表中有6个食谱。当我尝试制作一件物品时,没有任何东西出现在显示我的工艺品的盒子里。当我注释掉这一行和最后两个食谱时:

//firePitRecipe.Items.Add(new Rock());

一切都按预期运作。我不知道这个bug是什么。是因为每次添加到recipe.Items时我都在创建一个项目的新实例?

      public class Rock : Item
{
    Texture2D texture;

    public Rock()
    {
        this.Texture = texture;
        ItemName = "rock";
        ItemType itemType = ItemType.craft;
    }

    public Rock(Vector2 position)
    {
        texture = Content.Load<Texture2D>("rock");

        this.Position = position;
        this.Texture = texture;

        ItemName = "rock";
        ItemType itemType = ItemType.craft;
        ItemPickedUp = false;
    }
}

这是我的摇滚课,我所有的其他项目都很相似。这没有什么奇怪的,所以我没有看到任何错误。设置断点显示我的工艺列表中的所有项都不为空。

另一件事可能是当我循环浏览列表并检查我的库存中的项目时,但我对此表示怀疑。

我把它缩小到几种方法。它们只是没有在表中绘制或设置正确。

 private void SetItemPositionInTable(Player player, Item output)
    {
        for (int i = 0; i < CraftingTableItems.Count(); i++)
        {
            if (CraftingTableItems[i].ItemName == "empty")
            {
                CraftingTableItems[i].ItemName = output.ItemName;
                CraftingTableItems[i].Texture = output.Texture;
                Console.WriteLine(output.ItemName);
                break;
            }
            else if (CraftingTableItems[i].ItemName == output.ItemName)
            {
                break;
            }
            else if (CraftingTableItems[i].ItemName != "empty" && output.ItemName == "empty")
            {
                CraftingTableItems[i].ItemName = "empty";
            }
        }
    }

  public void DrawCraftingTable(SpriteBatch sb, Player player, SpriteFont font)
    {
        if (player.DrawCraftingTable == true)
        {
            sb.Draw(Texture, CraftingRectangle, Color.White);

            foreach (Item item in CraftingTableItems)
            {
                if (item.ItemName != "empty")
                {
                    sb.Draw(item.Texture, item.ItemSlotPosition, Color.White);
                }
            }
        }
    }

我很困惑,因为这与列表中的3项有效但不是6项?这是制作表列表。

              CraftingTableItems = new List<Item>(12);

        for (int x = 0; x < 4; x++)
        {
            for (int y = 0; y < 3; y++)
            {
                CraftingTableItems.Add(new Item(new Vector2((int)(64 * x), (int)(64 * y)), "empty"));
            }
        }

1 个答案:

答案 0 :(得分:2)

我认为你没有给我们足够的代码来知道出了什么问题。这引起了我的注意:

public class Rock : Item
{
    Texture2D texture;  // This isn't initialized so it is null?

    public Rock()
    {
        this.Texture = texture;  // Here the assumed null is assigned to base class Texture
        ItemName = "rock";
        ItemType itemType = ItemType.craft;
    }
    ...

因为您正在使用new Rock()创建摇滚,this.Texture将根据您向我们展示的内容保持为空。当以后绘制那块岩石时,也许如果由于缺少纹理而失败。