Help with menu selection in XNA

Started by
12 comments, last by moneal2001 11 years ago

We can PM if you like.

I sent you a PM. I changed the code to this:


        //Menu selections
        private void MainMenuSelection()
        {

            KeyboardState currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            // If player presses down arrow, select credits
            if (currentKeyboardState.IsKeyUp(Keys.Down) && previousKeyboardState.IsKeyDown(Keys.Down) && playButton.active == true)
            {
                playButton.active = false;
                creditsButton.active = true;
            }

            // If player presses down again, highlight exit
            if (currentKeyboardState.IsKeyUp(Keys.Down) && previousKeyboardState.IsKeyDown(Keys.Down) && creditsButton.active == true)
            {
                creditsButton.active = false;
                exitButton.active = true;
            }

            previousKeyboardState = currentKeyboardState;

It still scrolls through the credits right down to the exit. I am pretty sure it is how and where I am calling the current and previous keyboard states

Cpl Alt, Travis A

USMC

Advertisement

you are asking if the down key is down both times back to back, before assigning currentKeyboardState to previousKeyboardState. so yes it will jump all the way down to the exit button because you are asking about the same keyboardstate both times. so both will be activated during the same Update. you should set up the menu items in an array or list have an indexer that moves up or down one with each press. also move your keyboardstate declerations out of the method. like this.


int menuIndex = 0;  // place this in your menu fields
menuItem[] menuItems; //place this in your menu fields
KeyboardState currentKeyboardState; //place this in your menu fields
KeybardState previousKeyboardState; //place this in your menu fields

private void MainMenuSelection()
        {            
            currentKeyboardState = Keyboard.GetState();
            
            menuItems[index].IsActive = true;

            // If player presses down arrow go down in menu
            if (currentKeyboardState.IsKeyDown(Keys.Down) && previousKeyboardState.IsKeyUp(Keys.Down))
            {
                   //deactivate current menuItem
                   menuItems[menuIndex].IsActive = false;
 
                   //go down in the menu
                   menuIndex++;
                   
                   // keep index within the your menu items
                   if(menuIndex <= menuItems.Count)
                        index = 0;

                   //activate new menuItem
                   menuItems[index].IsActive = true;

            }

            // If player presses Up Go Up in menu
            if (currentKeyboardState.IsKeyDown(Keys.Up) && previousKeyboardState.IsKeyUp(Keys.Up))
            {
               //deactivate current menuItem
               menuItems[menuIndex].IsActive = false;

              //go up in the menu
              menuIndex--;

              // goes to last menuItem if already at top of menu
              if(menuIndex > 0)
                 index = menuItems.Count -1;

              //activate new menuItem
              menuItems[index].IsActive = true;
            }

            previousKeyboardState = currentKeyboardState;

you should set up the menu items in an array

Would this work even though I have the two different textures for each button in a List?

My Button Class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace TronPongClone
{
    class Buttons
    {
        // Button texture at Idle
        public List<Texture2D> texture;

        // Button height
        public int height;

        // Button width
        public int width;

        // Button position
        public Vector2 poition;

        // Button state
        public bool active;

        public Buttons()
        {
            texture = new List<Texture2D>();
            height = 100;
            width = 300;
            poition = Vector2.Zero;
            this.active = true;
        }

        public void Update(SpriteBatch spriteBatch)
        {
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (active == true)
            {
                spriteBatch.Draw(texture.ElementAt(1), poition, Color.White);
            }
            if (active == false)
            {
                spriteBatch.Draw(texture.ElementAt(0), poition, Color.White);
            }
        }
    }
}

Cpl Alt, Travis A

USMC

Yea that should work. i would create a list since you can add and remove from lists at will(adding or removing any number of buttons you need as you go). and put something together kinda like my example and it should work just fine. the button class you have looks set up pretty well.

there are some great examples of how to do a menu system out there for xna. a good one is at xnagpa, in his rpg tutorial(the one i think i modeled the example after if i remember right). he also has a lot of menu control items. and as someone mentioned earlier in the thread at the app hub resources the Game State Management Tutorial has a good menu and input handling system in it.

This topic is closed to new replies.

Advertisement