2017-03-14 14 views
1

私はプログラミングには新しく、C#には新しく、まだ2Dゲームを作ろうとしています。 BackgroundクラスとCloseクラスを作成しました.Classクラスは実装したい終了ボタン用です。私がこれから達成したいのは、クローズボタンを放すと、背景が徐々に白から濃い白になるということです。問題は、私は実際にこれをコード化する方法を知らないということです。これは私のコードのビューです。Monogameで徐々に背景色を変更するには

閉じるクラス

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){ 

} 

ただ、もう少し具体的には、私は色が背景クラス内で変更して閉じるクラスを介してこれを呼び出すことができるようにしたいです。また、背景色はGame1クラスで指定されており、Updateメソッドもそこから呼び出されていることに注意してください。

とにかく、どんな助けもありがとうございます。

+0

あなたは '' Background' Static'クラスを作ることはできますか? –

+0

そのようなものですが、背景がゆっくりとその色の低音に変化することはありません。 –

答えて

0

私はFormsに多くの経験を持っていないが、期待どおりに動作するかもしれないし、動作しないかもしれない。

あなたは、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(); 
} 
関連する問題