Evening all, having a bit of an issue with a game I'm making. As it stands the game will record your score, and update a high score integer if your score is higher than the current high score.
What I'm trying to do is make a couple of Save / Load methods that will save the highscore when the game is over and re load it when the game is opened again.
I'm trying to do this by just writing the number to a txt file and then reading it back in but I can't get it to work.
The method is:
public void Save(string highScore)
{
highScore.ToString();
System.IO.TextWriter textOut = null;
textOut = new System.IO.StreamWriter(highScore);
Save("highScore.txt");
}
and I'm calling it by using :
highScore.Save("highScore.txt");
But I get red lines under the "Save" on the method call saying " 'int' does not contain a defenition for 'Save' and no extension method 'Save' accepting a first agrument of type 'int' could be found"
I've tried calling ToString outside of the method before it is called as well but I had the same problem.
Any help would be great.
Cheers.
Loading
VulpesPosted Apr 30, 2011, 1:36 PM
Jamie WyattPosted Apr 30, 2011, 10:11 PM
Cheers for your help,
J.
Jamie WyattPosted Apr 30, 2011, 9:49 AM
This is my load method:
public void Load(string highScoreString)
{
StreamReader sr = new StreamReader("highScore.txt");
highScoreString = sr.Readline();
sr.Close();
int loadedHighScore = int.Parse(highScoreString);
}
and I'm calling it in my update method with:
Load("highScore.txt");
As far as I can see the method is loading the string from the file but then won't parse it for some reason.
Ideally what I want it to do is grab the number from the file, parse it and then set that int as the highscore.
Thanks so much for your help so far!
J
FroglegPosted Apr 29, 2011, 11:20 PM
if (keyState.IsKeyDown(Keys.F5))
{
Save(score);
}
and to save
public void Save(int highScore)
{
using (StreamWriter sw = new StreamWriter("highScore.txt"))
{
sw.WriteLine(Convert.ToString(highScore));
}
}
problem is as per your other post - you've got a hole in your bat
Jamie WyattPosted Apr 29, 2011, 9:57 PM
Same error under the "Save" call.
Am I calling this wrong? I've got that method in and I'm calling it by typing highScore.Save("highScore.txt");
Many thanks for your help.
J
EDIT : I've attached the source code now, the method I'm talking about is in the "PlayScreen" class at the bottom.
FroglegPosted Apr 29, 2011, 9:43 PM
public void Save(string highScore)
{
using (StreamWriter sw = new StreamWriter("highScore.txt"))
{
sw.WriteLine(highScore);
}
}