2

I have a problem with my game. It is divided in a two states: GameState.MainMenu and GameState.Playing. I want to draw on screen the timer when I playing. I use gameTime.TotalGameTime.Minutesand gameTime.TotalGameTime.Seconds variable. But when I clicked on button Play Now, and my game switched state from GameState.MainMenu to GameState.Playing., the timer dosent start from 0. It starts with the time, which elapsed when I spend in MainMenu. I try create next variable to count time, which I spent in MainMenu, and I try to subtract from the first variable, but the displaying time is not properly.

My substracting time:

minutesPlaying = gameTime.TotalGameTime.Minutes; ;
secondsPlaying = gameTime.TotalGameTime.Seconds;

switch (currentGameState)
{
    case GameState.MainMenu:

        minutesMenu = gameTime.TotalGameTime.Minutes;
        secondsMenu = gameTime.TotalGameTime.Seconds;



        if (btnPlay.isTapped == true) 
        { 
            currentGameState = GameState.Playing;
            soundEffectInstance.Stop();
        }
        btnPlay.Update(collection);
        break;

    case GameState.Playing:

        minutesTotal = minutesPlaying - minutesMenu;
        secondsTotal = secondsPlaying - secondsMenu;

        break;
}

Invoke my method:

timer.Update(minutesTotal, secondsTotal);

Update method:

public void Update(int min, int sec)
{
    string seconds, minutes;
    seconds = sec.ToString();
    minutes = min.ToString();

    if (sec <= 9) seconds = "0" + seconds;
    if (min <= 9) minutes = "0" + minutes;

    nowString = minutes + ":" + seconds;
}

Thanks for answer :)

Cyral
  • 13,999
  • 6
  • 50
  • 90
lukas22ew
  • 21
  • 4

4 Answers4

0

I would use a 'StopWatch' to 'Start' and 'Pause' the tracking of time spent in your playing state. When the playing state is entered start the 'StopWatch' and then pause it when leaving that state.

borrillis
  • 656
  • 4
  • 7
0

Your on the right track, but I think you should have a time passed variable instead of having to subtract it from the original gameTime, this way if you go back to the menu, then back into the game, your method wont be broken. Basicly it just takes a TimeSpan at 0 and adds gameTime to it as time passes, giving you the amount of time spent in the level.

First off add a new TimeSpan

public TimeSpan TimePassed;

Now in your Update() method inside case GameState.Playing: you will need to incriment the timer.

TimePassed -= gameTime.ElapsedGameTime;

You can also reset the timer to Zero if you need to make a new level (If that applies, Ex: Replaying a level will need a timer reset)

Now to render the time in your Draw() method.

string timeString = TimePassed.Minutes.ToString("00") + ":" + TimePassed.Seconds.ToString("00");
spriteBatch.DrawString(FONT,timeString,POSITION,Color.White);

And there you go!

Cyral
  • 13,999
  • 6
  • 50
  • 90
0

You should modify time only in Playing state, I have modified the code to achieve it, whithout too much changes... PlayTime keeps the total time elapsed in the playing state.

    TimeSpan Elapsed ;

    switch (currentGameState)
    {
        case GameState.MainMenu:

            if (btnPlay.isTapped == true) 
            { 
                currentGameState = GameState.Playing;
                soundEffectInstance.Stop();
            }
            btnPlay.Update(collection);
            Elapsed = TimeSpan.Zero;
            break;

        case GameState.Playing:

            Elapsed = gametime.ElapsedGameTime;
            break;
    }

Invoke my method:

    timer.Update(Elapsed);

Update method:

TimeSpan PlayTime = TimeSpan.Zero;
public void Update(TimeSpan Elapsed)
{
    PlayTime.Add(Elapsed);

    string seconds, minutes;
    seconds = PlayTime.Seconds.ToString();
    minutes = PlayTime.Minutes.ToString();

    if (sec <= 9) seconds = "0" + seconds;
    if (min <= 9) minutes = "0" + minutes;

    nowString = minutes + ":" + seconds;
}
Blau
  • 5,742
  • 1
  • 18
  • 27
  • Thanks Blau, but it doesn't work. When I replace the code and run the game, the timer still indicate 00:00 – lukas22ew May 24 '13 at 21:58
  • you should debug the code and check if it behaves as expected. Are you calling to timer.Upadte with the right Elapsed time? – Blau May 27 '13 at 01:36
0

Try this:

Before the game starts you don't need to keep track of the time, so I have set minutesPlaying and secondsPlaying to 0 in GameState.MainMenu. Don't initialize minutesPlaying and secondsPlaying within the main Update() function.

minutesPlaying = 0;
secondsPlaying = 0;

switch (currentGameState)
{
    case GameState.MainMenu:

        if (btnPlay.isTapped == true) 
        { 
           currentGameState = GameState.Playing;
           soundEffectInstance.Stop();
        }
        btnPlay.Update(collection);
        break;

    case GameState.Playing:

        secondsPlaying += gameTime.ElapsedGameTime.Seconds;
        if (secondsPlaying >= 60)
        {
            minutesPlaying++;
            secondsPlaying = 0;
        }
        break;
}

When you display the time, just display minutesPlaying and secondsPlaying. You don't need any extra variables.

If that doesn't work, you can always keep track of the milliseconds (secondsPlaying += gameTime.ElapsedGameTime.Milliseconds) that pass when the game starts and divide the value by 1000 to display the seconds.