0

A bit stuck on incrementing the player's score after 5 seconds. Eventually i would like to be able to use the same logic to increment the score by 'x' amount after all different time periods. 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). This means the score ends up being 560 instead of just 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.

I am using the Stopwatch class to keep track of the amount of seconds gone by since the start of the game.

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

Rotem
  • 21,452
  • 6
  • 62
  • 109
6a6179
  • 272
  • 4
  • 20
  • http://stackoverflow.com/questions/8552648/how-to-create-a-timer-that-does-something this might be helpful – Rotem Sep 08 '13 at 14:57
  • Could you determine how much you want to increment the score per second, and just use Stopwatch.ElapsedMilliseconds (after converting to seconds of course) – tim Sep 08 '13 at 15:00
  • Tim: I converted the StopWatch.ElapsedTicks into seconds before and also stated i wanted to increment by 10 but this didn't work, as the score must have been incrementing 60 times a second by 10. – 6a6179 Sep 08 '13 at 15:51

2 Answers2

1

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

pinckerman
  • 4,115
  • 6
  • 33
  • 42
  • thanks Pinckerman, this seems to work, even though i have been using the StopWatch class... maybe i can adapt it a bit, however this does seems to work and it's relatively straight forward. Thanks :D – 6a6179 Sep 09 '13 at 14:09
  • This is what I do everytime I need to handle a time interval, so I'm sure it actually works :D – pinckerman Sep 09 '13 at 14:52
0

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.

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

In this example, the score should be updated every 2 seconds.

  • Thanks for the reply. For some reason this doesn't seem to be working. I have commented out the previous code and im just using what you have recommended above. Actually, i have just run my game again and it randomly worked once, however i ran it before and after it worked and it didnt work then. I hope this makes sense. Thanks again for the reply, i appreciate it :) – 6a6179 Sep 08 '13 at 15:33
  • It only works if being called by another function. Making a separate thread can be a good idea, only then you should be using a while(true) loop which increments the score and sleeps for x milliseconds until it is triggered again. The syntax should be fairly simple, see [System.Threading](http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx). – Ianis Vasilev Sep 08 '13 at 16:08
  • Cool, ok i will give it a go. Thanks v much Ianis :-) – 6a6179 Sep 08 '13 at 16:22