2013-05-28 34 views
0

我打算製作一款射擊遊戲,玩家可以通過「+分數」查看他們射擊敵人的點數。他們每個人我已經加入到List這樣的:幾秒鐘後移除列表中的物體XNA C#

scoreList.Add(new ScoreHUD(Content.Load<SpriteFont>(@"arial"), 20, new Vector2(e.position.X, e.position.Y))); 

20是得到了刷怪的分數。這表明在每個沒有任何問題的敵人的位置上「+20」。

現在,我想在顯示3秒後從遊戲中移除它們。我已經嘗試在Update函數中連接一個定時器函數,但不知道如何將每個計時器函數連接到每個分數中(我認爲這是不好的做法)。有什麼辦法可以解決這個問題嗎?

回答

1

這是一個非常簡單的例子,其中一個項目的去除被稱爲每2秒。

//Declares a timespan of 2 seconds 
TimeSpan timeSpan = TimeSpan.FromMilliseconds(2000); 


public override void Update(GameTime gameTime) 
{ 
    // Decrements the timespan 
    timeSpan -= gameTime.ElapsedGameTime; 

    // If the timespan is equal or smaller time "0" 
    if (timeSpan <= TimeSpan.Zero) 
    { 
     // Remove the object from list 
     scoreList.RemoveAt(1); 
     // Re initializes the timespan for the next time 
     timeSpan = TimeSpan.FromMilliseconds(2000); 

    } 

    base.Update(gameTime) 
} 

我希望這可以幫助您

+0

我只是需要更改代碼,以便檢查列表是否包含任何內容,並將removeat(1)更改爲removeat(0),並且它的工作方式與魅力類似!謝謝! – user2002495

+0

謝謝你,總是樂意幫助別人^^。 –

0

確定...我將與僞代碼解釋,我會是這樣的:

class ScoreList 
    Position (vector) 
    Value (string) 
    Duration (integer) = 60 ' 1 second 
    visible = false 
end class 

class Score 
    inhertis list of scorelist 

    sub add(position, value, duration) 
    ' add position, set visible to true 
    end sub 
    sub update() 
    for each score in me 
     if score visible 
     score.duration -= 1 
     if score.duration < 1 then score.visible = false; 
     end if 
    end for 

    score.removeAll(function(c) c.visible = false) ' to remove unused 
    end sub 

    sub draw() 
    for each score in me 
     if score.visible then draw it 
    end if 
    end sub 

end class 
+0

我沒有使用這個邏輯,把一生的每個分數級,在比分類的更新我遞減它,使無形的,但它沒有工作,謝謝無論如何 – user2002495

+0

由該代碼我控制一切(敵人,星空,子彈,通電...),它的工作原理。 –