0
\$\begingroup\$

I have a splashscreen object that waits for a user input before loading the next screen, but I would also like the next screen to load after a certain amount of time has passed. I tried adding a check to see if the elapsed game time is greater than or equal to a set amount of time in seconds. The user input still changes the screen, but the screen will not change after that time requirement has been met. What am I doing wrong?Here is my code.

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to stack overflow. Please post code as text rather than screenshots. This makes it easier to read and answer the question. \$\endgroup\$ – craftworkgames Nov 26 '17 at 23:22
1
\$\begingroup\$

I figured it out. gameTime.ElapsedGameTime is a TimeSpan that refers to the amount of time that has passed since the last update, which is a fraction of a second. By using gameTime.TotalGameTime, I was able to get the total amount of time that has passed since the program started running. Also, for some reason I can't explain, using > in when comparing gameTime.TotalGameTime.Seconds to autoChangeTime.Seconds will not work. It only worked when I used == to compare them, which is fine because there are quite a few Update cycles where gameTime.TotalGameTime.Seconds is equal to autoChangeTime.Seconds.Here is my new code.

\$\endgroup\$
0
\$\begingroup\$

The problem is, if you use == and if update() is delayed for over a second when it reaches changeTime, then it wont ever be true. I cannot see why > would not work either they are both Ints? Have you tried nesting the two parts of the if statement to see if it works on its own after you have checked for the key presses?

\$\endgroup\$
  • \$\begingroup\$ If I nest the statements, the screen will only change after an input has been been detected. I would like the screen to change without any user input as well. Edit: I removed the Key press check entirely, and it does not work with >=. \$\endgroup\$ – Ryan Ennis Nov 27 '17 at 20:01
  • \$\begingroup\$ I see the issue with using ==, but is it likely that there would ever be a situation where the game is running less than 1 update cycle per second? \$\endgroup\$ – Ryan Ennis Nov 27 '17 at 20:05
  • \$\begingroup\$ It can happen but very unlikely and most people will bang the keyboard if they wait too long anyway, so there shouldn't be a problem. All I can suggest about the >= is to double check the values you are comparing(gameTime & autoChangeTime) either by putting in a break point at the check or just before it you can also Console.Write them to check them. \$\endgroup\$ – Neil Nov 27 '17 at 23:10
  • \$\begingroup\$ So I nested that if statement in another if statement that goes like this: if(gameTime.TotalGameTime.Seconds >= MinChangeTime) where MinChangeTime is an int. This blocks any user input until the time requirement is met. For some reason, greater than or equal to works in this case, @Neil. \$\endgroup\$ – Ryan Ennis Nov 28 '17 at 16:37
0
\$\begingroup\$

You are correct, if you simply set it (=) it will only ever be equal to the time since the last update call. If add that time each iteration however, you'll create the timer you originally needed.

 switch (currentState)
        {
            case States.otherStuff:
                timer = 0;
                if (!condition)
                    currentState = States.splashscreen;
                break;
            case States.splashscreen:
                timer += gameTime.ElapsedGameTime.TotalSeconds;
                if (condition || timer >= maxWaitTime)
                    currentState = States.otherStuff;
                break;
            default:
                break;
        }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.