3
\$\begingroup\$

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;
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – John Hamilton Apr 18 '17 at 16:08
  • 3
    \$\begingroup\$ 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) \$\endgroup\$ – DMGregory Apr 18 '17 at 16:14
  • \$\begingroup\$ @DMGregory thanks for the clarification :) I was hesitant to answer at first \$\endgroup\$ – John Hamilton Apr 18 '17 at 16:21
  • 1
    \$\begingroup\$ @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! :) \$\endgroup\$ – Adrianna Kaminska Apr 18 '17 at 16:29
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Draco18s Apr 18 '17 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.