XNA-Mouse.Left Button多次执行

问题描述 投票:2回答:2

我是编程新手,但在开始之前,请先回答我,为什么更新不止一次执行,就像我是个虚拟人一样解释它。

无论如何,我试图使此代码仅运行一次,因为到目前为止,它多次执行。

 protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

         button = Mouse.GetState();




         if (button.X < buttonPosition.X || button.Y < buttonPosition.Y || button.X > buttonPosition.X + font1.MeasureString(buttonText).X ||
             button.Y > buttonPosition.Y + font1.MeasureString(buttonText).Y)
             buttonColour = new Color(0, 0, 0);//if the mouse if not hovering over the font it stays that color
         else
             buttonColour = new Color(0, 255, 255);//changes to this color if it is hovering over text

        if(button.LeftButton==ButtonState.Pressed)
         display = (display == false) ? true : false;  //if display = true it will set it to false
        //if false then it will set it to false



 }

如果需要,这是Draw方法。

 protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.DrawString(font1, buttonText, buttonPosition, buttonColour);   //this is the button leftbutton has to click to trigger the below if statement.
        if (display)
            spriteBatch.DrawString(font1, text, position, Color.White);
        spriteBatch.End();  //it will draw this when leftbutton  clicks the above button

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
c# xna xna-4.0
2个回答
4
投票

之所以出现这种现象,是因为XNA每秒自动多次调用Update方法,大约每秒60次。与Draw相同,它可能会渲染约60 FPS。

因此,如果您按下按钮一秒钟,并且该方法被调用60次,则在这60次中ButtonState.Pressed的求值为true

要解决此问题,您需要保留按钮的历史记录。这可以通过在每次更新时存储状态来实现:

//Define this at class level
private MouseState lastMouseState = new MouseState();

protected override void Update(GameTime gameTime)
{
    // your other stuff

    MouseState currentState = Mouse.GetState(); //Get the state
    if (currentState.LeftButton == ButtonState.Pressed &&
        lastMouseState.LeftButton == ButtonState.Released) //Will be true only if the user is currently clicking, but wasn't on the previous call.
    {
        display = !display; //Toggle the state between true and false.
    }

    lastMouseState = currentState;
}

因此,单击将仅注册一次(因为鼠标当前需要单击,但是以前必须处于释放状态)。如果有一天,您还需要保留键盘的历史记录。

我还更改了显示切换的逻辑,这样更清洁。您的注释中也有一个错误(应该为//,如果为false,则它将设置为true)。这很正常,评论经常在说谎;总是尝试尽可能少地注释,只有代码才能说明事实。


如果对您有帮助,这是我之前使用的帮助程序类:

public class InputState : GameComponent
{
    private KeyboardState currentKeyboardState;
    private KeyboardState lastKeyboardState;
    private MouseState lastMouseState;
    private MouseState currentMouseState;

    public InputState(Game game) : base(game)
    {
        game.Components.Add(this);

        currentKeyboardState = new KeyboardState();
        lastKeyboardState = new KeyboardState();
        currentMouseState = new MouseState();
        lastMouseState = new MouseState();
    }

    public override void Update(GameTime gameTime)
    {
        lastKeyboardState = currentKeyboardState;
        currentKeyboardState = Keyboard.GetState();
        lastMouseState = currentMouseState;
        currentMouseState = Mouse.GetState();

        base.Update(gameTime);
    }

    public bool IsNewLeftClick()
    {
        return currentMouseState.LeftButton == ButtonState.Pressed &&
            lastMouseState.LeftButton == ButtonState.Released;
    }

    public bool IsNewRightClick()
    {
        return currentMouseState.RightButton == ButtonState.Pressed &&
            lastMouseState.RightButton == ButtonState.Released;
    }

    public Point GetMousePosition()
    {
        return new Point(currentMouseState.X, currentMouseState.Y);    
    }

    public bool IsNewKeyPress(params Keys[] keys)
    {
        return keys.Any(k => (currentKeyboardState.IsKeyDown(k) &&
                    lastKeyboardState.IsKeyUp(k)));
    }

    public bool IsCurrentlyPressed(params Keys[] keys)
    {
        return keys.Any(k => currentKeyboardState.IsKeyDown(k));
    }
}

它将自动注册为游戏组件,无需添加。状态将自行更新,因此只需调用helper方法。

您的最后一个if将成为:

if (inputState.IsNewLeftClick())
    display = !display;

这样比较干净,它提供了一种集中式方式来处理鼠标/键盘(是SRP的)。


0
投票
private KeyboardState currentKeyboardState;
private KeyboardState lastKeyboardState;

lastKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();

public bool IsNewKeyPress(params Keys[] keys)
    {
        return keys.Any(k => (currentstate.IsKeyDown(k) &&
                    laststate.IsKeyUp(k)));
    }

if (IsNewKeyPress(Keys.Space))
        { //Do something }

对于键盘上的任何键,此键都有效!谢谢兄弟皮埃尔

© www.soinside.com 2019 - 2024. All rights reserved.