0

我打算做一个射击游戏,玩家可以通过“+分数”来查看他们射击敌人得了多少分。我将它们中的每一个都添加到了List这样的位置:

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

20是获得杀死某物的分数。这表明每个被杀死的敌人的位置“+20”都没有问题。

现在,我试图在显示 3 秒后将它们从游戏中删除。我曾尝试在函数中连接计时器功能,Update但不知道(我认为这是不好的做法)如何将每个计时器功能连接到每个分数。有什么办法可以解决这个问题吗?

4

2 回答 2

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

我希望这对你有帮助

于 2013-05-28T08:17:40.070 回答
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
于 2013-05-28T08:14:59.410 回答