0

我有一个对象,如果它达到某种状态,我会创建一个新对象:

        if (currentShape.State == ShapeState.Landed)
        {
            Shape shape = new Shape();
            shape = utilities.GetRandomShape(contentManager);
            shapes.Add(shape);
            currentShape = shape;
        }

对象currentShape以这种方式不断变化。然而,出于某种原因,currentShape 仍然是 ShapeState.Landed 永远。

我的游戏中有物体掉落,当一个物体落地时,另一个物体被创建并分配给currentShape. 因此,无论何时currentShape着陆,都会创建另一个……如前所述。

Update方法的逻辑如下:

    public void Update(TimeSpan elapsedTime, List<Shape> shapes, Shape fallingShape)
    {
        List<Shape> shapesInSameColumn = new List<Shape>();

        foreach (var shape in shapes)
        {
            if (shape.ColumnNumber == fallingShape.ColumnNumber)
            {
                shapesInSameColumn.Add(shape);
            }
        }

        shapesInSameColumn.Remove(fallingShape);
        float yDestination = 0f;
        float yNextPosition = fallingShape.Position.Y + elapsedTime.Milliseconds / 30 * FallSpeed;

        if (shapesInSameColumn.Count == 0) // There are NO shapes in the column
        {
            yDestination = Utilities.bottomOfCanvas;
            if (yNextPosition > yDestination)
            {
                fallingShape.Position.Y = yDestination;
                fallingShape.State = ShapeState.Landed;
                return;
            }
            else
            {
                fallingShape.Position.Y = yNextPosition;
            }
        }
        else // There ARE shapes in the column
        {
            yDestination = shapesInSameColumn[shapesInSameColumn.Count - 1].Position.Y - Texture.Height;

            if (yNextPosition > yDestination)
            {
                fallingShape.Position.Y = yDestination;
                fallingShape.State = ShapeState.Landed;
                return;
            }
            else
            {
                fallingShape.Position.Y = yNextPosition;
            }
        }
    }

更新 几帧后,它进入了一个无限循环,将形状添加到集合中,因为状态总是登陆。

4

1 回答 1

1

这就是你听起来的样子(如果我弄错了,请纠正我):

public void Update(TimeSpan elapsedTime, List<Shape> shapes, Shape fallingShape)
    {
        List<Shape> shapesInSameColumn = new List<Shape>();

        //Added code.
        if (currentShape.State == ShapeState.Landed)
        {
            Shape shape = new Shape();
            shape = utilities.GetRandomShape(contentManager);
            shapes.Add(shape);
            currentShape = shape;
        }

        foreach (var shape in shapes)
        {
            if (shape.ColumnNumber == fallingShape.ColumnNumber)
            {
                shapesInSameColumn.Add(shape);
            }
        }

        shapesInSameColumn.Remove(fallingShape);
        float yDestination = 0f;
        float yNextPosition = fallingShape.Position.Y + elapsedTime.Milliseconds / 30 * FallSpeed;

        if (shapesInSameColumn.Count == 0) // There are NO shapes in the column
        {
            yDestination = Utilities.bottomOfCanvas;
            if (yNextPosition > yDestination)
            {
                fallingShape.Position.Y = yDestination;
                fallingShape.State = ShapeState.Landed;
                return;
            }
            else
            {
                fallingShape.Position.Y = yNextPosition;
            }
        }
        else // There ARE shapes in the column
        {
            yDestination = shapesInSameColumn[shapesInSameColumn.Count - 1].Position.Y - Texture.Height;

            if (yNextPosition > yDestination)
            {
                fallingShape.Position.Y = yDestination;
                fallingShape.State = ShapeState.Landed;
                return;
            }
            else
            {
                fallingShape.Position.Y = yNextPosition;
            }
        }
    }

所以这意味着它仍然会在//Added code执行后继续更新,对吗?解决方法是不更新 if currentShape.State == ShapeState.Landed

于 2013-08-07T00:52:28.950 回答