我是编程新手,在开始之前请回答我,为什么更新会执行多次,并像对待傻瓜一样解释它。
无论如何,我现在正在尝试使此代码仅运行一次,因为目前它会执行多次。
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
}
这是绘图方法,如果需要的话。
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);
}