Reputation: 574
I am trying to make a timer in C#. I do this by counting frames then making a variable(seconds) that is equal to the number of frames divided by 60. I also count minutes by having another variable(minutes) that is equal to seconds divided by 60.
The problem When minutes is upped by one, seconds keeps counting and i am not sure how to fix this. I want to add one to minutes whenever seconds reaches 60 but then go back down to zero without seting minutes to zero.
Here is what I've tried:
int frames = 0;
int seconds = 0;
int minutes = 0;
This is in update:
protected override void Update(GameTime gameTime)
{
minutes = seconds / 60;
seconds = frames / 60;
frames += 1;
Upvotes: 1
Views: 1908
Reputation: 1503839
I would suggest you just count frames, and derive seconds and minutes from that, whenever you need to. So if you really need to update all the variables in each Update
call, you'd have:
frames++;
seconds = (frames / 60) % 60;
minutes = frames / (60 * 60);
Or to be clearer, define some constants:
const int FramesPerSecond = 60; const int SecondsPerMinute = 60; const int FramesPerMinute = FramesPerSecond * SecondsPerMinute;
...
frames++;
seconds = (frames / FramesPerSecond) % SecondsPerMinute;
minutes = frames / FramesPerMinute;
(If you need to reset minutes to 0 after an hour, you would need to extend this further.)
It's not clear what you're trying to do though - there may well be a better way of achieving it. If you're definitely trying to count frames, this is fine... but if you're trying to compute elapsed time, you should remember a base time and then subtract that from the current time, rather than relying on a timer firing exactly once per second.
Upvotes: 5
Reputation: 2508
So, if you don't need System.Threading.Timer or System.Threading.DispatcherTimer, you can just use standard TimeSpan and add miliseconds to it. Or create your own TimeSpan-like class with automatically incrementing properties.
Upvotes: 1
Reputation: 6527
Attention: If you count from 0 to 60 (for minutes and second) you count 61 seconds. So go from 0 to 59 in your conditions.
for example there are 5 fingers at a hand but if we count from 0 we have 6 finger. (are we mutand?)
:)
Upvotes: 2