开发者

Remove all the objects from a list that have the same value as any other Object from an other List. XNA

I have two Lists of vector2: Position and Floor and I'm trying to do this: if a Position is the same a开发者_运维技巧s a Floor then delete the position from the List.

Here is what I thought would work but it doesn't:

    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];
        }

    }

I know this is not the good way, so how can I wright it? I need to delete all the Positions Vector2 that are the same As any of the Floors Vector2.

=============================================================================== EDIT: It works! For people searching how to do it, here is my final code of the answer of Hexxagonal:

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.
        }
     }


You could do a LINQ except. This will remove everything from the Positions collection that is not in the Floor collections.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜