<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Outer Level Blog &#187; Cocoa / Objective-C</title>
	<atom:link href="http://outerlevel.com/blog/categories/programming/cocoa-objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://outerlevel.com/blog</link>
	<description>Building a MicroISV</description>
	<lastBuildDate>Mon, 12 Sep 2011 17:30:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Code Example: Drawing the iPhone Back Button</title>
		<link>http://outerlevel.com/blog/2008/12/26/code-example-drawing-the-iphone-back-button/</link>
		<comments>http://outerlevel.com/blog/2008/12/26/code-example-drawing-the-iphone-back-button/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 20:42:41 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/?p=307</guid>
		<description><![CDATA[Recently, I had need to provide a back button similar to the one used in Mobile Safari for a consulting project. Many of the buttons used in the built-in iPhone applications are made available via the SDK with built in button types and graphics. Unfortunately, the back button is not one of these. Because I [...]]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:10px;">
<img src="http://outerlevel.com/blog/images/iPhoneBackButton.png" />
</div>
<p>Recently, I had need to provide a back button similar to the one used in Mobile Safari for a consulting project.  </p>
<p>Many of the buttons used in the built-in iPhone applications are made available via the SDK with built in button types and graphics.  Unfortunately, the back button is not one of these.</p>
<p>Because I needed to display the toolbar button from inside a static library which can not include images, I had to render the back arrow directly in code. </p>
<p>Since this was a bit time consuming, I thought I would share in hopes that it saves someone else a little bit of time.</p>
<div class="codeblock">
<pre lang="objc" line="1">
- (CGContextRef)createContext
{
   // create the bitmap context
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef context = CGBitmapContextCreate(nil,27,27,8,0,
                            colorSpace,kCGImageAlphaPremultipliedLast);
   CFRelease(colorSpace);
   return context;
}

- (CGImageRef)createBackArrowImageRef
{
   CGContextRef context = [self createContext];

   // set the fill color
   CGColorRef fillColor = [[UIColor blackColor] CGColor];
   CGContextSetFillColor(context, CGColorGetComponents(fillColor));

   CGContextBeginPath(context);
   CGContextMoveToPoint(context, 8.0f, 13.0f);
   CGContextAddLineToPoint(context, 24.0f, 4.0f);
   CGContextAddLineToPoint(context, 24.0f, 22.0f);
   CGContextClosePath(context);
   CGContextFillPath(context);

   // convert the context into a CGImageRef
   CGImageRef image = CGBitmapContextCreateImage(context);
   CGContextRelease(context);

   return image;
}

- (UIBarButtonItem *)backButton
{
   CGImageRef theCGImage = [self createBackArrowImageRef];
   UIImage *backImage = [[UIImage alloc] initWithCGImage:theCGImage];
   CGImageRelease(theCGImage);

   UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backImage
                                        style:UIBarButtonItemStylePlain
                                        target:self.webView
                                        action:@selector(goBack)];

   [backImage release], backImage = nil;

   return [backButton autorelease];
}
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/12/26/code-example-drawing-the-iphone-back-button/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New iPhone Game Programming Forum</title>
		<link>http://outerlevel.com/blog/2008/06/11/new-iphone-game-programming-forum/</link>
		<comments>http://outerlevel.com/blog/2008/06/11/new-iphone-game-programming-forum/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 15:34:18 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/?p=242</guid>
		<description><![CDATA[If you&#8217;re in to making games for the iPhone you may want to check out a new forum over on iDevGames.com dedicated to discussing iPhone &#038; iPod Game Development. While you&#8217;re there you may want to check out the other forums and resources on the site. There&#8217;s a ton of information on game programming for [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re in to making games for the iPhone you may want to check out a new forum over on <a href="http://www.idevgames.com/">iDevGames.com</a> dedicated to discussing <a href="http://www.idevgames.com/news/iphone-ipod-game-programming-message-board">iPhone &#038; iPod Game Development</a>.</p>
<p>While you&#8217;re there you may want to check out the other forums and resources on the site.  There&#8217;s a ton of information on game programming for the Mac too.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/06/11/new-iphone-game-programming-forum/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>See you at WWDC 2008</title>
		<link>http://outerlevel.com/blog/2008/06/06/see-you-at-wwdc-2008/</link>
		<comments>http://outerlevel.com/blog/2008/06/06/see-you-at-wwdc-2008/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 16:41:31 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/?p=241</guid>
		<description><![CDATA[Every year around this time a mass migration occurs around the world. Large numbers of Mac developers make the long arduous journey to San Francisco to gather, learn, network, and socialize. This year I make my own way to join this strange ritual for the first time. It&#8217;s time for Apple&#8217;s World Wide Developers Conference [...]]]></description>
			<content:encoded><![CDATA[<p>Every year around this time a mass migration occurs around the world.   Large numbers of Mac developers make the long arduous journey to San Francisco to gather, learn, network, and socialize.</p>
<p>This year I make my own way to join this strange ritual for the first time.</p>
<p>It&#8217;s time for Apple&#8217;s World Wide Developers Conference (WWDC).  A solid week of full emersion in all things Mac and iPhone.</p>
<p>I&#8217;m looking forward to finally meeting many people I&#8217;ve previously only known through following their Twitter feeds, reading their blogs, using their software, or chatting with them in IRC.  It&#8217;s a strange business we&#8217;re in where most of our contacts are strictly via electronic means.</p>
<p>With MacBook Pro, iPhone, and code in hand; I&#8217;m off.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/06/06/see-you-at-wwdc-2008/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Resolution Independent Buttons</title>
		<link>http://outerlevel.com/blog/2008/04/08/resolution-independent-buttons/</link>
		<comments>http://outerlevel.com/blog/2008/04/08/resolution-independent-buttons/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 10:00:00 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[User Interface Design]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2008/04/08/resolution-independent-buttons/</guid>
		<description><![CDATA[Sean Patrick O&#8217;Brien shares his approach and sample code for creating attractive resolution independent controls in Cocoa. I, like most developers, love shiny new user interfaces. Rounded corners, gradients, drop shadows, custom drawing &#8212; all of these things make me smile. Sean takes us through his steps for prototyping his controls in Photoshop to get [...]]]></description>
			<content:encoded><![CDATA[<p>Sean Patrick O&#8217;Brien shares his approach and sample code for <a href="http://www.seanpatrickobrien.com/journal/posts/3">creating attractive resolution independent controls in Cocoa</a>.</p>
<blockquote cite="http://www.seanpatrickobrien.com/journal/posts/3"><p>
I, like most developers, love shiny new user interfaces. Rounded corners, gradients, drop shadows, custom drawing &mdash; all of these things make me smile.
</p></blockquote>
<p>Sean takes us through his steps for prototyping his controls in Photoshop to get the look just right.  Then he walks us through his code step by step.  Complete sample Xcode project included.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/04/08/resolution-independent-buttons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Core Animation Podcast</title>
		<link>http://outerlevel.com/blog/2008/03/27/core-animation-podcast/</link>
		<comments>http://outerlevel.com/blog/2008/03/27/core-animation-podcast/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 14:14:19 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2008/03/27/core-animation-podcast/</guid>
		<description><![CDATA[In my previous post, Mac and iPhone Programming Books, I briefly mentioned the eBook Core Animation for OS X. Today, the publisher, The Pragmatic Programmers announced a new podcast interview with the book&#8217;s author, Bill Dudney. The podcast is obviously pushing his new book. But, if you have any interest in Core Animation or iPhone [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pragprog.com/titles/bdcora" alt="Core Animation for Mac OS X"><img style="float:right;border:none;margin-right:15px;margin-top:15px" src="http://outerlevel.com/blog/images/bdcora.jpg" /></a></p>
<p>In my previous post, <a href="http://outerlevel.com/blog/2008/03/24/mac-and-iphone-programming-books/">Mac and iPhone Programming Books</a>, I briefly mentioned the eBook <a href="http://www.pragprog.com/titles/bdcora">Core Animation for OS X</a>.</p>
<p>Today, the publisher, <a href="http://www.pragprog.com/">The Pragmatic Programmers</a> announced a new <a href="http://podcasts.pragprog.com/2008-03/core-animation-and-the-iphone.mp3">podcast interview with the book&#8217;s author, Bill Dudney</a>.  </p>
<p>The podcast is obviously pushing his new book. But, if you have any interest in Core Animation or iPhone development it does have some interesting information and it&#8217;s worth listening too.</p>
<p>UPDATE: <a href="http://www.macdevnet.com/index.php/component/content/frontpage">The Mac Developer Network</a> is also featuring Bill Dudney in their latest<a href="http://www.macdevnet.com/index.php/shows/latenightcocoa"> Late Night Cocoa Podcast</a> episode, <a href="http://www.macdevnet.com/index.php/shows/latenightcocoa/37-latenightcocoa/276-lnc028">Introduction to Core Animation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/03/27/core-animation-podcast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://podcasts.pragprog.com/2008-03/core-animation-and-the-iphone.mp3" length="6953198" type="audio/mpeg" />
		</item>
		<item>
		<title>Mac and iPhone Programming Books</title>
		<link>http://outerlevel.com/blog/2008/03/24/mac-and-iphone-programming-books/</link>
		<comments>http://outerlevel.com/blog/2008/03/24/mac-and-iphone-programming-books/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 16:12:05 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2008/03/24/mac-and-iphone-programming-books/</guid>
		<description><![CDATA[The introduction of Apple&#8217;s iPhone Software Development Kit has lead several friends to ask me for book recommendations to help them get started developing for the iPhone and Mac. While the selection is not huge at the moment, the few books that do exist are actually quite good. There are two primary technologies used to [...]]]></description>
			<content:encoded><![CDATA[<p>The introduction of Apple&#8217;s iPhone Software Development Kit has lead several friends to ask me for book recommendations to help them get started developing for the iPhone and Mac.</p>
<p>While the selection is not huge at the moment, the few books that do exist are actually quite good.</p>
<p>There are two primary technologies used to develop iphone and Mac software.  Objective-C is the programming language and Cocoa is the framework that provides the building blocks for building applications.</p>
<p><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fproduct%2F0321503619%2F&#038;tag=outerlevelcor-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">Cocoa Programming for Mac OS X (3rd Edition)</a><img src="http://www.assoc-amazon.com/e/ir?t=outerlevelcor-20&amp;l=ur2&amp;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, by Aaron Hilegass, is probably the most recommended Mac programming book published.  The current edition is outdated, but serves as a very good teaching aid for the beginning Cocoa programmer. But, if you can wait until this summer, the 3rd edition of this book will cover the additions and improvements to the Cocoa framework that Apple has added in Tiger and Leopard.</p>
<p>Cocoa uses the Objective-C language and<br />
<a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FProgramming-Objective-C-Developers-Library-Stephen%2Fdp%2F0672325861&#038;tag=outerlevelcor-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">Programming in Objective-C</a><img src="http://www.assoc-amazon.com/e/ir?t=outerlevelcor-20&amp;l=ur2&amp;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, by Stephen Kochan, teaches the fundamentals you&#8217;ll need to get your work done.</p>
<p>Alternatively, you may want to look into the eBook <a href="http://spiderworks.com/books/learnobjc.php">Learn Objective-C on the Macintosh</a>, By Mark Dalrymple and Scott Knaster.  I haven&#8217;t read this one, but have heard good things.</p>
<p>One of the cool things about Objective-C is that it&#8217;s basically an extension of the C programming language.  While programming in straight C is not usually required, it does come up from time to time and it can be very helpful to know what you are doing.  If you don&#8217;t know C, you many want to check out the eBook <a href="http://spiderworks.com/books/learncmac.php">Learn C on the Macintosh</a>, By Dave Mark.</p>
<p>The books listed above will get you started programming for the Mac and iPhone, but if you are interested in advanced topics or event Apple Script, the following books might be helpful.</p>
<ul>
<li><a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FAdvanced-Mac-Programming-Core-Unix%2Fdp%2F0974078514%2F&#038;tag=outerlevelcor-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">Advanced Mac OS X Programming (2nd Edition)</a><img src="http://www.assoc-amazon.com/e/ir?t=outerlevelcor-20&amp;l=ur2&amp;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /><br />
By Mark Dalrymple, Aaron Hillegass</li>
<li><a href="http://spiderworks.com/books/cocoagame.php">Cocoa Game Programming Workshop</a><br />
By David Hill</li>
<li><a href="http://www.pragprog.com/titles/bdcora">Core Animation for OS X</a><br />
By Bill Dudney</li>
<li><a href="http://spiderworks.com/books/ashandbook.php">AppleScript Handbook</a><br />
By Danny Goodman</li>
</ul>
<p>I&#8217;ve read most of these books, but the few that I haven&#8217;t were recommended to me and have very good reputations.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/03/24/mac-and-iphone-programming-books/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cocoa is My Girlfriend</title>
		<link>http://outerlevel.com/blog/2008/02/17/cocoa-is-my-girlfriend/</link>
		<comments>http://outerlevel.com/blog/2008/02/17/cocoa-is-my-girlfriend/#comments</comments>
		<pubDate>Sun, 17 Feb 2008 15:42:38 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2008/02/17/cocoa-is-my-girlfriend/</guid>
		<description><![CDATA[There&#8217;s a new cocoa development blog on the block. Cocoa is My Girlfriend, written by Marcus Zarra and Matt Long, has its first article posted: Cocoa Tutorial: NSOperation and NSOperationQueue Threading is hard in any language. And what is worse, when it goes wrong, it usually goes wrong in a very bad way. Looks like [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a new cocoa development blog on the block.  <a href="http://www.cimgf.com/">Cocoa is My Girlfriend</a>, written by <a href="http://www.zarrastudios.com/ZDS/Home/Home.html">Marcus Zarra</a> and <a href="http://www.matthew-long.com/">Matt Long</a>, has its first article posted: <a href="http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/">Cocoa Tutorial: NSOperation and NSOperationQueue</a></p>
<blockquote><p>
Threading is hard in any language. And what is worse, when it goes wrong, it usually goes wrong in a very bad way.
</p></blockquote>
<p>Looks like a new resource to keep tabs on.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2008/02/17/cocoa-is-my-girlfriend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Mac Developer Network</title>
		<link>http://outerlevel.com/blog/2007/11/23/the-mac-developer-network/</link>
		<comments>http://outerlevel.com/blog/2007/11/23/the-mac-developer-network/#comments</comments>
		<pubDate>Fri, 23 Nov 2007 16:01:52 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2007/11/23/the-mac-developer-network/</guid>
		<description><![CDATA[From the creator of the Late Night Cocoa podcast comes The Mac Developer Netowrk (MacDevNet.com) and the new Mac Developer Roundtable podcast. The Mac Developer Network (MDN) is hoping to become your first port of call for all things to do with Mac Development. Scotty has done a wonderful job with Late Night Cocoa over [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.macdevnet.com/index.php?option=com_content&#038;view=category&#038;layout=blog&#038;id=38&#038;Itemid=161" alt="Late Night Cocoa"><img style="width:128px;height:128px;float:right;border:none;margin-right:10px;" src="http://outerlevel.com/blog/images/macdevnet/MDR.png" /></a></p>
<p>From the creator of the <a href="http://www.macdevnet.com/index.php?option=com_content&#038;view=category&#038;layout=blog&#038;id=37&#038;Itemid=160">Late Night Cocoa podcast</a> comes <a href="http://www.macdevnet.com/">The Mac Developer Netowrk (MacDevNet.com)</a> and the new <a href="http://www.macdevnet.com/index.php?option=com_content&#038;view=category&#038;layout=blog&#038;id=38&#038;Itemid=161">Mac Developer Roundtable</a> podcast.</p>
<blockquote cite="http://www.macdevnet.com/"><p>
The Mac Developer Network (MDN) is hoping to become your first port of call for all things to do with Mac Development.
</p></blockquote>
<p><a href="http://www.macdevnet.com/index.php?option=com_content&#038;view=category&#038;layout=blog&#038;id=37&#038;Itemid=160" alt="Late Night Cocoa"><img style="width:128px;height:128px;float:left;border:none;margin-right:10px;" src="http://outerlevel.com/blog/images/macdevnet/LateNightCocoaSquare.png" /></a></p>
<p>Scotty has done a wonderful job with Late Night Cocoa over the past year and that makes me think the Mac Dev Net is going to be a great resource as its content and podcast episodes build up.</p>
<p>With the recent increase in Mac users and developers, it&#8217;s surprising there aren&#8217;t more programming resources outside of <a href="http://developer.apple.com/">The Apple Developer Connection</a>.  It&#8217;s a great community booster to have Cocoa developer resources like <a href="http://www.macdevnet.com/">Mac Dev Net</a>, <a href="http://www.cocoadev.com/">CocoaDev.com</a>, and <a href="http://www.cocoadevcentral.com/">Cocoa Dev Central</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2007/11/23/the-mac-developer-network/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bullfrog Game Design: Dynamic Bugs</title>
		<link>http://outerlevel.com/blog/2007/10/11/bullfrog-game-design-dynamic-bugs/</link>
		<comments>http://outerlevel.com/blog/2007/10/11/bullfrog-game-design-dynamic-bugs/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 13:42:01 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Bullfrog 2]]></category>
		<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2007/10/11/bullfrog-game-design-dynamic-bugs/</guid>
		<description><![CDATA[Developing software is an interesting process. There are times where I work very hard with little to show for it for several days. Then there are those days where I can knock out a ton of tasks on my &#8220;To Do&#8221; list with little effort. Usually, one state directly follows the other. I recently spent [...]]]></description>
			<content:encoded><![CDATA[<p>Developing software is an interesting process.  There are times where I work very hard with little to show for it for several days.  Then there are those days where I can knock out a ton of tasks on my &#8220;To Do&#8221; list with little effort.  Usually, one state directly follows the other.</p>
<p>I recently spent several days working on the mechanism that Bullfrog 2 will use to load level and bug definitions.  There was much reworking and testing involved.  Now that the work is complete, designing new levels and bugs should take little time and effort.</p>
<h2>Hardcoded Bugs</h2>
<p>In the original <a href="http://outerlevel.com/bullfrog">Bullfrog</a>, the player was tasked with munching bugs as fast as possible.  Bugs were randomly spawned on the screen based on a simple algorithm.  This algorithm would generate a certain number of bugs depending on what round the player had reached.  For example the gnat was generated using the following calculation.</p>
<div class="codeblock">
<pre lang="objc">
int numberOfGnats = roundIndex * GNAT_SPAWN_RATE;
</pre>
</div>
<div style="font-size:0.5em;text-align:center">listing 1</div>
<p>Each bug type had it&#8217;s own spawn formula and custom class.  Each bug class would then be allocated and tracked in a NSMutableArray during the round.</p>
<div class="codeblock">
<pre lang="objc" line="1">
NSPoint origin = GetRandomGamePoint(gameRect);
unsigned int direction = GetRandomAngle();
switch([bugDef bugType])
{
   case GnatType:
      bug = [[Gnat alloc] initAtPosition:origin
                           withDirection:direction];
      break;
//   <snip>...</snip>
}
</pre>
</div>
<div style="font-size:0.5em;text-align:center">listing 2</div>
<p>This approach worked fairly well for a simple game using random spawn points on a simple game board.  There were no level design elements to worry about.  A bug was either alive and on the screen, or it had been eaten.</p>
<h2>Dynamic Bugs</h2>
<p>For Bullfrog 2, we really wanted to move away from randomly generated rounds and bugs to designed levels.  We plan to have multiple levels with unique artwork and challenges.  But how do we define bugs and have them spawn based on a definition file?</p>
<p>We could read in the definition and run through a big switch statement with a case block for each bug type similar to the code above (listing 2) used in the first Bullfrog.  But, this gets nasty pretty quick.</p>
<p>We&#8217;re also adding new game elements to Bullfrog 2.  Players will have obstacles such as rocks and water to deal with while hunting down bugs.  These elements also need to be defined and tracked inside the game.</p>
<p>With a bit of imagination, rocks and bugs are basically the same thing except for a few different attributes.  Bugs can move, rocks can&#8217;t.  A mosquito can damage the player, but a rock can block the player&#8217;s movement.  </p>
<p>This lead us to a base class we&#8217;re calling <strong>Actor</strong>.  Each actor can be completely defined with a set of attributes which denote size, animation, and other things.</p>
<p>We still have the problem of needing unique AI (artificial intelligence) for the different bugs.  Using just the Actor class won&#8217;t provide us with customized code to define personality attributes such as chase and escape behaviors. </p>
<p>So we defined a set of Actor subclasses that provide the custom behaviors of the various bugs and other objects populating the Bullfrog world.  But, with each new game object, our switch case grows in complexity.</p>
<h2>Objective-C to the Rescue</h2>
<p>This is where our decision to use Objective-C pays off.  By using the dynamic nature of the Objective-C we can allocate and initialize our actors at runtime without using a huge switch statement.</p>
<p>Given the following <strong>Actor</strong> definitions (listing 3) we can dynamically generate the level&#8217;s actors with a simple loop (listing 4).</p>
<div class="codeblock">
<pre lang="xml" line="1">
<array>
   <dict>
      <key>class</key>
      <string>BFRock</string>
      <key>blocksMovement</key>
      <integer>1</integer>
   </dict>
   <dict>
      <key>class</key>
      <string>BFGnat</string>
      <key>blocksMovement</key>
      <integer>0</integer>
      <key>moveSpeed</key>
      <real>0.005</real>
      <key>turnFrequency</key>
      <real>5</real>
      <key>turnSpeed</key>
      <integer>20</integer>
   </dict>
</array>
</pre>
</div>
<div style="font-size:0.5em;text-align:center">listing 3</div>
<div class="codeblock">
<pre lang="objc" line="1">
- (void)loadActorsDefinition:(NSArray *)actorsDefinition
                   intoLayer:(NSMutableArray *)layer
{
   id actor;
   NSDictionary *actorDef;
   for(actorDef in actorsDefinition) {
      NSString *actorClass = [actorDef valueForKey:@"class"];
      actor = [[NSClassFromString(actorClass) alloc] init];
      if (![actor isKindOfClass:[OLActor class]]) continue;
      [actor loadDefinition:actorDef];
      [layer addObject:actor];
   }
}
</pre>
</div>
<div style="font-size:0.5em;text-align:center">listing 4</div>
<p>The key piece here is the function: <code>NSClassFromString(NSString *str).</code>  This Foundation function returns a Class object that can then be used just like your hardcoded class definition for allocating a new instance.</p>
<p>In the above function (listing 4) we get the &#8220;class&#8221; attribute from the XML and feed that into the NSClassFromString function to get our Class.  Then pass in the XML for this particular Actor into the instance method <code>-loadDefinition:</code> on the Actor class.  Some more Objective-C runtime magic reads in the defined attributes and stores them in the Actor instance properties using key-value coding (listing 5).</p>
<div class="codeblock">
<pre lang="objc" line="1">
NSString *key;
for (key in [definition allKeys]) {
   [self setValue:[definition valueForKey:key] forKey:key];
}
</pre>
</div>
<div style="font-size:0.5em;text-align:center">listing 5</div>
<p>Now all actors for each defined level are easily loaded and we don&#8217;t have to constantly rewrite or modify our code each time we add a new type of bug or game object.</p>
<p>The difference in complexity and number of lines of code in Bullfrog 2 is greatly reduced because of the magic that Objective-C provides.  Dynamic class loading is very powerful and provides us with great flexibility moving forward.  </p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2007/10/11/bullfrog-game-design-dynamic-bugs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bullfrog 2 Blogging</title>
		<link>http://outerlevel.com/blog/2007/09/18/bullfrog-2-blogging/</link>
		<comments>http://outerlevel.com/blog/2007/09/18/bullfrog-2-blogging/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 17:37:05 +0000</pubDate>
		<dc:creator>Jon Trainer</dc:creator>
				<category><![CDATA[Bullfrog 2]]></category>
		<category><![CDATA[Cocoa / Objective-C]]></category>
		<category><![CDATA[Products]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://outerlevel.com/blog/2007/09/18/bullfrog-2-blogging/</guid>
		<description><![CDATA[My Bullfrog 2 development partner, Marcus Zarra has started writing for the InformIT Blogs site and has published his first post, Starting over is hard to do. Marcus plans to share his side of the Bullfrog 2 development experience. If you are following along here, you may might want to head on over and follow [...]]]></description>
			<content:encoded><![CDATA[<p>My Bullfrog 2 development partner, <a href="http://zarrastudios.com/wordpress/">Marcus Zarra</a> has started writing for the InformIT Blogs site and has published his first post, <a href="http://www.informit.com/blogs/blog.aspx?uk=Starting-over-is-hard-to-do">Starting over is hard to do</a>.</p>
<p>Marcus plans to share his side of the Bullfrog 2 development experience.   If you are following along here, you may might want to head on over and follow along.</p>
]]></content:encoded>
			<wfw:commentRss>http://outerlevel.com/blog/2007/09/18/bullfrog-2-blogging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

