2016-08-25 5 views
-1

私は新しいOOPの学生です。私はC#でXamarinスタジオで簡単な時計プログラムを書いています。時計のフォーマットは00:00:00です。カウントアップして時計として動作する必要があります。 しかし、私の時計は画面上に描画することができますが、「チェック」しないので、いくつかの問題がなければなりません。時計プログラムはカウントされません

using SwinGameSDK; 
namespace MyGame 
{ 
    public class Clock 
    { 
     private Counter counter = new Counter(); 
     private int _seconds; 
     private int _minutes; 
     private int _hours; 

     public Clock() 
     { 
      _seconds = counter.SecondsCounter; 
      _minutes = counter.MinutesCounter; 
      _hours = counter.HoursCounter; 
     } 

     public int Seconds { 
      get { 
       return _seconds; 
      } 

      set { 
       _seconds = value; 
      } 
     } 

     public int Minutes { 
      get { 
       return _minutes; 
      } 

      set { 
       _minutes = value; 
      } 
     } 

     public int Hours { 
      get { 
       return _hours; 
      } 

      set { 
       _hours = value; 
      } 
     } 

     public void DrawClock() 
     { 
      DrawHours(); 
      SwinGame.DrawText (":", Color.Black, "Arial", 80, 360, 200); 
      DrawMinutes(); 
      SwinGame.DrawText (":", Color.Black, "Arial", 80, 520, 200); 
      DrawSeconds(); 
     } 

     public void DrawHours() 
     { 
      SwinGame.DrawText (Hours.ToString ("D2"), Color.Black, "Arial", 80, 250, 208); 
     } 

     public void DrawMinutes() 
     { 
      SwinGame.DrawText (Minutes.ToString ("D2"), Color.Black, "Arial", 80, 410, 208); 
     } 

     public void DrawSeconds() 
     { 
      SwinGame.DrawText (Seconds.ToString ("D2"), Color.Black, "Arial", 80, 560, 208); 
     } 

     public void UpdateClock() 
     { 
      counter.UpdateCounter(); 

     } 

     public void ResetClock() 
     { 
      counter.Reset(); 

     } 

     public void SetClock() 
     { 
      counter.SetTimer(); 
     } 
    } 
} 


using System.Timers; 

namespace MyGame 
{ 
    public class Counter 
    { 
     private int _hoursCounter; 
     private int _minutesCounter; 
     private int _secondsCounter; 

     public Counter() 
     { 
      _hoursCounter = 0; 
      _minutesCounter = 0; 
      _secondsCounter = 0; 
     } 

     public int HoursCounter { 
      get { 
       return _hoursCounter; 
      } 
      set { 
       _hoursCounter = value; 
      } 
     } 

     public int MinutesCounter { 
      get { 
       return _minutesCounter; 
      } 
      set { 
       _minutesCounter = value; 
      } 
     } 

     public int SecondsCounter { 
      get { 
       return _secondsCounter; 
      } 
      set { 
       _secondsCounter = value; 
      } 
     } 

     Timer timer = new Timer(); 

     public void SetTimer() 
     { 
      timer.Interval = 1000; 
      timer.Elapsed += (sender, e) => UpdateCounter(); 
     } 

     public void UpdateCounter() 
     { 
      timer.Start(); 
      SecondsCounter += 1; 
      if (SecondsCounter == 60) { 
       SecondsCounter = 0; 
       MinutesCounter += 1; 
      } 
      if (MinutesCounter == 60) { 
       MinutesCounter = 0; 
       HoursCounter += 1; 
      } 
      if (HoursCounter == 24) { 
       HoursCounter = 0; 
       MinutesCounter = 0; 
       SecondsCounter = 0; 
      } 
     } 



     public void Reset() 
     { 
      HoursCounter = 0; 
      MinutesCounter = 0; 
      SecondsCounter = 0; 
      timer.Close(); 
     } 
    } 
} 

using SwinGameSDK; 

namespace MyGame 
{ 
    public class GameMain 
    { 
     public static void Main() 
     { 
      var myClock = new Clock(); 

      //Open the game window 
      SwinGame.OpenGraphicsWindow ("GameMain", 800, 600); 
      SwinGame.ShowSwinGameSplashScreen(); 

      //Run the game loop 
      while (false == SwinGame.WindowCloseRequested()) { 
       //Fetch the next batch of UI interaction 
       SwinGame.ProcessEvents(); 

       //Clear the screen and draw the framerate 
       SwinGame.ClearScreen (Color.White); 
       SwinGame.DrawFramerate (0, 0); 

       myClock.DrawClock(); 
       myClock.SetClock(); 
       if (SwinGame.MouseClicked (MouseButton.LeftButton)) { 
        myClock.UpdateClock(); 
       } 
       if (SwinGame.MouseClicked (MouseButton.RightButton)) { 
        myClock.ResetClock(); 
       } 

       //Draw onto the screen 
       SwinGame.RefreshScreen (60); 
      } 
     } 
    } 
} 
+3

側注:使用自動実装プロパティ:https://msdn.microsoft.com/en-us/library/bb384054.aspx – user3185569

+0

をあなたはそれをデバッグしようとしましたか?チックが起きたときに画面を更新する方法にも入りますか? – 3615

+0

まず、コードをデバッグして何が起きているのかを確認する必要があります。 'SetTimer'に' timer.Enabled = true'を追加しても問題ありませんか? – Pikoh

答えて

1

あなたが継続的にそれをリセットしているので、あなたの時計はを刻々と過ぎていない理由は、myClock.SetClock()メソッドを使用して間隔です。このメソッドは、mainメソッドのwhileループの外側で1回だけ呼び出される必要があります。主な方法に従ってみてください。

public static void Main() 
    { 
     var myClock = new Clock(); 

     //Open the game window 
     SwinGame.OpenGraphicsWindow ("GameMain", 800, 600); 
     SwinGame.ShowSwinGameSplashScreen(); 

     myClock.SetClock();     //Set clock should be called from here. 
     //Run the game loop 
     while (false == SwinGame.WindowCloseRequested()) { 
      //Fetch the next batch of UI interaction 
      SwinGame.ProcessEvents(); 

      //Clear the screen and draw the framerate 
      SwinGame.ClearScreen (Color.White); 
      SwinGame.DrawFramerate (0, 0); 

      myClock.DrawClock(); 

      if (SwinGame.MouseClicked (MouseButton.LeftButton)) { 
       myClock.UpdateClock(); 
      } 
      if (SwinGame.MouseClicked (MouseButton.RightButton)) { 
       myClock.ResetClock(); 
      } 

      //Draw onto the screen 
      SwinGame.RefreshScreen (60); 
     } 
    } 

とのSetTimerあなたも、タイマーを開始いけないように思え

public void SetTimer() 
    { 
     timer.Interval = 1000; 
     timer.Elapsed += (sender, e) => UpdateCounter(); 
     timer.Start(); 
    } 
+0

私はあなたの方法を試しましたが、まだ動作しません..私はユーザーが時計を開始するようにしたいので、UpdateCounter()にtimer.startを入れます。 – CEz

0

を次のようにする必要があります。 timer.Start();の代わりに
を呼び出します。
UpdateCounter()の後にタイマーを開始します。timer.Elapsed + =(送信者、e)=> UpdateCounter();

また、変数をまとめておく必要があります。

private int _hoursCounter; 
private int _minutesCounter; 
private int _secondsCounter; 
Timer timer; 

とコンストラクタでタイマーを初期化します。

.. 
_secondsCounter = 0; 
timer = new Timer(); 
+0

私はこの方法を試しましたが、まだ動作しません... – CEz

+0

何がうまくいかないかをより多くの情報を提供し、変数や構築方法にブレークポイントを置き、f10を押して何が変化していないかを調べる。明らかに何かが欠けている。 – Mightee

関連する問題