Tutorial 4.3: Navigating The Menus With Input
The final thing we need to do to make our menus work is to make our input device control them.

We will be making alot of changes to our update method, so I will post the whole thing and explain it using comments.
protected override void Update(GameTime gameTime)
        if ((XInput.Device(1).KBState(1).IsKeyDown(Keys.Escape) // If the escape key is pressed...
                && !XInput.Device(1).KBState(0).IsKeyDown(Keys.Escape))) // And it wasn't pressed last frame...
                XGameState.TogglePaused(); // Pause the game...
                XMenus.ToggleMenu("Pause Menu"); // And bring up the main menu
                // Note: we checked if the button was pushed last frame so that we can avoid the button
                // push being recognized several times if it was held down

        if (XGameState.Paused != true) // If the game is not paused use the input as normal
                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Up))
                        XScene.Actor("ShipActor").Rotation.X += .75f;
                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Down))
                        XScene.Actor("ShipActor").Rotation.X -= .75f;
                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Left))
                        XScene.Actor("ShipActor").Rotation.Y -= .75f;
                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Right))
                        XScene.Actor("ShipActor").Rotation.Y += .75f;
        else // If the game is paused, use the input for the menus
                string ChosenItem = null;

                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Up)
                && !XInput.Device(1).KBState(0).IsKeyDown(Keys.Up))
                        XMenus.SelectPreviousMenuItem(); // Selects the previous menu item for the open menu
                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Down)
                && !XInput.Device(1).KBState(0).IsKeyDown(Keys.Down))
                if (XInput.Device(1).KBState(1).IsKeyDown(Keys.Enter)
                && !XInput.Device(1).KBState(0).IsKeyDown(Keys.Enter))
                        ChosenItem = XMenus.ChooseMenuItem(); // This selects the currently highlighted button

                // Now we will do certain things based on what button was chosen
                if (ChosenItem == "Start")
                else if (ChosenItem == "Quit")
                else if (ChosenItem == "QuitToMain")
                        XMenus.ToggleMenu("Main Menu");
                else if (ChosenItem == "Fullscreen")
                        XGameState.ToggleFullScreen(); // Toggle Fullscreen
                        graphics.PreferredBackBufferWidth = 1280; // Set the render area width
                        graphics.PreferredBackBufferHeight = 800; // Set the render area height
                        graphics.ApplyChanges(); // Apply the changes

                        SetMenuStyles(); // Because our window size has changed, we need to recreate our styles
                        XMenus.Menu("Main Menu").UpdateStyles(MainMenuStyle, ButtonStyle); // Update the menus
                        XMenus.Menu("Pause Menu").UpdateStyles(PauseMenuStyle, ButtonStyle);


You now have fully functional menus in your game, and you can easily expand on them from here.

Tutorial4a.jpg Tutorial4b.jpg

You can download the source code for this tutorial here: Tutorial4.zip

Last edited Mar 23, 2008 at 8:32 AM by Hazardous101, version 4


No comments yet.