簡體   English   中英

如何逐漸改變Monogame中的背景顏色

[英]How to gradually change background color in Monogame

我是編程的新手,也是c#的新手,但我正在嘗試制作2D游戲。 我創建了一個Background類和一個Close類,Close類用於我想要實現的退出按鈕。 我想要實現的是當我釋放關閉按鈕時,背景將逐漸降低,從白色到深白色。 問題是我不知道如何真正編寫代碼。這是我的代碼的視圖。

關閉班級

private Texture2D texture;
private Vector2 position;
private Rectangle bounds;
private Color color;
MouseState oldstate;

public Close(Texture2D texture, Vector2 position){
  this.texture = texture;
  this.position = position;
  bounds = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
  color = new Color(40, 40, 40);
}

public void Update(GameTime gameTime){
  MouseState state = Mouse.GetState();
  Point point = new Point(state.X, state.Y);
  if(bounds.Contains(point) && !(state.LeftButton == ButtonState.Pressed)){
    color = new Color(235, 50, 50);
  } else if((!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Pressed)) || (!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Released))){
    color = new Color(40, 40, 40);
  }
  if((state.LeftButton == ButtonState.Pressed) && (oldstate.LeftButton == ButtonState.Released) && (bounds.Contains(point))){
    color = new Color(172, 50, 50);
  }
  if((state.LeftButton == ButtonState.Released) && (oldstate.LeftButton == ButtonState.Pressed) && (bounds.Contains(point))){

  }
  oldstate = state;
}

public void Draw(SpriteBatch spriteBatch){
  spriteBatch.Draw(texture, position, color);
}

背景課

public Color color;

public Background(Color color){
  this.color = color;

}

public void Update(GameTime gameTime){

}

為了更具體一點,我希望在Background類中更改顏色,並能夠通過Close類調用它。 另外,請記住,在Game1類中指定了背景顏色,並且也從它調用了Update方法。

無論如何,任何幫助將不勝感激。

我會做的是這樣的,雖然我沒有很多表格經驗,所以它可能會或可能不會按預期工作。

您可以根據需要通過Close類調用SyncFadeOut()/AsycFadeOut()

同步(阻塞)版本:

public void SyncFadeOut()
{
     // define how many fade-steps you want
     for (int i = 0; i < 1000; i ++)
     {
         System.Threading.Thread.Sleep(10); // pause thread for 10 ms

         // ----
         // do the incremental fade step here
         // ----
     }
}

異步(非阻塞)版本:

System.Timers.Timer timer = null;

public void FadeOut(object sender, EventArgs e)
{
    // ----
    // do the incremental fade step here
    // ----

    // end conditions
    if ([current_color] <= [end_color])
    {
        timer.Stop();
        // trigger any additional things you want, like close window
    }
}

public void AsyncFadeOut()
{
    System.Timers.Timer timer = new System.Timers.Timer(10); // triggers every 10ms, change this if you want a faster/slower fade
    timer.Elapsed += new System.Timers.ElapsedEventHandler(FadeOut);
    timer.AutoReset = true;
    timer.Start();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM