Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Xna 3.1 menu system

$
0
0
Ok i posted this before and it got shot down without discussion so i will re try by explaining it a little better,
I am not looking for code, its not homework, i am new to xna and have made a pong clone game. Just started a new project and this time i would like to start off with a menu, so to the question :

I have coded this menu system and can't see any problems with it but it will not do what i want it to do, by this i mean when it loads up i get the menu displayed, its coded with the keys A and S to scroll through the options and when it does the text should be highlighted with black text, but when the key(s) are pressed the options dont change ie the start game text remains highlighted at all times, i think the problem is in my Game1 class update method but unsure thats why i am seeking your wisdom.

{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        MenuComponent menuComponent;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            string[] menuItems = { "Start Game", "High Scores", "End Game" };
            
            menuComponent = new MenuComponent(this,spriteBatch,Content.Load<SpriteFont>("menufont"),menuItems);
            Components.Add(menuComponent);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {


            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            base.Draw(gameTime);

            spriteBatch.End();

            
        }
    }
}

and my class

{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MenuComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {

        string[] menuItems;
        int selectedIndex;
        Color normal = Color.White;
        Color hilite = Color.Black;
        KeyboardState keyboardState;
        KeyboardState oldKeyboardState;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        Vector2 position;
        float width = 0f;
        float height = 0f;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = value;
                if (selectedIndex < 0)
                    selectedIndex = 0;
                if (selectedIndex >= menuItems.Length)
                    selectedIndex = menuItems.Length - 1;
            }
        }

        public MenuComponent(Game game,SpriteBatch spriteBatch,SpriteFont spriteFont,string[] menuItems)
            : base(game)
        {
            // TODO: Construct any child components here
            this.spriteBatch = spriteBatch;
            this.spriteFont = spriteFont;
            this.menuItems = menuItems;
            MeasureMenu();
        }

        private void MeasureMenu()
        {
            height = 0;
            width = 0;
            foreach (string item in menuItems)
            {
                Vector2 size = spriteFont.MeasureString(item);
                if (size.X > width)
                    width = size.X;
                height += spriteFont.LineSpacing + 5;
            }
            position = new Vector2(
            (Game.window.ClientBounds.Width - width) / 2,
            (Game.window.ClientBounds.Height - height) / 2);
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        private bool CheckKey(Keys theKey)
        {
            return keyboardState.IsKeyUp(theKey) && oldKeyboardState.IsKeyDown(theKey);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            keyboardState = Keyboard.GetState();

            if (CheckKey(Keys.A))
            {
                selectedIndex++;

                if (selectedIndex == menuItems.Length)
                {
                    selectedIndex = 0;
                }
                if (CheckKey(Keys.S))
                {
                    selectedIndex--;
                }
                if (selectedIndex < 0)
                {
                    selectedIndex = menuItems.Length - 1;
                }

                base.Update(gameTime);

                oldKeyboardState = keyboardState;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            Vector2 location = position;
            Color tint;

            for (int count = 0; count < menuItems.Length; count++)
            {

                if (count == selectedIndex)
                {
                    tint = hilite;
      
                }
                else
                {
                    tint = normal;
    
                }

                //spriteBatch.DrawString(spriteFont, menuItems[count], location, tint);
                

                location.Y += spriteFont.LineSpacing + 50;
                spriteBatch.DrawString(spriteFont, menuItems[count], location, tint);
            }
            
            
        }
    }
}



Thank you for any help, i am stumped it makes sense to me.

Viewing all articles
Browse latest Browse all 51036

Trending Articles