Execute code at specific intervals, only once?

Posted by Mathias Lykkegaard Lorenzen on Game Development See other posts from Game Development or by Mathias Lykkegaard Lorenzen
Published on 2011-05-30T13:08:46Z Indexed on 2012/06/20 3:25 UTC
Read the original article Hit count: 333

Filed under:
|
|
|

I am having an issue with XNA, where I want to execute some code in my Update method, but only at a given interval, and only once. I would like to avoid booleans to check if I've already called it once, if possible.

My code is here:

    if ((gameTime.TotalGameTime.TotalMilliseconds % 500) == 0)
    {
        Caret.Visible = !Caret.Visible;
    }

As you may have guessed, it's for a TextBox control, to animate the caret between invisible and visible states. I just have reason to believe that it is called twice or maybe even 3 times in a single update-call, which is bad, and makes it look unstable and jumpy.

© Game Development or respective owner

Related posts about XNA

Related posts about xna-4.0