<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>XPerienced Blog</title>
	
	<link>http://xperienced.com.pl/blog</link>
	<description>iPhone development from .NET developer perspective</description>
	<lastBuildDate>Fri, 10 Feb 2012 16:00:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/xperienced" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="xperienced" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Easier iOS table views with ios-datatable</title>
		<link>http://xperienced.com.pl/blog/easier-ios-tableviews-with-ios-datatable</link>
		<comments>http://xperienced.com.pl/blog/easier-ios-tableviews-with-ios-datatable#comments</comments>
		<pubDate>Fri, 10 Feb 2012 16:00:24 +0000</pubDate>
		<dc:creator>Bartek</dc:creator>
				<category><![CDATA[idevblogaday]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[obj-c]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[sourcecode]]></category>

		<guid isPermaLink="false">http://xperienced.com.pl/blog/?p=559</guid>
		<description><![CDATA[The project that I recently started to build in my spare time aims to simplify building a data table based views using Cocoa framework. Everyone that has tried to build a view that resembles a built-in iPhone Settings app knows that it&#8217;s not so easy, especially when you need to leverage a different UI controls [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="https://github.com/xperienced/ios-datatable">project</a> that I recently started to build in my spare time aims to simplify building a data table based views using Cocoa framework. Everyone that has tried to build a view that resembles a built-in iPhone Settings app knows that it&#8217;s not so easy, especially when you need to leverage a different UI controls inside your table cells like UISwitch, UISlider etc. Even if you know how to do it it&#8217;s a very repetitive and boring task which most often also results in a low quality code. The project that I started to build is in its early stages at the moment, but it might be already useful for some of you so I decided to finally reveal some details about it.<span id="more-559"></span></p>
<p>For everyone interested I suggest to get the sources from the <a href="https://github.com/xperienced/ios-dataview">sample project</a> hosted on a github. This project references <a href="https://github.com/xperienced/ios-datatable">ios-datatable</a> project using a git submodule.</p>
<h2>Download details</h2>
<pre>git clone git://github.com/xperienced/ios-dataview.git
cd ios-dataview
git submodule init
git submodule update</pre>
<p>The result of running just a few lines of a code should be the following:</p>
<p><a href="http://xperienced.com.pl/blog/wp-content/uploads/2012/02/Screen-shot-2012-02-09-at-10.07.05-PM.png"><img class="aligncenter size-medium wp-image-560" title="ios-datatable sample project screenshot" src="http://xperienced.com.pl/blog/wp-content/uploads/2012/02/Screen-shot-2012-02-09-at-10.07.05-PM-235x300.png" alt="ios-datatable screenshot" width="235" height="300" /></a></p>
<p>Just to give you an idea how easy it is to build a slider cell, look at the following line of a code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">XPSliderDataCell <span style="color: #002200;">*</span>cell4 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>XPSliderDataCell alloc<span style="color: #002200;">&#93;</span> initWithText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sample slider&quot;</span> value<span style="color: #002200;">:</span>.5f<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2>More to come&#8230;</h2>
<p>There is more than just displaying, the library will also support the following:</p>
<ul>
<li>2-way data binding &#8211; so you can bind your classes / properties to Data Cells and those properties will be displayed and updated by changes in data cell,</li>
<li>Events through delegates and blocks &#8211; so you can react on changes in data cells.</li>
</ul>
<p>Let me know if you have any other requests and if you like the project or want to contribute &#8211; do let me know. Thanks!</p>
<img src="http://feeds.feedburner.com/~r/xperienced/~4/XlEL2452vLE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://xperienced.com.pl/blog/easier-ios-tableviews-with-ios-datatable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS open-source projects on a Github</title>
		<link>http://xperienced.com.pl/blog/ios-open-source-projects-on-github</link>
		<comments>http://xperienced.com.pl/blog/ios-open-source-projects-on-github#comments</comments>
		<pubDate>Fri, 13 Jan 2012 18:01:59 +0000</pubDate>
		<dc:creator>Bartek</dc:creator>
				<category><![CDATA[idevblogaday]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[sourcecode]]></category>

		<guid isPermaLink="false">http://xperienced.com.pl/blog/?p=548</guid>
		<description><![CDATA[I got several requests to share a code to my tutorials so that folks can better understand what I blog about. I don&#8217;t have any problems with that except the fact that some of my posts are based on the actual work that I do and I can&#8217;t share all the stuff with you. I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://xperienced.com.pl/blog/wp-content/uploads/2012/01/github-logo.png"><img class="alignleft size-full wp-image-551" title="github-logo" src="http://xperienced.com.pl/blog/wp-content/uploads/2012/01/github-logo.png" alt="github" width="270" height="276" /></a>I got several requests to share a code to my tutorials so that folks can better understand what I blog about. I don&#8217;t have any problems with that except the fact that some of my posts are based on the actual work that I do and I can&#8217;t share all the stuff with you. I found some time recently to extract some of the code from my projects and I started sharing it on a <a href="https://github.com/">github</a>. The work is still in progress but I hope to add some more from time to time. In my professional career (.NET) I build few <strong>big</strong> libraries / frameworks that used to help me build more complex solutions easily but now I find this approach a bit awkward. A big library or a framework is all or nothing kind of thing which I don&#8217;t like.<span id="more-548"></span></p>
<p>So instead I started to build small projects (modules) that are one purpose only. If you need something bigger, take few of them and include it in your project, want to keep them up to date &#8211; include it as git submodule and pull the updates directly from github. If you want to contribute &#8211; fork the project on a github and do send me a pull request.</p>
<p>So far I only managed to start and include a sample project (not complete yet) that I use to generate a settings-like interface. It has a much nicer API than raw UITableView and it allows you to rapidly build data-driven, table based applications.</p>
<p>Major release will presumably come in few weeks, but in the meantime you can get an early look on what is going on there:</p>
<ul>
<li><strong><a title="ios-datatable" href="https://github.com/xperienced/ios-datatable">ios-datatable</a></strong> is the first small library (I will blog about it in my next <a href="http://idevblogaday.com/">idevblogaday</a> post)</li>
<li><strong><a href="https://github.com/xperienced/ios-dataview">ios-dataview</a></strong> is a sample project that is using ios-datatable and it includes a reference to it as a submodule.</li>
</ul>
<p>Coming soon:</p>
<ul>
<li>cocos2d-puzzlemvc</li>
<li>cocos2d-resourceloader</li>
</ul>
<img src="http://feeds.feedburner.com/~r/xperienced/~4/0dg6b9LI2pg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://xperienced.com.pl/blog/ios-open-source-projects-on-github/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>cocos2d meets MVC – wrap up</title>
		<link>http://xperienced.com.pl/blog/cocos2d-meets-mvc-wrap-up</link>
		<comments>http://xperienced.com.pl/blog/cocos2d-meets-mvc-wrap-up#comments</comments>
		<pubDate>Fri, 02 Dec 2011 19:04:22 +0000</pubDate>
		<dc:creator>Bartek</dc:creator>
				<category><![CDATA[idevblogaday]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[separation of concerns]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://xperienced.com.pl/blog/?p=544</guid>
		<description><![CDATA[This post is going to be short. If you have any questions regarding the previous parts or something is unclear please let me know. I have some contract projects going on (which going to be HUGE!!!) so not so much time for blogging. To wrap up the previous parts: We implemented the Model which helps [...]]]></description>
			<content:encoded><![CDATA[<p>This post is going to be short. If you have any questions regarding the previous parts or something is unclear please let me know. I have some contract projects going on (which going to be HUGE!!!) so not so much time for blogging. To wrap up the previous parts:</p>
<ol>
<li>We implemented the Model which helps us encapsulate the logic of a game that is not polluted with other concerns (like display),</li>
<li>We implemented the Controller that handles the user interaction (and updates the model),</li>
<li>We implemented the View which responsibility is ONLY to display the current state of a model.</li>
</ol>
<div>The interaction between the components is the following:</div>
<div>
<ol>
<li>The Controller starts the Model and the View,</li>
<li>The View displays the Model,</li>
<li>The View responds to user touches and passes (using delegate) the event to the Controller,</li>
<li>The Controller implements view delegate and reacts by calling the Model,</li>
<li>The Model performs some game logic and notifies the View that its state has changed,</li>
<li>The View updates its objects (like sprites) according to the current state of the Model.</li>
</ol>
<div>Open questions:</div>
<div>
<ol>
<li>If you were going to implement an &#8220;Undo&#8221; functionality using NSUndoManager, where would you put your undo calls?</li>
<li>How would you store the state of your current scene?</li>
<li>Others?</li>
</ol>
</div>
</div>
<img src="http://feeds.feedburner.com/~r/xperienced/~4/zRZ0j4mMQag" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://xperienced.com.pl/blog/cocos2d-meets-mvc-wrap-up/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>cocos2d meets MVC – Implementing simple board game part 3</title>
		<link>http://xperienced.com.pl/blog/cocos2d-meets-mvc-implementing-simple-board-game-part-3</link>
		<comments>http://xperienced.com.pl/blog/cocos2d-meets-mvc-implementing-simple-board-game-part-3#comments</comments>
		<pubDate>Fri, 18 Nov 2011 22:01:38 +0000</pubDate>
		<dc:creator>Bartek</dc:creator>
				<category><![CDATA[idevblogaday]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[separation of concerns]]></category>
		<category><![CDATA[sourcecode]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://xperienced.com.pl/blog/?p=532</guid>
		<description><![CDATA[This is the 3rd part from the series how to build a simple puzzle game using cocos2d and MVC pattern. If you haven&#8217;t read previous parts I suggest you do it before reading this post: cocos2d meets MVC part 1, cocos2d meets MVC part 2 Updating the Model When the user touches the toolbox item [...]]]></description>
			<content:encoded><![CDATA[<p>This is the 3rd part from the series how to build a simple puzzle game using cocos2d and MVC pattern. If you haven&#8217;t read previous parts I suggest you do it before reading this post:</p>
<ul>
<li><a title="cocos2d meets MVC – Implementing simple board game part 1" href="http://xperienced.com.pl/blog/cocos2d-meets-mvc-implementing-board-game-part-1">cocos2d meets MVC part 1</a>,</li>
<li><a title="cocos2d meets MVC – Implementing simple board game part 2" href="http://xperienced.com.pl/blog/cocos2d-meets-mvc-%e2%80%93-implementing-simple-board-game-part-2">cocos2d meets MVC part 2</a></li>
</ul>
<h2>Updating the Model</h2>
<p>When the user touches the toolbox item and then touches the space on a game board we want that the game piece represented by a toolbox item will be placed on a game board at touch position. In the previous part we already implemented <em>touchedAtRow</em> method in <em>GameBoardViewDelegate.</em> We will extend this protocol and add additional method for handling toolbox item selection.</p>
<p><span id="more-532"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> GameBoardViewDelegate
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gameBoard touchedAtRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gameBoard toolboxItemTouchedAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>We need to change the implementation of our touches handler, so that we now detect whether we clicked on a toolbox or on a game board.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ccTouchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event <span style="color: #002200;">&#123;</span>
    UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
    CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self convertTouchToNodeSpace<span style="color: #002200;">:</span>touch<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// touched on a game board</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>gameBoardRectangle, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">int</span> row, column;
        <span style="color: #11740a; font-style: italic;">// calculate row and column based on a touch coordinate</span>
        <span style="color: #11740a; font-style: italic;">// ...</span>
        <span style="color: #11740a; font-style: italic;">// call controller</span>
        <span style="color: #002200;">&#91;</span>self.delegate gameBoard<span style="color: #002200;">:</span>self.gameBoard touchedAtRow<span style="color: #002200;">:</span>row column<span style="color: #002200;">:</span>column<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #11740a; font-style: italic;">// touched on a toolbox</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>toolboxRectangle, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">int</span> index;
        <span style="color: #11740a; font-style: italic;">// calculate toolbox item index based on a touch coordinate</span>
        <span style="color: #002200;">&#91;</span>self.delegate gameBoard<span style="color: #002200;">:</span>self.gameBoard toolboxItemTouchedAtIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Handling events on the Controller side is easy, first of all we extend the Model to keep the state of our model and allows us to modify the state based on user touches. Our interface will look something like this (implementation details omitted for clarity):</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> GameBoard <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>putGamePiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GamePiece <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gamePiece row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>GamePiece <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>getGamePieceFromToolboxItemAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>index;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Then we finish the implementation of <em>GameBoardViewDelegate</em> in GameBoardController and handle our events accordingly.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aGameBoard toolboxItemTouchedAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>index <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// keep the toolbox selection state in the Model</span>
    gameBoard.selectedToolboxItemIndex <span style="color: #002200;">=</span> index;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aGameBoard touchedAtRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// if the toolbox item is selected move item from toolbox to game board</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>gameBoard.selectedToolboxItemIndex <span style="color: #002200;">!=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        GamePiece <span style="color: #002200;">*</span>gamePiece <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>gameBoard getGamePieceFromToolboxItemAtIndex<span style="color: #002200;">:</span>gameBoard.selectedToolboxItemIndex<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>gameBoard putGamePiece<span style="color: #002200;">:</span>gamePiece row<span style="color: #002200;">:</span>row column<span style="color: #002200;">:</span>column<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>What we achieved so far is that when the user touches on of the toolbox item and then touches the space on a game board the Model gets updated reflecting the desired game board state.</p>
<h2>Notifying the View about the changes in the Model</h2>
<p>In order to reflect the state of the Model in the View we need to send some notifications so when the Model gets changed the View will be able to react upon it.</p>
<p>We do this the same way we notify the Controller about the View changes. We start from the delegate declaration inside the  Model.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> GameBoardDelegate;
<span style="color: #a61390;">@interface</span> GameBoard <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span>
id<span style="color: #002200;">&amp;</span>lt;GameBoardDelegate<span style="color: #002200;">&amp;</span>gt; delegate;
<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@protocol</span> GameBoardDelegate
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gameBoard didPutGamePiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GamePiece <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gamePiece row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> GameBoard
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>putGamePiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GamePiece <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gamePiece row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #11740a; font-style: italic;">// store game piece</span>
    <span style="color: #11740a; font-style: italic;">// notify that the game piece was put on a gameboard</span>
    <span style="color: #002200;">&#91;</span>delegate gameBoard<span style="color: #002200;">:</span>self didPutGamePiece<span style="color: #002200;">:</span>gamePiece row<span style="color: #002200;">:</span>row column<span style="color: #002200;">:</span>column<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The last part is the implementation of <em>GameBoardDelegate</em> in the <em>GameBoardView</em> so that when the game piece is put on a game board the View will put a <em>CCSprite</em> at a given position.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> GameBoardView <span style="color: #002200;">:</span> CCLayer
<span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> GameBoardView
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithGameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aGameBoard delegate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>aDelegate <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// retain gameboard</span>
        self.gameBoard <span style="color: #002200;">=</span> aGameBoard;
        self.gameBoard.delegate <span style="color: #002200;">=</span> self;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// assign delegate</span>
        self.delegate <span style="color: #002200;">=</span> aDelegate;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gameBoard didPutGamePiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GamePiece <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gamePiece row<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// create CCSprite and put it on a game board at corresponding position</span>
    CCSprite <span style="color: #002200;">*</span>gamePieceSprite <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSprite spriteWithFile<span style="color: #002200;">:</span>fileName<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>gamePieceSprite<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h2>Summary</h2>
<p>All the pieces are now connected together. The interaction between the Model, the Controller and the View conforms to MVC paradigm:</p>
<ul>
<li>The View handles the touches and notifies the Controller,</li>
<li>The Controller reacts upon the user touches and updates the Model,</li>
<li>The model updates its state, does the logic and tells the View that it has been changed,</li>
<li>The View updates itself based on the current Model state.</li>
</ul>
<div>Comments?</div>
<img src="http://feeds.feedburner.com/~r/xperienced/~4/T2lymoHZcz8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://xperienced.com.pl/blog/cocos2d-meets-mvc-implementing-simple-board-game-part-3/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>cocos2d meets MVC – Implementing simple board game part 2</title>
		<link>http://xperienced.com.pl/blog/cocos2d-meets-mvc-%e2%80%93-implementing-simple-board-game-part-2</link>
		<comments>http://xperienced.com.pl/blog/cocos2d-meets-mvc-%e2%80%93-implementing-simple-board-game-part-2#comments</comments>
		<pubDate>Fri, 04 Nov 2011 22:19:41 +0000</pubDate>
		<dc:creator>Bartek</dc:creator>
				<category><![CDATA[idevblogaday]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[obj-c]]></category>
		<category><![CDATA[separation of concerns]]></category>
		<category><![CDATA[sourcecode]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://xperienced.com.pl/blog/?p=515</guid>
		<description><![CDATA[In the first part of the cocos2d meets MVC series I described how to start implementing MVC paradigm for cocos2d based board game. The first post only scratched the surface, we have prepared a skeleton but we still have a lot to do. Introducing model In the previous part we introduced the View and the [...]]]></description>
			<content:encoded><![CDATA[<p>In the first part of the <a title="cocos2d meets MVC – Implementing simple board game part 1" href="http://xperienced.com.pl/blog/cocos2d-meets-mvc-implementing-board-game-part-1">cocos2d meets MVC</a> series I described how to start implementing MVC paradigm for cocos2d based board game. The first post only scratched the surface, we have prepared a skeleton but we still have a lot to do.</p>
<h2>Introducing model</h2>
<p>In the previous part we introduced the View and the Controller. To comply with MVC paradigm we need to add a model which will represent the current state of a game board. Our implementation now should contain the following classes:</p>
<ol>
<li><em>GameBoardView</em> - the View,</li>
<li><em>GameBoardController</em> - the Controller.</li>
<li><strong>GameBoard &#8211; the Model.</strong></li>
</ol>
<h2><a href="http://xperienced.com.pl/blog/wp-content/uploads/2011/11/MVC-diagram-introduced-model.jpg"><img class="aligncenter size-medium wp-image-523" title="MVC diagram - introduced model" src="http://xperienced.com.pl/blog/wp-content/uploads/2011/11/MVC-diagram-introduced-model-300x194.jpg" alt="MVC diagram - introduced model" width="300" height="194" /></a></h2>
<h2><span id="more-515"></span>Model implementation</h2>
<h3>GameBoard implementation</h3>
<p>Our requirement (described in the first part) is that:</p>
<p><em>&#8230;A game board is defined by a number of rows and columns, which may vary for different application levels.</em></p>
<p>We implement it in the following way:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> GameBoard <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    NSInteger numberOfRows;
    NSInteger numberOfColumns;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithRows<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>aNumberOfRows columns<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>aNumberOfColumns;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> NSInteger numberOfRows;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> NSInteger numberOfColumns;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Please notice that the model extends NSObject &#8211; the model should contain just the state of a game board (and the methods to update this state) &#8211; we shouldn&#8217;t put any view related concerns here.</p>
<h3>GameBoardView implementation</h3>
<p>We need to modify the View and pass it the Model, we do this in initWithGameBoard method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> GameBoardView <span style="color: #002200;">:</span> CCNode <span style="color: #002200;">&#123;</span>
    GameBoard <span style="color: #002200;">*</span>gameBoard;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> GameBoard <span style="color: #002200;">*</span>gameBoard;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithGameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aGameBoard;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Our implementation of a GameBoardView can look like this (rendering game board spaces omitted to simplify the code):</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithGameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aGameBoard <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// retain gameboard</span>
        self.gameBoard <span style="color: #002200;">=</span> aGameBoard;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// render gameboard background</span>
        CCSprite <span style="color: #002200;">*</span>gameboardSprite <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSprite spriteWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;gameboard.png&quot;</span><span style="color: #002200;">&#93;</span>;
        gameboardSprite.anchorPoint <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>gameboardSprite<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// render spaces</span>
        <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; gameBoard.numberOfRows; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> j <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j <span style="color: #002200;">&amp;</span>lt; gameBoard.numberOfColumns; j<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #11740a; font-style: italic;">// position and render game board spaces</span>
            <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h3>GameBoardController</h3>
<p>Finally the implementation of an init method in the Controller should be updated &#8211; the View needs to have the GameBoard model injected in its init method so we construct the game board in Controller (just for now) and pass it to the View (later on we will create a game board based on a level definition).</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// initialize model</span>
        gameBoard <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GameBoard alloc<span style="color: #002200;">&#93;</span> initWithRows<span style="color: #002200;">:</span><span style="color: #2400d9;">7</span> columns<span style="color: #002200;">:</span><span style="color: #2400d9;">9</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// initialize view</span>
        view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GameBoardView alloc<span style="color: #002200;">&#93;</span> initWithGameBoard<span style="color: #002200;">:</span>gameBoard<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>view<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h2>Handling touches</h2>
<h3>GameBoardView updates</h3>
<p>In order to handle touches we need to slightly modify the View. We make it extend CCLayer instead of CCNode to have a possibility to handle touch events inside of it:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> GameBoardView <span style="color: #002200;">:</span> CCLayer <span style="color: #002200;">&#123;</span>
...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The View itself should not be responsible for handling the actions that are results of user interaction with the game board so we will pass it to the Controller using protocol.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> GameBoardViewDelegate
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gameBoard touchedAtRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>We also modify the init method of a GameBoardView so we can pass a delegate object that will be responsible for handling the touches:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithGameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aGameBoard delegate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>aDelegate;</pre></div></div>

<p>And the implementation is:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ccTouchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event <span style="color: #002200;">&#123;</span>
    UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
    CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self convertTouchToNodeSpace<span style="color: #002200;">:</span>touch<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// calculate row and column touched by the user and call a delegate method</span>
    <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #002200;">&#91;</span>self.delegate gameBoard<span style="color: #002200;">:</span>self.gameBoard touchedAtRow<span style="color: #002200;">:</span>row column<span style="color: #002200;">:</span>column<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h3>GameBoardController updates</h3>
<p>GameBoardController will be responsible for handling user touches, so we need to implement a GameBoardViewDelegate in a GameBoardController:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> GameBoardController <span style="color: #002200;">:</span> CCNode</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>gameBoard<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameBoard <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gameBoard touchedAtRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>row column<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>column <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// do the game logic here and update view accordingly</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The last step is to modify the View initialization in the following way (in init method):</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// initialize view</span>
view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GameBoardView alloc<span style="color: #002200;">&#93;</span> initWithGameBoard<span style="color: #002200;">:</span>gameBoard delegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h2>Summary</h2>
<p>In this part we implemented the Model and we did the necessary steps to make it visible to the View and to Controller. We also setup an indirect link between the View and the Controller so if the user touches the space on a game board the Controller will be able to handle the action according to game rules. The next part will cover:</p>
<ul>
<li>Updating the Model inside the Controller,</li>
<li>Notifying the View about the changes in the Model.</li>
</ul>
<div>If you have any questions or remarks feel free to leave a comment after this post.</div>
<img src="http://feeds.feedburner.com/~r/xperienced/~4/HID9jQ7JCBM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://xperienced.com.pl/blog/cocos2d-meets-mvc-%e2%80%93-implementing-simple-board-game-part-2/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

