1
\$\begingroup\$

I'm trying to delay the time it takes for the main menu screen to pop up after a user has won / lost a match. As it stands, the game immediately displays a message stating "you won / lost" and waits for 6 seconds before loading the menu screen.

I would also like players to have the ability to press a key to advance to the menu screen immediately but thus far my switch statement doesn't seem to do the trick. I've included the switch statement, along with my (theoretical) inputs. What could I be doing wrong here?

if (gamestate == GameStates.End)
                    switch (input.IsMenuDown(ControllingPlayer))
                    {
                        case true:
                            ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen 
                            break;

                        case false:
                            if (screenLoadDelay > 0)
                            {
                                screenLoadDelay -= gameTime.ElapsedGameTime.TotalSeconds;
                            }
                            ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen 
                            break;
                    }

     /// <summary>
    /// Checks for a "menu down" input action.
    /// The controllingPlayer parameter specifies which player to read
    /// input for. If this is null, it will accept input from any player.
    /// </summary>
    public bool IsMenuDown(PlayerIndex? controllingPlayer)
    {
        PlayerIndex playerIndex;

        return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex)
               || IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex)
               || IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
    }
\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

(I'm not familiar with XNA but AFAIK there's nothing like an input listener so I'm assuming you are using flags to represent input)

With input flags (that could be as simple as booleans) it makes more sense to check for them at the start of the games' main loop. That way, for each cycle you know any current input.

Something like this should then work:

set Delay=0;

main loop

{

[...]

Gather input flags.

if(gamestate == GameStates.End) then

 if (Desired_Flag == true || Delay > 6)

  Delay = 0; ShowMenu();

 else Delay += Last_TimeSlice

else KeepPlaying //the game is not in .End state

[...]

Last_TimeSlice = current_elapsed;

}
\$\endgroup\$
1
\$\begingroup\$

Treat your message dialog as just another "enemy" or "npc" object that has animations. Think of it as having an animation that lasts 6 seconds.

MessageDialog message = new MessageDialog();

public override Update(GameTime gameTime)
{
    if(gamestate == PLAYING)
    {
        // check game win or lose
        if(player_now_wins_or_lose)
        { 
            message.show("You Win or Lose!");
            gamestate == PLAYER_WIN_OR_LOSE;
        }
        else
        {
            // TODO: Run the game logic
            RunGameLogic();
        }
    }
    else if(gamestate == PLAYER_WIN_OR_LOSE)
    {
        // update the animations in the message dialog.
        // this can be some fade-in/out; icon spinning; text shimmering effects
        // on the message box that completes in 6 secs.
        message.Update(gameTime);

        if(Keyboard.GetState() == SOME_KEY_PRESSED || message.state == SIX_SECONDS_UP)
        {
           message.hide();
           gamestate == MAIN_MENU;
           // TODO: Add main menu screen
        }
    }
}

The MessageDialog

public class MessageDialog
{
    public MsgDialogState state = IDLE;
    float totalTime = 0;
    string TextToShow = string.Empty;

    void Show(string text)
    {
       TextToShow = text;
       state = RUNNING;
       totalTime = 0;
    }

    void Update(GameTime gametime)
    {
       totalTime += gametime.elapsedTime;
       if(totalTime >= 6000)
       { 
           totalTime = 6000;
           state = SIX_SECONDS_UP;
       }
    }

    void Draw()
    {
       // TODO: Draw the message box with the text;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Thank you guy for taking the time to respond. Collectively, your answers have helped me understand the problem better. In the end, the solution was pretty simple, and I used this code: \$\endgroup\$ Commented Jun 3, 2012 at 12:20
  • \$\begingroup\$ Well I'd write the code, but I realized that I have no idea of how to insert it using mini-Markdown. if I use code it looks identical to me not using nothing at all. \$\endgroup\$ Commented Jun 3, 2012 at 12:20
0
\$\begingroup\$

For starters, you might consider just making your switch statement an if/else statement since it's only checking for true or false. Also, correct me if I'm wrong, but it looks like you're trying to do the delay if the switch is false. The problem is that after your if statement completes it still executes the next line.

ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen

You may instead try this:

if (screenLoadDelay > 0)
    screenLoadDelay -= gameTime.ElapsedGameTime.TotalSeconds;
else
    ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen
\$\endgroup\$
2
  • \$\begingroup\$ I originally had it as an if / else statement, but wasn't sure if it would work correctly that way. Looks like I'll be going back to that now. I'll implement this now and let you know how it comes out. Thanks again for taking the time to assist me, I appreciate it. I see what you mean about it going straight from one statement to the next now. \$\endgroup\$ Commented Jun 2, 2012 at 0:52
  • \$\begingroup\$ Looking at the code again, I realize that it is what I originally had (nothing that I posted here), but what I'm trying to do now is make it so that players have the option to press a key and advance the screen, without having to wait those 6 seconds. \$\endgroup\$ Commented Jun 2, 2012 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .