简体   繁体   English

一定时间后如何增加分数? XNA

[英]How to increment score after certain time period? XNA

A bit stuck on incrementing the player's score after 5 seconds. 在5秒钟后增加玩家的得分有些卡住。 Eventually i would like to be able to use the same logic to increment the score by 'x' amount after all different time periods. 最终,我希望能够使用相同的逻辑在所有不同时间段后将分数增加'x'数量。 At the moment, the score is being incremented by 10 but 60 times a second (due to the update method being called 60 times a second). 此刻,乐谱正在每秒增加10到60次(由于更新方法被称为每秒60次)。 This means the score ends up being 560 instead of just 10. 这意味着得分最终是560,而不是10。

I was thinking of trying to use variables such as isPreviousScoreChanged and isCurrentScoreChanged , like when detecting if a button has been pressed and released, however this isn't working too well either. 我正在考虑尝试使用isPreviousScoreChangedisCurrentScoreChanged类的变量,就像在检测是否已按下和释放按钮时一样,但这也不能很好地工作。

I am using the Stopwatch class to keep track of the amount of seconds gone by since the start of the game. 我正在使用Stopwatch类来跟踪自游戏开始以来经过的秒数。

the code is pretty much: 代码差不多:

if (Stopwatch.ElapsedTicks == 5)
{
    playerScore += 10;
}

[I know it's possible to paste code in here but there isn't much to paste and it's quite simple what i've done so far] [我知道可以在这里粘贴代码,但是粘贴的内容不多,到目前为止我做的很简单]

thanks v much for reading everyone :-) 非常感谢您阅读大家:-)

Do something like this: 做这样的事情:

if (timer > TimeSpan.Zero)
{
   timer -= gameTime.ElapsedGameTime;
   if (timer <= TimeSpan.Zero)
   {
       playerScore += 10;
       timer = TimeSpan.Zero;
   }
}

Of course you have to set timer = new TimeSpan(0, 0, 5); 当然,您必须设置timer = new TimeSpan(0, 0, 5);

First - I recommend not using ticks, since one tick is a very small amount of time. 首先-我不建议使用刻度线,因为一个刻度线只占用很少的时间。 Second - if updating every x seconds, using the modulus operator if more efficient. 第二-如果每x秒更新一次,则使用模运算符(如果更有效)。

if((Stopwatch.ElapsedMilliseconds%2000)==0){playerScore+=10}

In this example, the score should be updated every 2 seconds. 在此示例中,分数应每2秒更新一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM