到目前为止,我向foreach
语句下去,这导致了巨大的红色X.即使我/*
评论一切都期望构造者。我不明白为什么foreach会在我的应用程序中创建所有问题。
public class Map
{
[XmlElement("Layer")]
public List<Layer> Layers;
public Vector2 TileDimensions;
public void Initialize(ContentManager content)
{
foreach (Layer l in Layers)
l.Initialize(content, TileDimensions);
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Layer l in Layers) //This is causing the problems
l.Draw(spriteBatch);
}
}
}
Layer类是
public class Layer
{
/* public class TileMap // For some reason it still gets the giant red X even know it commented out.
{
[XmlElement("Row")]
public List<string> Row;
}
Vector2 tileDimensions;
[XmlElement("TileMap")]
List<List<Vector2>> tileMap;
TileMap tileLayout = new TileMap();
public Image image;
public Layer()
{
tileDimensions = new Vector2();
tileMap = new List<List<Vector2>>();
}
*/
public void Initialize(ContentManager content, Vector2 tileDimensions)
{
/* foreach (string row in tileLayout.Row)
{
string[] split = row.Split(']');
List<Vector2> tempTileMap = new List<Vector2>();
foreach (string s in split)
{
int value1, value2;
if (s != String.Empty && !s.Contains('x'))
{
string str = s.Replace("[", String.Empty);
value1 = int.Parse(str.Substring(0, str.IndexOf(':')));
value2 = int.Parse(str.Substring(str.IndexOf(':') + 1));
}
else
value1 = value2 = -1;
tempTileMap.Add(new Vector2(value1, value2));
}
tileMap.Add(tempTileMap);
}
image.Initialize(content);
this.tileDimensions = tileDimensions;*/
}
public void Draw(SpriteBatch spriteBatch)
{
/* for (int i = 0; i < tileMap.Count; i++)
{
for (int j = 0; j < tileMap[i].Count; j++)
{
if (tileMap[i][j] != -Vector2.One)
{
image.position = new Vector2(j* tileDimensions.X, i* tileDimensions.Y);
image.SourceRect = new Rectangle((int)(tileMap[i][j].X * tileDimensions.X),
(int)(tileMap[i][j].Y * tileDimensions.Y), (int)tileDimensions.X,
(int)tileDimensions.Y);
image.Draw(spriteBatch);
}
}
}*/
}
}
}
为什么foreach
循环会显示Giant Red X?我需要该功能使Map显示部分工作,所以我可以编辑地图。所以非常重要。我创建了另一个帖子,在我发现foreach
循环打破它之前。我希望有人可以帮我解决这个问题。我花了3天时间试着理解它为什么会破碎。