我是编程的新手,但在开始之前请回答我为什么更新执行不止一次解释它就像我是假人。
无论如何,所以我试图让这段代码只运行一次,因为截至目前它只执行了一次。
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);
}
答案 0 :(得分:3)
您正在获得此行为,因为XNA会每秒多次自动调用Update
方法,可能大约每秒60次。它与Draw
相同,可能会渲染大约60 FPS。
因此,如果您按下某个按钮一秒钟并且该方法被调用60次,ButtonState.Pressed
将评估为true
60次。
要解决此问题,您需要保留按钮的历史记录。这可以通过在每次更新时存储状态来实现:
//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则应该为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));
}
}
它会将自己注册为游戏组件,无需添加它。状态将自行更新,因此只需调用辅助方法。
您的上一个if
将成为:
if (inputState.IsNewLeftClick())
display = !display;
它有点干净,它提供了一种集中处理鼠标/键盘的方法(yay for SRP)。
答案 1 :(得分: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 }
对于键盘上的任何键,此键都有效!谢谢皮埃尔兄弟