SortedList的困难

时间:2013-04-26 12:35:18

标签: c# xna

我总是在以下行中收到此错误消息:

posDiffRectFList.Add(posDiff_Float, Backgrounds.levellistshapes[j]);

ArgumentException:已存在具有相同密钥的条目。

有什么问题?我应该改变什么?

例如: 是否可以将其添加到SortedList?或者这是不可能的,因为它是相同键的两倍(1.5f)?

posDiffRectFList.Add(1.5f, (10,20,100,100));

posDiffRectFList.Add(1.5f, (50,70,60,60));


SortedList<float, System.Drawing.RectangleF> posDiffRectFList = new SortedList<float, System.Drawing.RectangleF>();
for (int j = 0; j <= Backgrounds.LevelListTiles.Count - 1; j++)
{
  posDiff = new Vector2((int)((Backgrounds.LevelListTiles[j].X + Backgrounds.LevelListTiles[j].Width * 0.5) - (Basketball_Rect.X + Basketball_Rect.Width * 0.5)), (int)((Backgrounds.LevelListTiles[j].Y + Backgrounds.LevelListTiles[j].Height * 0.5) - (Basketball_Rect.Y + Basketball_Rect.Height * 0.5)));
  posDiff_Float = posDiff.Length();
  posDiffRectFList.Add(posDiff_Float, Backgrounds.LevelListTiles[j]);
}
//check for collision between tiles and basketball, beginning with the lowest posDiff_Float and his corresponding RectangleF
for (int j = 0; j <= posDiffRectFList.Count - 1; j++)
{ 
 if (kugelNewRect.IntersectsWith(posDiffRectFList.Values[j]))

1 个答案:

答案 0 :(得分:3)

SortedList文档指出您不能两次添加相同的密钥。

  

ArgumentException - SortedList对象中已存在具有指定键的元素。