C# 2D XNA 游戏鼠标点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9712932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:31:11  来源:igfitidea点击:

2D XNA game mouse clicking

c#xna

提问by Ahmed Hegazy

I have a 2D game in which I use only the mouse as input. How can I make it so that my when the mouse hovers over a Texture2D object, the Texture2D and the mouse cursor change, and when the texture is clicked it moves to another place.

我有一个 2D 游戏,我只使用鼠标作为输入。我怎样才能做到这一点,当鼠标悬停在 Texture2D 对象上时,Texture2D 和鼠标光标会发生变化,并且当点击纹理时,它会移动到另一个地方。

Simply put, I want to know how to do something when I hover over or click on a Texture2D.

简单地说,我想知道当我将鼠标悬停在或单击 Texture2D 时如何做某事。

采纳答案by Lucius

In XNA you can use the Mouse classto query user input.

在 XNA 中,您可以使用Mouse 类来查询用户输入。

The easiest way of doing it is to check the mouse state for each frame and react accordingly. Is the mouse position inside a certain area? Display a different cursor. Is the right button pressed during this frame? Show a menu. etc.

最简单的方法是检查每一帧的鼠标状态并做出相应的反应。鼠标位置是否在某个区域内?显示不同的光标。在此帧期间是否按下了右键?显示菜单。等等。

var mouseState = Mouse.GetState();

Get the mouse position in screen coordinates (relative to the top left corner):

获取屏幕坐标中的鼠标位置(相对于左上角):

var mousePosition = new Point(mouseState.X, mouseState.Y);

Change a texture when the mouse is inside a certain area:

当鼠标位于特定区域内时更改纹理:

Rectangle area = someRectangle;

// Check if the mouse position is inside the rectangle
if (area.Contains(mousePosition))
{
    backgroundTexture = hoverTexture;
}
else
{
    backgroundTexture = defaultTexture;
}

Do something while the left mouse button is clicked:

单击鼠标左键时执行某些操作:

if (mouseState.LeftButton == ButtonState.Pressed)
{
    // Do cool stuff here
}

Remember though that you will always have information of the currentframe. So while something cool may happen during the time the button is clicked, it will stop as soon as released.

请记住,您将始终拥有当前帧的信息。因此,虽然在单击按钮期间可能会发生一些很酷的事情,但它会在释放后立即停止。

To check for a single click you would have to store the mouse state of the last frame and compare what has changed:

要检查单击,您必须存储最后一帧的鼠标状态并比较更改的内容:

// The active state from the last frame is now old
lastMouseState = currentMouseState;

// Get the mouse state relevant for this frame
currentMouseState = Mouse.GetState();

// Recognize a single click of the left mouse button
if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
{
    // React to the click
    // ...
    clickOccurred = true;
}

You could make it even more advanced and work with events. So you would still use the snippets from above, but instead of directly including the code for the action you would fire events: MouseIn, MouseOver, MouseOut. ButtonPush, ButtonPressed, ButtonRelease, etc.

您可以使其更高级并处理事件。因此,您仍将使用上面的代码片段,但不是直接包含操作的代码,而是触发事件:MouseIn、MouseOver、MouseOut。ButtonPush、ButtonPressed、ButtonRelease 等。

回答by Vilhelm

I would just like to add that the Mouse Click code could be simplified so that you don't have to make a variable for it:

我只想补充一点,可以简化鼠标单击代码,这样您就不必为其创建变量:

if (Mouse.GetState().LeftButton == ButtonState.Pressed)
    {
        //Write code here
    }