Something slightly more interesting than Hello World!
September 29, 2009
I started going through the Hello World tutorials again after getting thoroughly confused by Interface Builder. I mean seriously, talk about non-intuitive! I have to write some code, then go into a GUI to connect lines to dots, then switch back out and somehow through magic the lines made the objects that make up the interface visible in code. I’m really scratching my head on that one. Like any new tool there is a learning curve involved and I’ll just have to get used to it or learn how to go without IB which I understand is possible and is a good idea for a spike [investigate building ui w/o IB].
That said, I did finally get something working. I wanted to build an easy game that I used to play with my Father called the stopwatch game. Back when I was young I was really keen on digital watches. Most of the watches I owned had a stopwatch feature. By pressing a tiny button on the watch you could start and stop the time with a granularity down to the hundredths of a second. The game was that we would tap that button as fast as we could and see who could get the lowest “score” i.e. time on the watch.
That is our user story for a new project, stopwatch game. My only card really is to do this spike: Card #10 Try to write the stopwatch game. The game isn’t complicated… all I need is one button to start and stop the timer, and a label to store the score (time) in. It is not much different from ‘Hello World’ on the monotouch site so I won’t go over things step by step.
Here are the results:
namespace StopWatch
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
bool isRunning = false;
DateTime start, end = new DateTime();
TimeSpan ts = new TimeSpan();
// 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 += UpdateDisplay;
window.MakeKeyAndVisible ();
return true;
}
public void UpdateDisplay(object sender, EventArgs e)
{
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);
}
else
{
start = DateTime.Now;
isRunning = true;
button.SetTitle("Stop", UIControlState.Normal);
}
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
}
}
At some point I will figure out how to package and upload/link the solution here. It doesn’t run as is, you will need to define a button and a label in Interface Builder and then configure outlets called button and display respectively. If you hook those up in the app delegate class you will be good to go.
I need to figure out how to do screen shots on this mac lol… So much to do!
October 5, 2009 at 11:32 pm
MonoTouch Article – Mauja – Part 1 – Stopwatch Game – Something slightly more interesting than Hello World!…
Thank you for submitting this entry – Trackback from MonoTouch.Info…