2014-12-06 63 views
0

我創建的XNA遊戲,我需要一個碰撞檢測邏輯:XNA - 碰撞永遠不會發生

public Rectangle boundingBox = new Rectangle((int)playerShipPos.X, (int)playerShipPos.Y, frameWidth, frameHeight); 

this.boundingBox = new Rectangle((int)meteorPosPub.X, (int)meteorPosPub.Y, (int)meteorTexture.Width, (int)meteorTexture.Height); 

for (int i = meteorList.Count - 1; i >= 0; i--) 
{ 
    meteorGenerator meteor = new meteorGenerator(Vector2.Zero); 

    if (meteorList[i].meteorPosPub.Y > 664) 
    { 
     meteorList.RemoveAt(i); 
     if (meteor.boundingBox.Intersects(playerShip.boundingBox)) 
     { 
      meteorList.RemoveAt(i); 
     } 
    } 
} 

所以我想實現這一目標的效果:如果玩家船舶觸碰流星流星是皮並從列表中刪除,但實際上沒有任何反應。

回答

0
for (int i = meteorList.Count - 1; i >= 0; i--) 
{ 
    meteorGenerator meteor = new meteorGenerator(Vector2.Zero);//you are creating new list meteors every frame, this is ridiculous 

    if (meteorList[i].meteorPosPub.Y > 664) 
    { 
     meteorList.RemoveAt(i);//if you are removing a position from a list, not destroying the meteor 
     if (meteor.boundingBox.Intersects(playerShip.boundingBox)) 
     { 
      meteorList.RemoveAt(i);//you already did this, this conditional is unnecessary 
     } 
    } 
} 

我不知道你在做什麼,但這是我會做的。

1.讓玩家和流星從一個具有固體物體的屬性的類繼承。

  1. 將這些添加到具有基於對象類型的唯一ID的列表中。

  2. 每一幀,檢查的ID(這給你你想要什麼碰撞什麼額外的控制)。

  3. 繼續檢查碰撞,如果你想刪除一個元素,只是從列表中刪除它,消滅它。