我正在创建一个记忆游戏(匹配游戏),我正在尝试节省玩家在匹配所有图块时获得的时间。我的节选者确实有效,而且我很好。这是我的save方法代码和查找所有tile是否匹配的方法:
private void Save(string time)
{
StreamWriter write = new StreamWriter(path, true);
write.WriteLine(time);
write.Close();
}
private void CheckForWinner()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
{
if(iconLabel != null)
{
if (iconLabel.ForeColor == iconLabel.BackColor)
{
return;
}
}
}
}
MessageBox.Show("You finished the game, your time was: " + timeLabel.Text);
Save();
//Close(); is outcommented because I want to see if it works.
}
答案 0 :(得分:1)
保存应该如下:
private void Save(string time)
{
File.WriteAllText(path, time); // Or AppendAllText, depends on what you want.
}
在CheckForWinner中你必须调用不是Save()而是Save(timeLabel.Text)。
答案 1 :(得分:1)
在调用time
时,您似乎无法指定参数Save
。
将timeLabel.Text
添加到您的函数调用中。
编辑:使用StreamWriter
的一个好方法是使用可用的using
命令。由于StreamWriter
是Disposable,您可以将其使用包含在using
内,而不必担心关闭它。请参阅更新的保存功能。
private void Save(string time)
{
using(StreamWriter write = new StreamWriter(path, true)){
write.WriteLine(time);
}
}
private void CheckForWinner()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
{
if(iconLabel != null)
{
if (iconLabel.ForeColor == iconLabel.BackColor)
{
return;
}
}
}
}
MessageBox.Show("You finished the game, your time was: " + timeLabel.Text);
Save(timeLabel.Text);
//Close(); is outcommented because I want to see if it works.
}