XNA对象不变

时间:2013-08-07 00:16:32

标签: c# xna

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

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

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

我所拥有的游戏中有物体掉落,当一个物体到达地面时,另一个物体被创建并被分配给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;
            }
        }
    }

更新 在几帧之后,它会进入无限循环,将状态添加到集合中,因为State始终是Landed。

1 个答案:

答案 0 :(得分: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执行后继续更新,对吗?如果currentShape.State == ShapeState.Landed,则修复不会更新。