C# 列表填充的问题
这种情况在我身上发生过不止一次,我不知道会是什么。我有一个手工制作食谱列表,我添加如下:C# 列表填充的问题,c#,C#,这种情况在我身上发生过不止一次,我不知道会是什么。我有一个手工制作食谱列表,我添加如下: ItemRecipe firePitRecipe = new ItemRecipe(); firePitRecipe.Items.Add(new Rock()); foreach (Item item in firePitRecipe.Items) { item.ItemCount = 5; }
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;
}
}
公共级摇滚乐:项目
{
纹理2D纹理;
公共摇滚乐()
{
这个。纹理=纹理;
ItemName=“岩石”;
ItemType ItemType=ItemType.craft;
}
公共摇滚(矢量2位置)
{
纹理=含量。荷载(“岩石”);
这个位置=位置;
这个。纹理=纹理;
ItemName=“岩石”;
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);
}
}
}
}
private void setItemPositionTable(播放器,项目输出)
{
对于(int i=0;i
我很困惑,因为这适用于列表中的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"));
}
}
CraftingTableItems=新列表(12);
对于(int x=0;x<4;x++)
{
对于(int y=0;y<3;y++)
{
添加(新项(新向量2((int)(64*x),(int)(64*y)),“空”);
}
}
我认为您没有给我们提供足够的代码来了解哪里出了问题。这件事引起了我的注意:
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()
创建岩石,此.Texture
将根据您向我们显示的内容保持为空。以后绘制岩石时,可能由于缺少纹理而失败。您没有提供足够的代码进行调试。请提供产生错误的代码库。我不知道是什么产生了错误。这只是一条线,在移除后,使我的工艺盒中的对象再次绘制。我想知道在向列表中添加我不知道的项目时是否有一些限制此代码的用途:foreach(firefitrecipe.items中的项目){Item.ItemCount=5;}
?这是制作火坑所需的项目数量。我需要5块石头。我不知道为什么我把它放进一个循环…我发现我的SetItemPosition方法工作不正常。在控制台中,只要有一个飞行器可用,它就会写一行。但我的工艺桌还是空的。我只是在SetItemPositionTable方法中找不到错误。好吧,如果你在工艺表中设置一个“空”项目,它似乎会清除整个表,也就是说,它将所有工艺表插槽设置为“空”。我不知道这是否是你想要的行为,不过…很好地捕捉到了这一点。这不是我的本意,但它解决了我的问题!