【问题标题】:How to gradually change background color in Monogame如何在 Monogame 中逐渐改变背景颜色
【发布时间】:2017-03-16 10:33:10
【问题描述】:

我是编程新手和 c# 新手,但我正在尝试制作 2D 游戏。我创建了一个背景类和一个关闭类,关闭类用于我要实现的退出按钮。我想要实现的是,当我释放关闭按钮时,背景会逐渐降低,从白色到深白色。问题是我不知道如何真正编码。这是我的代码的视图。

关闭课程

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 方法。

无论如何,我们将不胜感激。

【问题讨论】:

  • 你能把Background 变成Static 类吗?
  • 那种是可以的,但不会让背景慢慢变成它颜色的低调。

标签: c# monogame


【解决方案1】:

我会做的是这样的事情,虽然我对表单没有太多经验,所以它可能会也可能不会按预期工作。

您可以根据需要通过您的 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 2012-06-16
    • 1970-01-01
    • 2012-03-11
    • 2010-12-17
    相关资源
    最近更新 更多