XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

game time doesnt work?

Last post 09/06/2009 15:42 by dadoo Games. 2 replies.
  • 09/06/2009 15:32

    game time doesnt work?

    hi all
    im new to xna
    so im just writing on my first game.
    so for now i made a "bomb" class, wichs draws my bomb and checks for collision between the bomb and a jeep.
    if there is one, the current bombstate changes to "jeeephit"

    //update
               if (aCurrentbombstate == bombstate.jeephit)
                {
                    if (!justonce)
                    {
                        thetime = (float)theGameTime.ElapsedGameTime.TotalSeconds;
                        justonce = true;
                    }
                    showntime = (float)theGameTime.ElapsedGameTime.TotalSeconds - thetime;

                    propertime = Convert.ToInt32(showntime);
    ...

    //draw
                if (aCurrentbombstate == bombstate.hit)
                {
                    if (propertime <= 3)
                    {
                        base.mSpriteTexture = wragtexture;
                        base.Scale = 0.8f;
                        base.Source = new Rectangle(0, 0, wragtexture.Width, wragtexture.Height);
                        base.Draw(theSpriteBatch);
                    }
                }

    and displays , on the last known vector2 of the bomb, a "damaged jeep". 3 seconds later the damaged jeep should disappear, but he doesn't

    can someone please help me, i dont want the damaged jeep to stay there for ever!!

  • 09/06/2009 15:41 In reply to

    Re: game time doesnt work?

    Note that GameTime.ElapsedGameTime contains the time since the last call to Update. So at 60 frames per second, this time will be 1/60sec long, or about 0.016666sec. This value will be the same for each call to Update (for a fixed framerate game that is). So your "thetime" is set to 0.016666, then showntime is set to 0.016666 - thetime, i.e. to 0. That you convert to an int to assign to propertime. So at the end, propertime is always 0.

    Use GameTime.TotalGameTime instead, for your purposes.

    Or even better, do something like this: Ditch "showntime" and "propertime", and only use "thetime":

    // update:
    if (!justonce) {
        thetime = 0;
        justonce = true;
    }
    thetime += (float)theGameTime.ElapsedGameTime.TotalSeconds.

    // draw:

    if (aCurrentbombstate == bombstate.hit) {
      if (thetime <= 3.0) {
        // bla bla
      }
    }


    Doc
    Please consider playtesting my game: Your Doodles Are Bugged!

    Twitter - My Game Trailers - www.spyn-doctor.de - Games: Kuchibi, Golden Tangram

    Useful for peer reviews and testing your own game: My little "evil" checklist for peer review stress testing
  • 09/06/2009 15:42 In reply to

    Re: game time doesnt work?

    GameTime.ElapsedGameTime is the time that has passed since the last update. If you are waiting for it to get really big, it won't. You might be looking for GameTime.TotalGameTime, which is the time since the game started. If you want to take some action after a set amount of time you can either keep a running total (i.e. add gameTime.ElapsedGameTime to a member variable on each update) or look into using Timers or the Stopwatch class.
    www.dadoogames.com
    Curling 2010 - in playtest soon, this month or next, this year for sure (maybe)
Page 1 of 1 (3 items) Previous Next