【问题标题】:Mouse hovering over menu options鼠标悬停在菜单选项上
【发布时间】:2023-03-31 01:05:01
【问题描述】:

我的鼠标悬停在菜单上时遇到问题。当鼠标悬停在它们上方时,我试图突出显示各种菜单选项,但不确定我做错了什么。不是的,当我将鼠标移到它们上方时,它会非常快速地循环它们。任何帮助将不胜感激。问题出在更新功能或测量菜单功能中。顺便说一句,我的键盘部分工作正常。

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 Blocks
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MenuComponents : Microsoft.Xna.Framework.DrawableGameComponent
    {
        string[] menuItems;
        int selectedIndex;

        Color normal = Color.White;
        Color hilite = Color.Yellow;

        KeyboardState keyboardState;
        KeyboardState oldKeyboardState;

        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        Vector2 position,size;
        float width = 0f;
        float height = 0f;

        MouseState mouseState;
        Rectangle area;


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

        public MenuComponents(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)
            {
                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) + 50);

            int positionX = (int) position.X;
            int positionY = (int) position.Y;
            int areaWidth = (int) width;
            int areaHeight = (int) height;


            //for (int i = 0; i < area.Length; i++)
            //{
                area = new Rectangle(positionX, positionY, areaWidth, areaHeight);
            //}
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        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();
            mouseState = Mouse.GetState();

            Point mouseLocation = new Point(mouseState.X, mouseState.Y);

            if (CheckKey(Keys.Down))
            {
                selectedIndex++;
                if (selectedIndex == menuItems.Length)
                    selectedIndex = 0;
            }
            if (CheckKey(Keys.Up))
            {
                selectedIndex--;
                if (selectedIndex < 0)
                    selectedIndex = menuItems.Length - 1;
            }
            if (area.Contains(mouseLocation))
            {
                selectedIndex++;
                if (selectedIndex == menuItems.Length)
                    selectedIndex = 0;
                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 i = 0; i < menuItems.Length; i++)
            {
                if (i == selectedIndex)
                    tint = hilite;
                else
                    tint = normal;

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

【问题讨论】:

    标签: c# xna


    【解决方案1】:
    if (area.Contains(mouseLocation))
                {
                    selectedIndex++;
                    if (selectedIndex == menuItems.Length)
                        selectedIndex = 0;
                    if (selectedIndex < 0)
                        selectedIndex = menuItems.Length - 1;
    
                }
    

    如果您将鼠标悬停在任何矩形上,该代码会使 selectedIndex 再增加一个。 这可以解释每一个循环。

    您需要为每个菜单项检查一个矩形,并适当地设置 selectedIndex。

    伪代码:

    for (int i = 0; i

    Mouse.GetState(); 点 m​​ouseLocation = new Point(mouseState.X, mouseState.Y); if (area.Contains(mouseLocation) { 选定索引 = i; }

    }

    类似的东西。

    【讨论】:

    • 好的,我明白你在说什么,这对我来说几乎是完全合理的。只有一件事我不明白或不知道如何得到。如何获取菜单项的高度和偏移量。我知道如何获取菜单项的高度,但不确定如何获取偏移量。我知道如何设置和偏移,但仅此而已。非常感谢您迄今为止的帮助,我真的很感激。
    • 没问题,我所说的偏移量,就是你想要的菜单项相距多远。
    • 另外,如果您需要这个答案,请点击“检查”按钮将其作为答案。
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多