所有菜单项都可以选择

时间:2015-09-30 14:23:29

标签: c# menu xna position

我正在尝试创建一个简单的菜单,所选的字符串会根据指示进行更改颜色,我似乎无法将Y轴上字符串列表中的每个项目分开,目前它们都是位于屏幕中间,但屏幕顶部相互重叠。我知道这可能是一个非常简单的修复,但我是XNA的新手。任何帮助将非常感激。提前致谢。

菜单管理类

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

namespace Game2
{
    class MenuManagement 
    {
        KeyboardState keyboard;
        KeyboardState prevKey;

        MouseState mouse;

        Vector2 position;

        List<String> buttonList = new List<string>();
        SpriteFont spriteFont;
        int selected = 0; 

        public MenuManagement ()
        {

            buttonList.Add("Play");
            buttonList.Add("Options");
            buttonList.Add("instructions");
            buttonList.Add("Exit");

        }


     private void MeasureMenu()
    {
        height = 0;
        width = 0;

         foreach (string item in buttonList)
         {
             Vector2 size = spriteFont.MeasureString(item);
             height += spriteFont.LineSpacing + 5;
         }
    }

        public void LoadContent (ContentManager Content )
        {
            spriteFont = Content.Load<SpriteFont>("spriteFont");          

        }

        public void Update (GameTime theGameTime)
        {

            keyboard = Keyboard.GetState();

            if (checkKeyBoard(Keys.Up))
            {
                if (selected > 0)
                {
                    selected--;
                }
            }

            if (checkKeyBoard(Keys.Down))
            {
                if (selected < buttonList.Count - 1)
                {
                    selected++;
                }
            }

            prevKey = keyboard;

        }

        public bool checkKeyBoard (Keys key)
        {
            return (keyboard.IsKeyDown(key) && prevKey.IsKeyDown(key)); 
        }

        public void Draw (SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Begin();
            Color color;


            for (int i = 0; i < buttonList.Count; i++)
            {
                Vector2 location = position;
                if (i == selected)
                {
                    color = Color.Yellow;
                }
                else
                {
                    color = Color.Blue;
                }


        location.Y += spriteFont.LineSpacing + 5;
    }
           theSpriteBatch.DrawString(spriteFont, buttonList[0], new Vector2 (300, 100), color);
    theSpriteBatch.DrawString(spriteFont, buttonList[1], new Vector2(300, 150), color);
    theSpriteBatch.DrawString(spriteFont, buttonList[2], new Vector2(300, 200), color);
    theSpriteBatch.DrawString(spriteFont, buttonList[3], new Vector2(300, 250), color);
    theSpriteBatch.End();

        }

    }


}

的Game1

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game2
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch theSpriteBatch;
        public static Rectangle screen;
        public static string GameState = "Menu";
        MenuManagement menuManagement;


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


        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            menuManagement = new MenuManagement();
            screen = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            base.Initialize();
        }


        protected override void LoadContent()
        {

            theSpriteBatch = new SpriteBatch(GraphicsDevice);
            menuManagement.LoadContent(Content);

            // TODO: use this.Content to load your game content here
        }

            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }

 // <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();



 switch (GameState)
        {
            case "Menu":
                menuManagement.Update(gameTime);
                break;
        }
        base.Update(gameTime);
    }

    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {

        switch (GameState)
        {
            case "Menu":
                menuManagement.Draw(theSpriteBatch);
                break;
        }
        base.Draw(gameTime);

    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以计算字符串的长度,并将下一个项目放在您想要的位置。你应该看看SpriteFont.MeasureString()。

https://msdn.microsoft.com/en-us/library/bb464128(v=xnagamestudio.30).aspx

要清楚,它们重叠的原因是因为你把它们放在同一个位置:

Vector2 location = position; 

然后:

new Vector2 (300, location.Y)

是您绘制整个批次的地方。您需要更改每个菜单项的位置。最简单的解决方法是增加每个项目的位置的Y值。这会将下一个菜单项移到底部。增加X向右移动。