2

我有一个游戏菜单(目前)有 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;
}
4

3 回答 3

2

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

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

或者更好的是,找到一些现有的精灵库供 XNA 使用。我敢肯定有一些可以为您提供此功能。

于 2012-10-17T12:28:32.480 回答
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
}

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

于 2012-10-20T21:21:38.513 回答
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;
于 2012-10-17T22:37:18.550 回答