Panathinaikos22 -14 Junior Poster

What the hell is wrong with XNA? i use this very simple code and it dont work

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNAMenu
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont MenuFont;
        KeyboardState KState;
        int MenuId = 0;

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


        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // Create Font
            MenuFont = Content.Load<SpriteFont>("MFont");
        }

        protected override void UnloadContent()
        {  }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState KState = Keyboard.GetState();

            // Allows the game to exit
            if (KState.IsKeyDown(Keys.Escape))
                this.Exit();

            UpdateMenu();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            UpdateMenu();
            base.Draw(gameTime);
        }

        public void UpdateMenu() 
        {
            KState = Keyboard.GetState();

            if (KState.IsKeyDown(Keys.Up))
                MenuId--;

            if (KState.IsKeyDown(Keys.Down))
                MenuId++;

            spriteBatch.Begin();
                if (MenuId == 1)
                    spriteBatch.DrawString(MenuFont, "Play", new Vector2(100, 180), Color.Green);
                else
                    spriteBatch.DrawString(MenuFont, "Play", new Vector2(100, 180), Color.White);

                if (MenuId == 2)
                    spriteBatch.DrawString(MenuFont, "Option", new Vector2(100, 250), Color.Green);
                else
                    spriteBatch.DrawString(MenuFont, "Option", new Vector2(100, 250), Color.White);

                spriteBatch.DrawString(MenuFont, "Menu ID: " + MenuId, new Vector2(10, 10), Color.Black);
            spriteBatch.End();
        }

    }
}

The problem is in this part

public void UpdateMenu() 
        {
            KState = Keyboard.GetState();

            if (KState.IsKeyDown(Keys.Up))
                MenuId--;

            if (KState.IsKeyDown(Keys.Down))
                MenuId++;

            spriteBatch.Begin();
                if (MenuId == 1)
                    spriteBatch.DrawString(MenuFont, "Play", new Vector2(100, 180), Color.Green);
                else
                    spriteBatch.DrawString(MenuFont, "Play", new Vector2(100, 180), Color.White);

                if (MenuId == 2)
                    spriteBatch.DrawString(MenuFont, "Option", new Vector2(100, 250), Color.Green);
                else
                    spriteBatch.DrawString(MenuFont, "Option", new Vector2(100, 250), Color.White);

                spriteBatch.DrawString(MenuFont, "Menu ID: " + MenuId, new Vector2(10, 10), Color.Black);
            spriteBatch.End();
        }

Code must work, the problem is:
When i press key it increase "MenuId" more than 1 time, example when i press UP menu from default 0 goes to -10 about...

it is like code run more than 10times/time ja ja

any link for this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.