C#鼠标选择XNA

时间:2012-10-17 12:08:40

标签: c# 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;
}

3 个答案:

答案 0 :(得分:2)

Texture2D只是图像的表示 - 它只有一个2D网格纹素。它在屏幕上没有位置,因此您无法对其进行鼠标点击检查。

你需要一些包含类,如Sprite,它包含纹理和位置。然后你可以在该类中添加一个hittest()函数,它将检查纹理的位置和大小。

或者更好的是,找一些现有的用于XNA的精灵库来使用。我确信有一些能为您提供此功能。

答案 1 :(得分:2)

1。创建一个Button类

你如何添加这样简单的东西:

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);

Update致电

button.Update(Mouse.GetState());

Draw致电

spriteBatch.Begin();
button.Draw(spriteBatch);
spriteBatch.End();

3。使用它

使用一个按钮数组(或者,如果我可以推荐,使用List<Button>),而不是一个按钮,然后循环更新并以类似的方式绘制所有按钮。

然后很容易在事件处理程序上调用自定义代码:

void button_OnClick(Button sender)
{
    _gameState = GameStates.MainScreen; //or whatever else you might need
}

如果鼠标悬停,或者使用时尚的淡入淡出,您甚至可以考虑更改纹理 - 如果您可以编码它们,可能性是无穷无尽的!

答案 2 :(得分:1)

使用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;
相关问题