Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

XNA: How to create a timer using GameTime?

Name: Anonymous 2010-06-16 8:15

[/code]
/--------------------------------------------------------------
/--------------------------DISCLAIMER--------------------------
/This code is in C# using the XNA Framework, if you have no /clue what im talking about, please take your SICP to other /threads, theres a reason im using a greasemonkey scripts to /block the word SICP
/--------------------------------------------------------------
[/code]

So in my pong remake, i want to make a timer to show when to reset the ball after the player scores.

Using GameTime, how would i go about making a timer readable by other code

for example:

Here is when the ball will be reset


while (resetTimer <= 0)
            {
                ballVelocity = new Vector2(10, 10);
            }


This is saying whenever the "resetTimer" reaches 0 seconds, the ball will begin to move again.


I understand that ElapsedGameTime means the amount of time between frames, but is there any way for my "resetTimer" to be subtracted by one every second? (in this case, "resetTimer" is declared an int with the value of 3)

Name: Anonymous 2010-06-16 11:58

Well here's one way of doing it

//set this variable somewhere to initialize and start the timer
double mark = gameTime.ElapsedGameTime.TotalMilliseconds;

//for each frame you check if a second have passed
if (gameTime.ElapsedGameTime.TotalMilliseconds - mark >= 1000)
{
    //if it has we change mark to reflect this
    mark += 1000;
    //and decrement the resetTimer
    resetTimer--;
}

Modify to suit your needs, I hope you get the general idea.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List