简体   繁体   English

一直按住鼠标键而不是单击

[英]Mouse button keeps being pressed instead of clicking

I'm currently working on a game in monogame. 我目前正在从事单机游戏。 The game is heavily based on clicking buttons and this is where the problem occurs. 游戏很大程度上取决于单击按钮,而这就是问题所在。 Whenever I hold down the left mouse button and then move it over a button in the game, that button gets clicked instantly. 每当我按住鼠标左键,然后将其移到游戏中的某个按钮上时,该按钮都会立即被单击。

I've tried to fix the issue in many ways, by rearrange the if statements in different ways, adding an extra bool to check if the mouse button is clicked, etc.. but without any luck. 我试图以多种方式解决此问题,方法是通过以不同方式重新排列if语句,添加一个额外的bool来检查是否单击了鼠标按钮等,但是没有任何运气。 Haven't found any solutions anywhere either. 在任何地方都没有找到任何解决方案。

public override void Update(GameTime gameTime)
{
    MouseState state = Mouse.GetState();

    if (new Rectangle((position - ElementCenter).ToPoint(), sprite.Bounds.Size)
           .Contains(state.Position) && oldState.LeftButton == ButtonState.Released)
    {
        renderColor = Color.LightSlateGray;
        if (state.LeftButton == ButtonState.Pressed &&
            oldState.LeftButton == ButtonState.Released)
        {
            switch (button)
            {
                case "UI/submit":
                    if (GameWorld.Instance.Team.UserTeamName.Length > 0)
                    {
                        GameWorld.Instance.SubmitTeamName();
                    }
                    break;
                case "UI/teammanager":
                    GameWorld.Instance.TeamManager();
                    break;
                default:
                    break;
            }
        }
    }
    else
    {
        renderColor = Color.White;
    }
    oldState = state;
}

Ideally I would like that a button gets clicked only if the left mouse button has been released before clicking a button. 理想情况下,我希望仅在单击按钮之前释放鼠标左键时才单击按钮。

If oldState is declared as instance field, different instances of this class will have different versions of it. 如果将oldState声明为实例字段,则此类的不同实例将具有不同的版本。 Ie, the old state may get lost. 即,旧状态可能会丢失。

Declare oldState as static ! oldState声明为static

private static MouseState oldState = Mouse.GetState();

I usually use a class like this to read the input 我通常使用这样的类来读取输入

public class InputManager
{
    KeyboardState currentKeyboard, previousKeyboard;
    MouseState currentMouse, previousMouse;

    public bool LeftKeyIsHeldDown { get; private set; }
    public bool RightKeyIsHeldDown { get; private set; }
    public bool JumpWasJustPressed { get; private set; }
    public bool FireWasJustPressed { get; private set; }

    public void Update()
    {
        previousKeyboard = currentKeyboard;
        currentKeyboard = Keyboard.GetState();

        previousMouse = currentMouse;
        currentMouse = Mouse.GetState();

        LeftKeyIsHeldDown = currentKeyboard.IsKeyDown(Keys.A);
        RightKeyIsHeldDown = currentKeyboard.IsKeyDown(Keys.D);
        JumpWasJustPressed =  currentKeyboard.IsKeyDown(Keys.Space) && previousKeyboard.IsKeyUp(Keys.Space);
        FireWasJustPressed = currentMouse.LeftButton == ButtonState.Pressed && previousMouse.LeftButton == ButtonState.Released;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM