1

メニューの上にマウスを置くと問題が発生します。マウスを上に置いたときにさまざまなメニューオプションを強調表示しようとしていますが、何が間違っているのかわかりません。マウスをそれらの上に移動すると、それらすべてを非常に高速に循環するだけではありません。どんな助けでも大歓迎です。問題は、更新機能またはメジャーメニュー機能のいずれかにあります。ちなみに、キーボード部分は問題なく動作しています。

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;
            }
        }
    }
}
4

1 に答える 1

1
if (area.Contains(mouseLocation))
            {
                selectedIndex++;
                if (selectedIndex == menuItems.Length)
                    selectedIndex = 0;
                if (selectedIndex < 0)
                    selectedIndex = menuItems.Length - 1;

            }

長方形のいずれかにカーソルを合わせている場合、そのコードは selectedIndex をもう 1 つ作成します。これは、それぞれの循環を説明できます。

メニュー項目ごとに四角形を確認し、selectedIndex を適切に設定する必要があります。

疑似コード:

for (int i = 0; i<= menuItems.Count;i++) { Rectangle rect = new Rectangle(position.X,position.Y + (i * heightOfMenuItemAndOffset),Width,Height);

マウス.GetState(); Point mouseLocation = new Point(mouseState.X, mouseState.Y); if (area.Contains(mouseLocation) { selectedIndex = i; }

}

その線に沿った何か。

于 2012-07-06T23:56:00.377 に答える