2013-08-07 88 views
0

我有,如果它達到一定的狀態,我創建了一個新對象的對象:XNA對象不改變

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

對象currentShape一直以這種方式改變。但是由於某些原因,currentShape仍然是ShapeState.Landed。

當我到達地面時,有另一個物體被創建並被分配到currentShape。所以無論何時currentShape登陸,另一個創建......如上所述。

的更新方法的邏輯如下:

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

UPDATE 幾幀後,它會在以增加形狀的集合作爲狀態總是登陸的無限循環。

+0

我的猜測是,在您提供的第一個代碼運行後,它會運行'Update'方法,其中'fallingShape.State'設置爲'Landed'。 – ShadowCat7

+0

......請您詳細說明您的意思/您認爲問題是什麼? – Subby

+0

顯示運行您提供的第一個代碼的對象的代碼。它可能正在執行該代碼,然後運行'Update'方法。 – ShadowCat7

回答

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