A high score
October 3, 2009
Persisting the high score turned out to be pretty easy. First I had to set up the variable that stores it at runtime. After that it was a matter of hooking that value up to the label and then updating it each time a better score was reached. I had to do two things next, create a new highscore.xml file to store the score, then write the score to it. Next I made a check to see if the file existed at load time, if so load it and read the contents. Once again the complete source is posted below. Pretty soon I’ll figure out how to upload solutions and then you can try it out for yourself. The source is getting a little long now so I’ll probably start breaking it up and posting snippets instead of most of the main.cs file.
public partial class AppDelegate : UIApplicationDelegate
{
bool isRunning = false;
DateTime start, end = new DateTime();
TimeSpan ts = new TimeSpan(0,0,0,0,0);
TimeSpan hs = new TimeSpan(0,0,1);
private static Selector selector = new Selector("UpdateTimer");
private static string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
private static string filePath = Path.Combine(path, "highscore.xml");
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
button.TouchDown += ButtonPushed;
InitHighScore();
// Create timer
// notes: the timer starts immediately
// selector is the delegate we want to run when the timer fires
NSTimer.CreateScheduledTimer(0.01, this, selector, null, true);
window.MakeKeyAndVisible ();
return true;
}
public void InitHighScore()
{
Console.WriteLine("InitHighScore() Enter");
if (File.Exists(filePath))
{
string hsXML = File.ReadAllText(filePath);
XmlDocument doc = new XmlDocument();
doc.LoadXml(hsXML);
string hsText = doc.SelectSingleNode("/HighScore").InnerText;
highScore.Text = hsText;
}
else
{
highScore.Text = string.Format("{0}:{1}", hs.Seconds, hs.Milliseconds);
}
Console.WriteLine("InitHighScore() Exit");
}
[Export ("UpdateTimer")]
public void UpdateTimer()
{
string time;
DateTime increment;
if (isRunning)
{
increment = DateTime.Now;
ts = increment.Subtract(start);
time = string.Format("{0}:{1}", ts.Seconds, ts.Milliseconds);
display.Text = time;
}
}
public void UpdateScoreFile()
{
Console.WriteLine("UpdateScoreFile() Enter");
if (!File.Exists(filePath))
{
Console.WriteLine("Creating new highscore file");
StreamWriter sw = File.CreateText(filePath);
sw.Close();
}
string hsXML = string.Format("{0}:{1}", hs.Seconds, hs.Milliseconds);
File.WriteAllText(filePath, hsXML);
Console.WriteLine(File.ReadAllText(filePath));
Console.WriteLine("UpdateScoreFile() Exit");
}
public void ButtonPushed(object sender, EventArgs e)
{
Console.WriteLine("ButtonPushed() Enter");
if (isRunning)
{
end = DateTime.Now;
isRunning = false;
button.SetTitle("Start", UIControlState.Normal);
ts = end.Subtract(start);
display.Text = string.Format("{0}:{1}", ts.Seconds, ts.Milliseconds);
if (ts < hs) {
hs = ts;
highScore.Text = string.Format("{0}:{1}", hs.Seconds, hs.Milliseconds);
UpdateScoreFile();
}
}
else
{
start = DateTime.Now;
isRunning = true;
button.SetTitle("Stop", UIControlState.Normal);
}
Console.WriteLine("ButtonPushed() Exit");
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
}
}
And here is the first ginormous screenshot of the StopWatch game:

I’ll figure out how to scale them better later.
October 5, 2009 at 11:48 pm
MonoTouch Article – Mauja – Part 4 – Stopwatch Game – A High Score…
Thank you for submitting this entry – Trackback from MonoTouch.Info…
October 8, 2009 at 6:25 pm
Yah man. This is a cinch to do. If you really want me to make this in flash, I can do it in about an hour or so…or I can wait until you have something a bit more complex.
The concept would be the same:
- timer object
- button with event listener
- labels for high score, current score
- xml file for keeping high score
- relevant functions
October 8, 2009 at 10:14 pm
http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/
October 14, 2009 at 2:19 am
Actually saving xml might be a problem. I’ll check into Adobe Air and Flex Builder.
If not, I think (think) Director might be able to do it.
October 14, 2009 at 2:34 am
Ok, so that was a trick question, it was Flash CS5 Pro…