我有一个对象,如果它达到某种状态,我会创建一个新对象:
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;
}
}
}
更新 几帧后,它进入了一个无限循环,将形状添加到集合中,因为状态总是登陆。