2012-10-27 55 views
0

我只是想處理在XNA遊戲工作室2D遊戲中創建的船上Land碰撞時的得分。Life(Score)被解析爲100個變量,稱爲GameLife類中的Life ...XNA碰撞時的處理得分

我想要得到2分,以減少生命當兩個物體相撞......

但問題是,當船相撞上的土地的生命瞬間將負值,直到船從對象保持距離陸地物體...請給我一個幫助...

代碼在這裏提供

`private void HandleLandCollition(List<LandTile> landtiles) 
{ 
    foreach (LandTile landtile in landtiles) 
    { 
     rectangle1 = new Rectangle((int)landtile.position.X - landtile.texture.Width/2, 
        (int)landtile.position.Y - landtile.texture.Height/2, 
        landtile.texture.Width, landtile.texture.Height);//land object 

     rectangle2 = new Rectangle((int)position.X - texture.Width/2, 
        (int)position.Y - texture.Height/2, 
        texture.Width, texture.Height);//rectangle2 is defined to ship object 
     if (rectangle1.Intersects(rectangle2)) 
     { 
      shiplife.Life = shiplife.Life - 2; 
     } 
    } 
} 
+0

它繼續計算傷害,對吧? ..直到船離開土地? – Rolice

+0

是的Rolice ..你是對的..有沒有其他的...... ??? – tharindlaksh

+1

你必須在碰撞後「扔掉」物體。當物體在地面上時,渲染繼續計數碰撞。 :) 以現實的方式翻譯並旋轉它。另一件事是檢查是否有足夠的速度來計算傷害,即在碰撞速度爲0時,你可以跳過生命的懲罰。 – Rolice

回答

1

您的問題可能是您每幀調用此方法。通常XNA會每秒調用Update()60次,所以如果您的船一秒鐘觸及landtile,它會失去2 * 60 = 120的健康點,這會導致您看到的負值。

我的解決辦法是這樣的:

protected override void Update(GameTime gameTime) 
{ 
    float elapsedTime = (float) gameTime.ElapsedTime.TotalSeconds; 
    HandleCollision(landtiles, elapsedTime); 
} 
float landDamagePerSecond = 2; 
private void HandleLandCollision(List<LandTile> landtiles, float elapsedTime) 
{ 
    shipRectangle= new Rectangle((int)position.X - texture.Width/2, 
       (int)position.Y - texture.Height/2, 
       texture.Width, texture.Height);//rectangle2 is defined to ship object 

    foreach (LandTile landtile in landtiles) 
    { 
       landRectangle= new Rectangle(
       (int)landtile.position.X - landtile.texture.Width/2, 
       (int)landtile.position.Y - landtile.texture.Height/2, 
       landtile.texture.Width, landtile.texture.Height);//land object 

     if (landRectangle.Intersects(shipRectangle)) 
     { 
      shiplife.Life -= landDamagePerSecond * elapsedTime; 
     } 
    } 
} 

elapsedTime是自上次框架是所謂的,通過與損害multiplicating這個landtile交易,以每秒船將導致船舶失去2個healthpoints每當它觸及到一片土地時;)