XNA / Monogame如何检测鼠标悬停在按钮上的时间

时间:2016-01-12 18:42:00

标签: c# xna monogame

我目前正在学习编程,正在制作一个以游戏为主的小游戏。但是,我的进度已停止在菜单上,我正在尝试找出如何检测我的鼠标是否在按钮上方,但我无法弄清楚如何用精灵检查这个?

class Menubutton
{
    public Texture2D texture;
    public Vector2 coords;
    bool isClicked = false;
    bool isHovered = false;

    public Menubutton(Texture2D textur, int X, int Y)
    {
        this.texture = textur;
        this.coords.X = X;
        this.coords.Y = Y;
    }

    public void Update(GameWindow window)
    {


    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, coords, Color.White);
    }
}

}

2 个答案:

答案 0 :(得分:2)

这样的事情可以解决问题:

public void Update(GameWindow window)
{
    var mouseState = Mouse.GetState();
    var mousePoint = new Point(mouseState.X, mouseState.Y);
    var rectangle = new Rectangle(mousePoint.X, mousePoint.Y, this.texture.Width, this.texture.Height);

    if (rectangle.Contains(mousePoint))
    {
        isHovered = true;
        isClicked = mouseState.LeftButton == ButtonState.Pressed;
    }
    else
    {
        isHovered = false;
        isClicked = false;
    }
}

请注意,isClicked标志实际含义的定义可能会有所不同。在这个简单的实现中,它只是意味着在按钮上方按下鼠标按钮,但从技术上讲,这并不是一个“点击”,所以我会让你考虑是否需要比这更复杂的东西。

答案 1 :(得分:0)

我通常将尽可能多的输入逻辑分离到静态InputManager类:

public class MenuButton
{
    #region Properties

    // Some properties that might come is handy
    public Rectangle Bounds { get { return bounds; } }

    public MenuButtonState State { get { return currentState; } }

    // Redundant but handy
    public bool IsClicked { get { return currentState == MenuButtonState.Clicked; } }

    #endregion



    #region Fields

    private Texture2D texture;
    private Vector2 position;
    private Rectangle bounds;

    private MenuButtonState currentState;

    #endregion



    #region Constructor

    public MenuButton(Texture2D texture, Vector2 position)
    {
        this.texture = texture;
        this.position = position;
        // Cast position X and Y to ints for the rectangle's constructor.
        bounds = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);

        // MenuButton starts off as Released.
        currentState = MenuButtonState.Released;
    }

    #endregion



    #region Update

    public void Update()
    {
        // There are much better ways to impliment button interaction than this 
        // but here is a simple example:

        // If the mouse cursor is on our MenuButton...
        if (InputManager.GetMouseBounds(true).Intersects(bounds))
        {
            // And if the Left mouse button is down...
            if (InputManager.GetIsMouseButtonDown(MouseButton.Left, true))
            {
                // Then our MenuButton is down!
                currentState = MenuButtonState.Pressed;
            }
            // Else if the Left mouse button WAS down but isn't any more...
            else if (InputManager.GetIsMouseButtonDown(MouseButton.Left, false))
            {
                // Then our MenuButton has been clicked!
                currentState = MenuButtonState.Clicked;
            }
            // Otherwise...
            else
            {
                // The mouse cursor is simply hovering above our MenuButton!
                currentState = MenuButtonState.Hovered;
            }
        }
        else
        {
            // If the cursor does not intersect with the MenuButton then just set the state to released.
            currentState = MenuButtonState.Released;
        }
    }

    #endregion
}

public enum MenuButtonState
{
    Released,
    Hovered,
    Pressed,
    Clicked
}

您的MenuButton类可能看起来像这样:

<script>
    var bad_words = ["<?php echo implode('","',$bad_words); ?>"];
    alert(bad_words);
</script>

当然,你有一个Draw()函数,可能还有其他一些函数。

我没有测试过上面的代码,但主要是希望它可以帮助你找到你想要的地方。尝试实现与按钮交互的不同方式,我有很多乐趣。例如,当按下按钮时能够处理点击,然后光标移开,然后移回按钮,然后释放。