# Why is the mana regenerating too fast?

So I got this weird problem with regeneration - I decided to create a mana bar which is going down every time when character attack. When the bar is less than integer 100, it should regenerate 1 points per 9 seconds and then stop. The problem is that instead of it, mana regenerate rapidly after chosen time and then, with every attack it regenerate so fast, that is basically staying in same place.

        ////MANA REGENERATION
currentTime += gameTime.ElapsedGameTime.Milliseconds;
if (max_mana>=0 && max_mana <= 100 &&currentTime >= 9000)
{
max_mana += 1;
}


that's the parameters for the mana:

in Initialize:

   //PLAYER MANA RECTANGLE

Texture2D manabar;
Vector2 posManaBar;
Rectangle recManaBar;
//REGENERATION TIME FOR MANA

int max_mana;
float currentTime = 0;


In drawGameplay:

        //PLAYER MANA
spriteBatch.Draw(backBar, posManaBar, recBackBar, Color.Gray);
spriteBatch.Draw(manabar, posManaBar, recManaBar, Color.Blue);
recManaBar.Width = max_mana;


You forgot to reset your currentTime variable. Do it like this instead:

    ////MANA REGENERATION
currentTime += gameTime.ElapsedGameTime.Milliseconds;
if (max_mana>=0 && max_mana <= 100 &&currentTime >= 9000)
{
currentTime = 0;
max_mana += 1;
}


That way, you're resetting your timer every time you add a mana point to your max_mana variable.

• To be clear, this was more of a code review thing than a game development thing. I just answered because it had a clear-cut answer (for me at least). – John Hamilton Apr 18 '17 at 16:08
• Just because a question is on-topic in one exchange, doesn't automatically make it off-topic at another exchange. In this case, the question is a clearly outlined gamedev problem, so I think you're entirely right to answer it here. (And besides, I think the code review exchange is only for evaluating & improving working code, not debugging glitches) – DMGregory Apr 18 '17 at 16:14
• @DMGregory thanks for the clarification :) I was hesitant to answer at first – John Hamilton Apr 18 '17 at 16:21
• @JohnHamilton thank you so much for answer! I cannot believe it was something so small :) And I post so much code, because I read other questions on stack overflow and stack exchange and sometimes people aren't happy if there is not shown enough of code, so I just decided to post anything what could be useful. Sorry for that! And again thanks! :) – Adrianna Kaminska Apr 18 '17 at 16:29
• Really? That was too much code @AdriannaKaminska ? There's like 11 lines! I've seen...*had* MCVE's longer than that. I've seen questions on SO that I take a look at and #Nope right out of because it's pages upon pages of code and I don't have the mental capacity to decide if its all necessary. It probably is, I just don't have the energy to figure out what it is all for some days. – Draco18s Apr 18 '17 at 18:04