0

我有一个在 aList内的对象List

public List<List<MyObject>> Grid { get; set; }

我注意到有时当我这样做时:

Grid[currentShape.ColumnNumber].Remove(currentShape);

它从数组中删除两个对象。

我填充Grid对象的方式是,如果满足某个条件,那么我基本上会这样做:

Grid[newShape.ColumnNumber].Add(newShape);
currentShape = newShape;

currentShape是一个类变量,我用来在屏幕上的对象周围移动。是不是因为引用类型,当我有时这样做时.Remove(),它会从数组中删除多个对象?我需要以.Remove()某种方式扩展吗?

非常感谢任何帮助。

更新- 整个方法:

    public void MoveIfPossible(MoveDirection direction)
    {
        List<Shape> neighbouringShapes = new List<Shape>();

        switch (direction)
        {
            case MoveDirection.Right:
                if (currentShape.ColumnNumber == utilities.columns.Count - 1)
                    return;

                neighbouringShapes = GetNeighbouringShapes(currentShape.ColumnNumber);

                foreach (var s in neighbouringShapes)
                {
                    if (currentShape.Position.X + 1 < s.Position.X && currentShape.Position.Y + currentShape.Texture.Height >= s.Position.Y)
                    {
                        return;
                    }
                }
                world.Grid[currentShape.ColumnNumber].Remove(currentShape);
                world.Grid[currentShape.ColumnNumber + 1].Add(currentShape);

                currentShape.Position.X = utilities.columns[currentShape.ColumnNumber + 1].X;
                currentShape.ColumnNumber++;

                currentShape.Coordinate.X = currentShape.ColumnNumber;
                currentShape.Coordinate.Y = world.Grid[currentShape.ColumnNumber].Count - 1;
                break;

            case MoveDirection.Left:
                if (currentShape.ColumnNumber == 0)
                    return;

                neighbouringShapes = GetNeighbouringShapes(currentShape.ColumnNumber - 1);

                foreach (var s in neighbouringShapes)
                {
                    if (currentShape.Position.X - 1 > s.Position.X && currentShape.Position.Y + currentShape.Texture.Height >= s.Position.Y)
                    {
                        return;
                    }
                }
                world.Grid[currentShape.ColumnNumber].Remove(currentShape);
                world.Grid[currentShape.ColumnNumber - 1].Add(currentShape);

                currentShape.Position.X = utilities.columns[currentShape.ColumnNumber - 1].X;
                currentShape.ColumnNumber--;

                currentShape.Coordinate.X = currentShape.ColumnNumber;
                currentShape.Coordinate.Y = world.Grid[currentShape.ColumnNumber].Count;
                break;
        }
    }
4

0 回答 0