2016-06-17 2 views
0

로드하고 저장할 수있는 게임을 만들고 있습니다. 사용자가 얼마나 오래 연주했는지에 대한 세부 정보를 포함하고자했습니다. 나는 그들이 게임에 쓴 시간을 결정하기 위해 다음과 같은 것을 가지고있다. 총 시간을 저장하고로드하려면 어떻게합니까? 그 값을 Properties.Settings에 넣고 싶습니다.하지만로드해야하고 저장해야합니다. 어떻게해야합니까?시간 값로드, 업데이트 및 저장?

private void Form1_Load(object sender, EventArgs e) 
{ 
    //Load previous time 
    stopWatch.Start(); 
} 

private void Form1_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter && pbPlayer.Bounds.IntersectsWith(pnlSave.Bounds)) 
     { 
     string timePlayed; 
     stopWatch.Stop(); 
     TimeSpam timeSpan = stopWatch.Elapsed; //+ Their previous time 
     timePlayed = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); 
     MessageBox.Show("You've played for" + timePlayed + ". Game saved!" 
     } 
     //Save their total time 
} 
+1

가능한 [C# 양식의 앱 설정 저장 및 복원] (http://stackoverflow.com/questions/10359029/saving-and-restoring-app-settings-in-c-sharp-forms) –

+0

알고 있습니다. 나는'Properties.Settings'을 사용하고 있지만 어떻게 접근해야하는지에 대한 도움이 필요합니다. –

+2

어떤 특정 문제가 있습니까? 데이터를 종료 할 때 저장하고 시작시로드하십시오. 문자열 값이 아닌 경과 된 값을 저장해야하므로 다음에로드 할 때로드하고 추가 할 수 있습니다. –

답변

0

나는 이것을 알아 냈다. 이것은 내 대답을 해결했다.

private void Form1_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Enter && pbPlayer.Bounds.IntersectsWith(pnlSave.Bounds)) 
      { 
      string timePlayed; 
      stopWatch.Stop(); 
      TimeSpam totalTime = stopWatch.Elapsed + Properties.Settings.savedPlayTime; 
      timePlayed = string.Format("{0:D2}:{1:D2}:{2:D2}", Properties.Settings.Default.savedPlayTime.Hours, Properties.Settings.Default.savedPlayTime.Minutes, Properties.Settings.Default.savedPlayTime.Seconds); 
      if (MessageBox.Show("Are you sure you wish to overwrite your last save of " + timePlayed + "?")== DialogResult.OK) 
       { 
       Properties.Settings.Default.savedPlayTime = totalTime; 
       Properties.Settings.Default.Save(); 
       stopWatch.Reset(); 
       MessageBox.Show("Game saved!"); 
       } 
      stopWatch.Start(); 
      } 
    } 
0

잘 했어. 나는 틀릴 수도 있지만, 그 의도는 그들이 구원 받았을 때의 총 시간을 보여주는 것이라고 생각했습니다. timePlayed는 대신 totalTime의 문자열 형식이어야합니까?

timePlayed = string.Format ("{0 : D2} : {1 : D2} : {2 : D2}", totalTime.Hours, totalTime.Minutes, totalTime.Seconds);

+0

정말 죄송합니다, 내 실제 프로젝트에서 조금 다릅니다. 마지막으로 저장 한 시간에 timePlayed를 표시하려고합니다. 내가 생각하기에 MessageBox 텍스트를 변경할 수 있습니다. –