我是编程的菜鸟,但在我们开始之前,请回答我为什么更新执行不止一次,并像我是个傻瓜一样解释一下。
无论如何,所以我试图让这段代码只运行一次,因为到目前为止它执行了不止一次。
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
}
Run Code Online (Sandbox Code Playgroud)
如果需要,这就是 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);
}
Run Code Online (Sandbox Code Playgroud)
您之所以会出现这种行为,是因为UpdateXNA 每秒自动多次调用该方法,大约每秒 60 次。与 相同Draw,它可能会渲染大约 60 FPS。
因此,如果您按下按钮一秒钟并且该方法被调用 60 次,ButtonState.Pressed则将评估true60 次。
要解决此问题,您需要保留按钮的历史记录。这可以通过在每次更新时存储状态来实现:
//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;
}
Run Code Online (Sandbox Code Playgroud)
所以现在点击只会被注册一次(因为鼠标当前需要点击,但之前必须处于释放状态)。如果有一天你包含它,你也需要保留键盘的历史记录。
我还改变了你的显示切换逻辑,这样更清晰。您的评论中也有错误(应该是 //if false then it will set it to 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));
}
}
Run Code Online (Sandbox Code Playgroud)
它将自己注册为游戏组件,无需添加。状态会自行更新,因此只需调用辅助方法即可。
你最后if会变成:
if (inputState.IsNewLeftClick())
display = !display;
Run Code Online (Sandbox Code Playgroud)
这样更简洁一些,它提供了一种集中处理鼠标/键盘的方式(对 SRP 来说是的)。