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;
}
}
}
}
好吧,我明白你說的話,這讓近乎完美。感覺對我來說,只有一件事我不明白或知道如何得到,我怎樣才能得到我的菜單項和偏移量的高度,我知道如何獲得菜單項的高度,但不知道如何獲得偏移量,我看到了如何設置和抵消,但是這是關於它的。非常感謝您的幫助,我非常感謝。 – Tokenfreak 2012-07-07 02:03:36
沒問題,我是我一個偏移量,就是你想要多少菜單項。 – Cyral 2012-07-07 02:26:17
此外,如果這個答案是你需要的,請點擊「檢查」按鈕接受答案。 – Cyral 2012-07-08 22:43:02