C#鼠标拾取XNA
我有一个游戏菜单(目前)1图片(按钮)。这是一个纹理2D,我把它放在一个数组中。我想知道鼠标何时悬停在我的图片上。Actionscript有一个名为“hitTestObject”的内置函数。但现在看起来我必须检查图像的每个像素,看看鼠标是否在上面。我愿意改变一切,我只想能够选择不同的图片C#鼠标拾取XNA,c#,xna,C#,Xna,我有一个游戏菜单(目前)1图片(按钮)。这是一个纹理2D,我把它放在一个数组中。我想知道鼠标何时悬停在我的图片上。Actionscript有一个名为“hitTestObject”的内置函数。但现在看起来我必须检查图像的每个像素,看看鼠标是否在上面。我愿意改变一切,我只想能够选择不同的图片 Texture2D[] clickable_objects = new Texture2D[1]; clickable_objects[0] = Content.Load<Texture2D>("
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[]可点击的对象=新的Texture2D[1];
可点击的_对象[0]=Content.Load(“大脑图标”);
公共布尔相交(矢量2鼠标位置,纹理2D[]\u对象)
{
int X=(int)鼠标位置X;
int Y=(int)鼠标位置Y;
if()//鼠标悬停在对象[0]上
返回true;
其他的
返回false;
}
纹理2D只是图像的一种表示——它只有一个二维纹理网格。它在屏幕上没有位置,因此您无法对其执行鼠标点击检查
您需要一些包含类,如Sprite,它同时包含纹理和位置。然后可以向该类添加一个hittest()函数,该函数将检查纹理的位置和大小
或者更好的是,找到一些现有的sprite库供XNA使用。我敢肯定,有一些可以提供此功能。使用矩形。相交
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;
1.创建一个按钮类
你可以添加一些简单的东西,比如:
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);
}
}
2.设置它 要使用它,你需要在某处提供参考
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);
在您的绘图中
调用
button.Update(Mouse.GetState());
spriteBatch.Begin();
button.Draw(spriteBatch);
spriteBatch.End();
3.使用它 不要只使用一个按钮,而是使用一组按钮(或者,如果我可以推荐的话,使用
列表
),然后循环更新并以类似的方式绘制它们
然后,在事件处理程序上调用自定义代码就很容易了:
void button_OnClick(Button sender)
{
_gameState = GameStates.MainScreen; //or whatever else you might need
}
你甚至可以考虑改变纹理,如果鼠标悬停,或使用时尚褪色-可能性是无穷的,如果你可以编码他们! 您可以改为使用
矩形。包含(点)
,看起来更自然。是的,我忘记了点重载。