I am new to programming and new to c#, yet I am trying to make a 2D game. I created a Background class and a Close class, the Close class is for an exit button that I want to implement. What I want to achieve from this is that when I release the close button, the background will gradually lower is tone from white to a darker white. The problem is that I don't know how to really code this.Here is a view to my code.
Close Class
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);
}
Background Class
public Color color;
public Background(Color color){
this.color = color;
}
public void Update(GameTime gameTime){
}
Just to be a little more specific, I want the color to change within the Background class and be able to call this through the Close class. Also, keep in mind that the background color is being specified in the Game1 class, and the Update method is being called from it too.
Anyway, any help will be appreciated.
What I would do is something like this, though I don't have a lot of experience with Forms so it may or may not work as expected.
You'd call SyncFadeOut()/AsycFadeOut()
through your Close
class as required.
Synchronous (blocking) version:
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
// ----
}
}
Async (non-blocking) version:
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();
}