Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:25:13+00:00 2026-06-18T12:25:13+00:00

I am trying to implement a simple counter in my XNA game. Thought this

  • 0

I am trying to implement a simple counter in my XNA game. Thought this would be simple enough. I have the following code:

    elapsed = gameTime.ElapsedGameTime.TotalMilliseconds;
        timer -= (int)elapsed;

        if (timer <= 0)
        {
            timer = 10;   //Reset Timer
        }

But elapsed never changes from 0.0. Am I missing something obvious here? I suspect I am. I have gameTime declared at the top and initialised as usual.

As asked, here is a bit more code:

public class Game1 : Microsoft.Xna.Framework.Game
{

private GameTime zombieTime; 



    public Game1()
    {
        zombieTime = new GameTime();
        // Other (unrelated) stuff here
    }



    protected void AddZombie()
    {
        elapsed = zombieTime.ElapsedGameTime.TotalMilliseconds;
        timer -= (int)elapsed;

        if (timer <= 0)
        {
            timer = 10;   //Reset Timer
            Zombie zombie = new Zombie(ScreenWidth, ScreenHeight, random);
            zombie.LoadContent(this.Content, "ZombieSprites/ZombieLeft1");
            zombies.Insert(0, zombie);
        }
     }

    protected void Update()
    {
        AddZombie();
        // Other game update stuff here
    }
}

I am sorry, I believed the original code snippet would have been enough. I read some pages online where people posted examples of a timer and used the method I have used above. I understand some of the comments made here about the update going fast enough so that elapsed time will always be 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:25:14+00:00Added an answer on June 18, 2026 at 12:25 pm

    You’re not using the correct GameTime. zombieTime is never updated by anything so it will always be zero’d out. The GameTime you want to use is passed into the Update() function already for you.

    The correct way to do it would be like this:

    protected void AddZombie(GameTime gameTime)
    {
        float elapsed = gameTime.ElapsedGameTime.TotalMilliseconds;
        timer -= (int)elapsed;
    
        if (timer <= 0)
        {
            timer = 10;   //Reset Timer
            // Rest of stuff goes here
        }
    }
    
    protected void Update(GameTime gameTime)
    {
        AddZombie(gameTime);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
I’m trying to implement a simple fixed headers table. I know this can in
I've been trying to implement simple (I think) thing in my app. I thought
I'm trying to implement simple Comet chat example and for this I implemented Long
I am trying to implement simple xhr abstraction, and am getting this warning when
Im trying to complete what I thought would be simple task but after several
I'm trying to implement simple Event Bus I started like this: public class RegistrationData
I'm trying to implement simple continuous collision detection for my pong game however i'm
This a conceptual question on how one would implement the following in Lisp (assuming
I am trying to implement simple paging on my sharepoint webpart. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.