[英]C# mouse picking XNA
我有一個游戲菜單,(現在)有1張圖片(按鈕)。 這是一個texture2D,我把它放在一個數組中。 我想知道鼠標何時懸停在我的圖片上。 Actionscript具有一個稱為“ hitTestObject”的內置函數。 但是開始看起來我必須檢查圖像的每個像素,看看鼠標是否在那里。 我願意改變一切,我只想能夠選擇不同的圖片。
Texture2D[] clickable_objects = new Texture2D[1];
clickable_objects[0] = Content.Load<Texture2D>("brain-icon");
public bool Intersects(Vector2 mouse_loc, Texture2D[] _objects)
{
int X = (int) mouse_loc.X;
int Y = (int) mouse_loc.Y;
if () //Mouse hovers over object[0]
return true;
else
return false;
}
Texture2D只是圖像的一種表示形式-它只有2D的紋理像素網格。 它在屏幕上沒有位置,因此您無法對其進行鼠標點擊檢查。
您將需要一些包含類(例如Sprite)的類,該類同時包含紋理和位置。 然后,您可以向該類添加一個hittest()函數,該函數將檢查紋理的位置和大小。
或者更好的辦法是,找到一些現有的精靈庫供XNA使用。 我敢肯定有一些可以為您提供此功能。
您如何添加這樣的簡單內容:
public delegate void ButtonEvent(Button sender);
public class Button
{
public Vector2 Position { get; set; }
public int Width
{
get
{
return _texture.Width;
}
}
public int Height
{
get
{
return _texture.Height;
}
}
public bool IsMouseOver { get; private set; }
public event ButtonEvent OnClick;
public event ButtonEvent OnMouseEnter;
public event ButtonEvent OnMouseLeave;
Texture2D _texture;
MouseState _previousState;
public Button(Texture2D texture, Vector2 position)
{
_texture = texture;
this.Position = position;
_previousState = Mouse.GetState();
}
public Button(Texture2D texture) : this(texture, Vector2.Zero) { }
public void Update(MouseState mouseState)
{
Rectangle buttonRect = new Rectangle((int)this.Position.X, (int)this.Position.Y, this.Width, this.Height);
Point mousePoint = new Point(mouseState.X, mouseState.Y);
Point previousPoint = new Point(_previousState.X, _previousState.Y);
this.IsMouseOver = false;
if (buttonRect.Contains(mousePoint))
{
this.IsMouseOver = true;
if (!buttonRect.Contains(previousPoint))
if (OnMouseEnter != null)
OnMouseEnter(this);
if (_previousState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed)
if (OnClick != null)
OnClick(this);
}
else if (buttonRect.Contains(previousPoint))
{
if (OnMouseLeave != null)
OnMouseLeave(this);
}
_previousState = mouseState;
}
public void Draw(SpriteBatch spriteBatch)
{
//spritebatch has to be started! (.Begin() already called)
spriteBatch.Draw(_texture, Position, Color.White);
}
}
要使用它,您需要在某處提供參考
Button _button;
在您的LoadContent
,您可以執行類似的操作
button = new Button(Content.Load<Texture2D>("Textures\\Button"), new Vector2(100, 100));
button.OnClick += new ButtonEvent(button_OnClick);
button.OnMouseEnter += new ButtonEvent(button_OnMouseEnter);
button.OnMouseLeave += new ButtonEvent(button_OnMouseLeave);
在您的Update
您致電
button.Update(Mouse.GetState());
在Draw
您致電
spriteBatch.Begin();
button.Draw(spriteBatch);
spriteBatch.End();
除了使用一個按鈕之外,還可以使用一系列按鈕(或者,如果我建議,可以使用List<Button>
),然后循環遍歷,以類似的方式更新和繪制它們。
然后很容易在事件處理程序上調用自定義代碼:
void button_OnClick(Button sender)
{
_gameState = GameStates.MainScreen; //or whatever else you might need
}
如果您將鼠標懸停,或者甚至使用時髦的淡入淡出,您甚至可以考慮更改紋理-如果可以編碼,則可能性是無限的!
使用Rectangle.Intersects
int X = (int) mouse_loc.X;
int Y = (int) mouse_loc.Y;
Rectangle MouseRect = new Rectangle(X,Y,1,1)
if (MouseRect.Intersects(TexturePosition.X,TexturePosition.Y,Texture.Width,Texture.Height)) //Mouse hovers over object[0]
return true;
else
return false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.