2011-08-08 13 views
0

我有兩個Vector2列表:Position和Floor,我試圖這樣做: 如果Position與Floor相同,則從List中刪除該位置。從列表中刪除與其他列表中的任何其他對象具有相同值的所有對象。 XNA

以下是我認爲會工作,但它不會:

public void GenerateFloor() 
    { 

     //I didn't past all, the code add vectors to the floor List, etc. 
     block.Floor.Add(new Vector2(block.Texture.Width, block.Texture.Height) + RoomLocation); 

     // And here is the way I thought to delete the positions: 
     block.Positions.RemoveAll(FloorSafe); 
    } 

    private bool FloorSafe(Vector2 x) 
    { 
     foreach (Vector2 j in block.Floor) 
     { 
      return x == j; 
     } 

     //or 
     for (int n = 0; n < block.Floor.Count; n++) 
     { 
      return x == block.Floor[n]; 
     } 

    } 

我知道這不是好辦法,所以我怎麼能賴特呢?我需要刪除所有位置矢量2,它們與任何矢量矢量2相同。

============================================== ================================= 編輯: 它的工作原理!對於搜索如何做的人,這裏是我的最終代碼Hexagonal的答案:

public void FloorSafe() 
    { 
     //Gets all the Vectors that are not equal to the Positions List. 
     IEnumerable<Vector2> ReversedResult = block.Positions.Except(block.Floor); 

     //Gets all the Vectors that are not equal to the result.. 
     //(the ones that are equal to the Positions). 
     IEnumerable<Vector2> Result = block.Positions.Except(ReversedResult); 

     foreach (Vector2 Positions in Result.ToList()) 
     { 
      block.Positions.Remove(Positions); //Remove all the vectors from the List. 
     } 
    } 

回答

2

你可以做一個LINQ除外。這將刪除不在Floor集合中的Positions集合中的所有內容。

result = block.Positions.Except(block.Floor) 
+0

甜,它的工作!我有一些垃圾沒有被刪除,但我會發現如何做其餘的,謝謝! – Nairda

相關問題