<?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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Martin's Dev Blog</title>
	
	<link>http://www.martinhoeller.net</link>
	<description>Cocoa development and productivity</description>
	<lastBuildDate>Mon, 14 May 2012 20:44:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MartinsDevBlog" /><feedburner:info uri="martinsdevblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Using OpenStreetMap Data in an iPhone App</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/EaHGvHlSnr4/</link>
		<comments>http://www.martinhoeller.net/2012/05/14/using-openstreetmap-data-in-an-iphone-app/#comments</comments>
		<pubDate>Mon, 14 May 2012 20:44:18 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[mapkit]]></category>
		<category><![CDATA[openstreetmap]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=254</guid>
		<description><![CDATA[Visiting a session about OpenStreetMap at Barcamp Graz 2012 inspired me to make an iPhone app which shows me restaurants, bars etc. in which smoking is not allowed (yes, this is still an issue in 2012). Getting the Data OpenStreetMap ...]]></description>
				<content:encoded><![CDATA[<p>Visiting a session about <a href="http://www.openstreetmap.org">OpenStreetMap</a> at <a href="http://barcamp-graz.at/">Barcamp Graz 2012</a> inspired me to make an iPhone app which shows me restaurants, bars etc. in which smoking is not allowed (yes, this is <a href="http://www.wissenswertes.at/index.php?id=rauchverbot">still an issue</a> in 2012).</p>
<h2>Getting the Data</h2>
<p>OpenStreetMap nodes are filled with useful meta information, one of them being the &#8220;smoking&#8221; tag which indicates whether smoking is allowed, not allowed or only allowed in a separated area. So first we have to get the data from somewhere. Because downloading the whole planet is a bit much, I downloaded only the Austria file (about 320 MB compressed / 4 GB uncompressed) from <a href="http://download.geofabrik.de/osm/europe/">http://download.geofabrik.de/osm/europe/</a>.</p>
<p>The raw file contains all available nodes, ways and relations and their meta data. As I am only interested in nodes containing the smoking tag, I somehow needed to filter the file to get a much smaller file containing only the necessary data. This can be done with the command line tool <em>osmosis</em> (<a href="http://wiki.openstreetmap.org/wiki/Osmosis/Installation#OS_X">install instructions</a>). To make life easier I also used <a href="http://osmembrane.de/">OSMembrane</a> as a GUI on top of <em>osmosis</em>. The configuration I used was read-xml -> node-key filter -> write-xml:<br />
<a href="http://www.martinhoeller.net/wp-content/uploads/2012/05/osmosis.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2012/05/osmosis.png" alt="" title="osmosis" width="578" height="126" class="aligncenter size-full wp-image-255" /></a><br />
The node-key filter was configured with &#8220;Task = node-key&#8221; and &#8220;keyList = smoking&#8221;, which results in all the nodes containing the tag &#8220;smoking&#8221;.</p>
<p>When you run this pipeline, the result is a 234kb <em>osm</em> file containing standard XML. A sample node from the file looks like this:</p>
<div class="codecolorer-container text railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;node id=&quot;26899232&quot; version=&quot;10&quot; timestamp=&quot;2011-11-09T08:18:02Z&quot; uid=&quot;449374&quot; user=&quot;geobene&quot; changeset=&quot;9779408&quot; lat=&quot;47.0756757&quot; lon=&quot;15.4482614&quot;&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;addr:city&quot; v=&quot;Graz&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;addr:country&quot; v=&quot;AT&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;addr:housenumber&quot; v=&quot;17&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;addr:postcode&quot; v=&quot;8010&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;addr:street&quot; v=&quot;Zinzendorfgasse&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;amenity&quot; v=&quot;pub&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;contact:email&quot; v=&quot;contact@propeller.co.at&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;contact:fax&quot; v=&quot;+43 316 58 14 77&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;contact:mobile&quot; v=&quot;+43 699 10 350 350&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;contact:phone&quot; v=&quot;+43 316 22 50 53&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;internet_access&quot; v=&quot;wlan&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;name&quot; v=&quot;Propeller&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;old_name&quot; v=&quot;Schuberthof&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;opening_hours&quot; v=&quot;09:00-02:00&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;smoking&quot; v=&quot;isolated&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;source&quot; v=&quot;survey&quot;/&gt;<br />
&nbsp; &nbsp; &lt;tag k=&quot;website&quot; v=&quot;http://www.propeller.co.at/&quot;/&gt;<br />
&lt;/node&gt;</div></div>
<p>As you can see it contains a lot of useful data, e.g. name, address and other contact information of the facility. The most important data for our app are of course the geolocation (lat/lon) and the &#8220;smoking&#8221; tag. </p>
<h2>What now?</h2>
<p>The exported file can now be used in an iPhone app by parsing it with the NSXMLParser class, for example. The geolocation can be used to place pins on a MKMapView. Smoking restrictions of the facilities can be visualized using custom map annotations. Using the other meta data it is also possible to show detailed information about a facility (and make phone calls directly within the app). A simple app using this data could look like this:</p>
<p><a href="http://www.martinhoeller.net/wp-content/uploads/2012/05/smokingapp.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2012/05/smokingapp-159x300.png" alt="" title="smokingapp" width="159" height="300" class="aligncenter size-medium wp-image-263" /></a></p>
<p>Please contact me if you need further details or require source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2012/05/14/using-openstreetmap-data-in-an-iphone-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2012/05/14/using-openstreetmap-data-in-an-iphone-app/</feedburner:origLink></item>
		<item>
		<title>Grain Wallpaper</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/t2ufte4jw1M/</link>
		<comments>http://www.martinhoeller.net/2012/02/09/grain-wallpaper/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 22:53:58 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=237</guid>
		<description><![CDATA[I like to have a clean, non-distracting desktop wallpaper. For a long time now I have been using a solid grey background (57% white). Inspired by Matt Gemmel&#8217;s subtle UI texture tutorial I made a grainy wallpaper that has the ...]]></description>
				<content:encoded><![CDATA[<p>I like to have a clean, non-distracting desktop wallpaper. For a long time now I have been using a solid grey background (57% white). Inspired by Matt Gemmel&#8217;s <a href="http://www.mattgemmell.com/2012/01/29/subtle-ui-texture-in-photoshop/">subtle UI texture tutorial</a> I made a grainy wallpaper that has the same non-distracting nature as a solid color, but is more &#8220;yummy&#8221; than pure grey. The archive contains the wallpapers optimized for 7 different screen resolutions. </p>
<p>
<a href='http://www.martinhoeller.net/wp-content/uploads/2012/02/Grain-Wallpaper.zip'>Download Grain-Wallpaper.zip<br/><img src="http://www.martinhoeller.net/wp-content/uploads/2012/02/grain_preview.png" alt="" title="Grain Wallpaper" width="164" height="127" class="alignleft size-full wp-image-240" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2012/02/09/grain-wallpaper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2012/02/09/grain-wallpaper/</feedburner:origLink></item>
		<item>
		<title>A Game Idea Machine</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/O2YCnfM63l4/</link>
		<comments>http://www.martinhoeller.net/2012/02/05/a-game-idea-machine/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 13:16:17 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=231</guid>
		<description><![CDATA[Did you ever get stuck when trying to come up with new game ideas? Try out my little tool I put together: What Game should I make? It generates new game ideas by randomly choosing from genre, visual style and ...]]></description>
				<content:encoded><![CDATA[<p>Did you ever get stuck when trying to come up with new game ideas? Try out my little tool I put together: </p>
<p><a href="http://www.martinhoeller.net/whatgame">What Game should I make?</a></p>
<p>It generates new game ideas by randomly choosing from genre, visual style and other parameters. You can also preselect each parameter, e.g. if you just want to make 8bit 2D games.<br />
Feel free to use it, share it or do whatever you like with it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2012/02/05/a-game-idea-machine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2012/02/05/a-game-idea-machine/</feedburner:origLink></item>
		<item>
		<title>Sneak Peek: First Screenshot of Rotron</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/TN1N0PN_K0A/</link>
		<comments>http://www.martinhoeller.net/2012/02/05/sneak-peek-first-screenshot-of-rotron/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 12:58:40 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[app store]]></category>
		<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=225</guid>
		<description><![CDATA[My new game I am working on is called &#8220;Rotron&#8221; and is nearing its completion. After some polishing, tweaking and balancing I hope to submit it by next weekend to the App Store for review. It is an iPhone/iPod Touch ...]]></description>
				<content:encoded><![CDATA[<p>My new game I am working on is called &#8220;Rotron&#8221; and is nearing its completion. After some polishing, tweaking and balancing I hope to submit it by next weekend to the App Store for review. It is an iPhone/iPod Touch arcade game made with Cocos2D. Here is the first public screenshot:</p>
<p><a href="http://www.martinhoeller.net/wp-content/uploads/2012/02/rotron_screen.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2012/02/rotron_screen-300x153.png" alt="" title="Rotron" width="300" height="153" class="aligncenter size-medium wp-image-226" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2012/02/05/sneak-peek-first-screenshot-of-rotron/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2012/02/05/sneak-peek-first-screenshot-of-rotron/</feedburner:origLink></item>
		<item>
		<title>Developing a Musical Instrument App for Android</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/uu2VnCct760/</link>
		<comments>http://www.martinhoeller.net/2012/01/13/developing-a-musical-instrument-app-for-android/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 17:16:09 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[android]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=201</guid>
		<description><![CDATA[I am currently developing my first app on Android, which can be quite frustrating as an iOS developer (more on that in another post perhaps). In this post I will focus on how to develop a multi-touch enabled musical instrument ...]]></description>
				<content:encoded><![CDATA[<p>I am currently developing my first app on Android, which can be quite frustrating as an iOS developer (more on that in another post perhaps).</p>
<p>In this post I will focus on how to develop a multi-touch enabled musical instrument app with low latency playback of audio samples.</p>
<h2>Prerequisites</h2>
<p>The app should be able to play about 50 different sound samples, at least 5 of them simultaneously. Playing a sound should be instant, so low latency is required.</p>
<h2>Android Sound Libraries</h2>
<p>Android offers three different sound APIs: MediaPlayer, SoundPool and AudioTrack. MediaPlayer is for playback of longer audio files and video, so this API is not suitable for our needs.<br/><br />
SoundPool&#8217;s documentation tells us that it should be used for repeated, simultaneous low-latency playback of multiple short sound samples. That&#8217;s exactly what we need! Unfortunately it turned out to be very laggy when playing. So the last resort was Android&#8217;s low level audio API AudioTrack.</p>
<h2>Using AudioTrack</h2>
<p>To play a sound with AudioTrack, the file has to be read into a buffer which is then pushed directly to the audio hardware. Playing multiple sounds simultaneously requires a thread for each sound.</p>
<h3>Playing a Sound</h3>
<p>To encapsulate audio playback, I created the AudioTrackSoundPlayer class:</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AudioTrackSoundPlayer<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> HashMap<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, PlayThread<span style="color: #339933;">&gt;</span> threadMap <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Context</span></a> context<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> AudioTrackSoundPlayer<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acontext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Context</span></a> context<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; threadMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>, AudioTrackSoundPlayer.<span style="color: #006633;">PlayThread</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>The two members are a HashMap to store the threads of the currently playing sounds and a reference to the activity context which is needed later to create AudioTrack objects.</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> playNote<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> note<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isNotePlaying<span style="color: #009900;">&#40;</span>note<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlayThread thread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PlayThread<span style="color: #009900;">&#40;</span>note<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thread.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>note, thread<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stopNote<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> note<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PlayThread thread <span style="color: #339933;">=</span> threadMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>note<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>thread <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thread.<span style="color: #006633;">requestStop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threadMap.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>note<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>When playing a note a new thread is started and put into the map. Notes are only played if they are not already playing. Stopping a note (if the user releases the button) fetches the thread out of the map and requests it to stop.</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isNotePlaying<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> note<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> threadMap.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span>note<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>To check if a note is already playing we simply have to look for the kay in the map.<br/></p>
<p>Now to the fun part: the PlayThread class.</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> PlayThread <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Thread</span></a><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> note<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> stop <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; AudioTrack audioTrack <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> PlayThread<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> note<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">note</span> <span style="color: #339933;">=</span> note<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>The class stores the note it plays, a stop flag which is set when the thread should be terminated (and thus stop playing the sound) and the AudioTrack instance used to play the sound.</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> path <span style="color: #339933;">=</span> note <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.wav&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AssetManager assetManager <span style="color: #339933;">=</span> context.<span style="color: #006633;">getAssets</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AssetFileDescriptor ad <span style="color: #339933;">=</span> assetManager.<span style="color: #006633;">openFd</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> fileSize <span style="color: #339933;">=</span> ad.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> bufferSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4096</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span>bufferSize<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioTrack <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AudioTrack<span style="color: #009900;">&#40;</span>AudioManager.<span style="color: #006633;">STREAM_MUSIC</span>, <span style="color: #cc66cc;">22050</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aaudioformat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">AudioFormat</span></a>.<span style="color: #006633;">CHANNEL_CONFIGURATION_MONO</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aaudioformat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">AudioFormat</span></a>.<span style="color: #006633;">ENCODING_PCM_16BIT</span>, bufferSize, AudioTrack.<span style="color: #006633;">MODE_STREAM</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>First the file name is constructed by adding &#8220;.wav&#8221; to the note. Then the file size is read by opening the file with AssetManager. Next we create a 4096 byte buffer to store the audio data. Adapt the buffer size to your needs. 4096 seemed to work best for me. The AudioTrack object is then created with the appropriate configuration, depending on the sample files you use.</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioTrack.<span style="color: #006633;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> audioStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> headerOffset <span style="color: #339933;">=</span> 0x2C<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> bytesWritten <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> bytesRead <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>stop<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// loop sound</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioStream <span style="color: #339933;">=</span> assetManager.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytesWritten <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytesRead <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioStream.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, headerOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// read until end of file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>stop <span style="color: #339933;">&amp;&amp;</span> bytesWritten <span style="color: #339933;">&lt;</span> fileSize <span style="color: #339933;">-</span> headerOffset<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytesRead <span style="color: #339933;">=</span> audioStream.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, bufferSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytesWritten <span style="color: #339933;">+=</span> audioTrack.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, bytesRead<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioTrack.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; audioTrack.<span style="color: #006633;">release</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> e<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>Next audio playback of the AudioTrack object is started. Now we have to continuously feed audio data to the object. In a standard WAVE file the raw audio data starts at offset 0x2C, so we skip the header. The outer while loop is used for looping the sound sample indefinitely. The inner while loop reads the actual audio data and feeds it to the AudioTrack object until the end of the file is reached. If the stop flag is set, the loop stops execution and the thread is terminated. The requestStop method sets the stop flag.</p>
<div class="codecolorer-container java railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> requestStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stop <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h2>Conclusion</h2>
<p>The AudioTrackSoundPlayer class gives us a very simple interface for usage in musical instrument apps. Simultaneous low-latency playback of sound samples using Android&#8217;s AudioTrack API can be done quite easily by spawning threads for each sample. I hope this saves you some of the trouble I went through while figuring out Android audio.</p>
<p>To further improve the class, additional configuration settings could be added as well as support for compressed file formats like mp3 or ogg. Currently only PCM data (WAV) can be played.</p>
<p>The complete project including the code used for multi-touch handling can be downloaded here: <a href='http://www.martinhoeller.net/wp-content/uploads/2012/01/PianoTest.zip'>PianoTest.zip</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2012/01/13/developing-a-musical-instrument-app-for-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2012/01/13/developing-a-musical-instrument-app-for-android/</feedburner:origLink></item>
		<item>
		<title>Using a physical Joystick in a Cocos2D (for Mac) Game (Part 2)</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/E_DJKvK0dlw/</link>
		<comments>http://www.martinhoeller.net/2011/06/13/using-a-physical-joystick-in-a-cocos2d-for-mac-game-part-2/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 20:13:46 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[cocos2d]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=179</guid>
		<description><![CDATA[In the previous post I explained how to intercept joystick events in a Cocos2D for Mac project using DDHidLib. Now we will use this foundation to control a space ship. Adding a Space Ship Download the zip file containing the ...]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://www.martinhoeller.net/?p=146">previous post</a> I explained how to intercept joystick events in a Cocos2D for Mac project using DDHidLib. Now we will use this foundation to control a space ship.</p>
<h2>Adding a Space Ship</h2>
<p>Download the zip file containing the image of a space ship and a bullet to shoot <a href='http://www.martinhoeller.net/wp-content/uploads/2011/06/images.zip'>here</a>. Extract the zip and add both files to the Resources group in your Xcode project.</p>
<p>Now add a new class derived from CCNode and name it &#8220;Ship&#8221;. Open <strong>Ship.m</strong>. Add an init method:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// add ship sprite</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CCSprite <span style="color: #002200;">*</span>ship <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;ship.png&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>ship<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> self;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Open <strong>HelloWorldLayer.m</strong>. Remove the code which adds the &#8220;Hello World&#8221; label in the init-method. We don&#8217;t need that any more. Now add the following code at the end of init:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; CGSize winSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span>.winSize;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Ship alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">30.0</span>, winSize.height <span style="color: #002200;">/</span> 2.0f<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>self.ship<span style="color: #002200;">&#93;</span>;</div></div>
<p>Build an run the project. You should now see the space ship on the screen.</p>
<h2>Moving the Ship</h2>
<p>We will implement ship movement using booleans to control the directions. Open <strong>Ship.h</strong> and modify it so that it looks like this:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span><br />
<span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span><br />
<br />
<span style="color: #a61390;">@interface</span> Ship <span style="color: #002200;">:</span> CCNode <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">BOOL</span> moveUp;<br />
&nbsp; &nbsp; <span style="color: #a61390;">BOOL</span> moveDown;<br />
&nbsp; &nbsp; <span style="color: #a61390;">BOOL</span> moveLeft;<br />
&nbsp; &nbsp; <span style="color: #a61390;">BOOL</span> moveRight;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">BOOL</span> moveUp;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">BOOL</span> moveDown;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">BOOL</span> moveLeft;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">BOOL</span> moveRight;<br />
<br />
<span style="color: #a61390;">@end</span></div></div>
<p>In <strong>Ship.m</strong> add the @synthesize directives:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@synthesize</span> moveUp;<br />
<span style="color: #a61390;">@synthesize</span> moveDown;<br />
<span style="color: #a61390;">@synthesize</span> moveLeft;<br />
<span style="color: #a61390;">@synthesize</span> moveRight;</div></div>
<p>The actual movement will be handled in a Cocos2D standard update method:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>update<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span>dt<br />
<span style="color: #002200;">&#123;</span><br />
<span style="color: #6e371a;">#define VELOCITY&nbsp; &nbsp; 150</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; CGPoint position <span style="color: #002200;">=</span> self.position;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.moveUp<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; position.y <span style="color: #002200;">+=</span> VELOCITY <span style="color: #002200;">*</span> dt;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.moveDown<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; position.y <span style="color: #002200;">-=</span> VELOCITY <span style="color: #002200;">*</span> dt;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.moveRight<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; position.x <span style="color: #002200;">+=</span> VELOCITY <span style="color: #002200;">*</span> dt;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.moveLeft<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; position.x <span style="color: #002200;">-=</span> VELOCITY <span style="color: #002200;">*</span> dt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; self.position <span style="color: #002200;">=</span> position;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>VELOCITY is the amount of pixels per second the ship should move. The ship&#8217;s position is updated depending on which direction is currently active.</p>
<p>Register the ship object to be included in Cocos2D&#8217;s update calls by adding</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span>self scheduleUpdate<span style="color: #002200;">&#93;</span>;</div></div>
<p>to the init method. To clean up, also add a dealloc with the according unscheduleUpdate:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self unscheduleUpdate<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Open <strong>HelloWorldLayer.m</strong> and scroll down to the DDHidLib delegate methods. The ship can be controlled either with an analog stick or a d-pad, so we will implement movement behavior in the according delegate methods.</p>
<p>Analog:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#define THRESHOLD &nbsp; 2&lt;&lt;13 &nbsp; // this threshold defines the dead zone of analog sticks</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method gets called when an analog stick is moved on the x axis.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &lt; <span style="color: #002200;">-</span>THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveLeft <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &gt; THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveRight <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveLeft <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveRight <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method gets called when an analog stick is moved on the y axis.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &lt; <span style="color: #002200;">-</span>THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveUp <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &gt; THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveDown <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveUp <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveDown <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #002200;">&#125;</span></div></div>
<p>d-pad:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; povNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>povNumber<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valueChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// This method gets called when d-pad buttons are pressed. The value is direction in degrees * 100</span><br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// reset movement</span><br />
&nbsp; &nbsp; self.ship.moveUp <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; self.ship.moveDown <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; self.ship.moveLeft <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; self.ship.moveRight <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>value<span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// no movement</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveUp <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">4500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveUp <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveRight <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">9000</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveRight <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">13500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveRight <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveDown <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">18000</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveDown <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">22500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveDown <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveLeft <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">27000</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveLeft <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">31500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveLeft <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ship.moveUp <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Build and run. You should now be able to move the ship around using an analog stick or a d-pad.</p>
<h2>Shooting</h2>
<p>Open <strong>HelloWorldLayer.h</strong> and add a member variable:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">BOOL</span> shoot;</div></div>
<p>The BOOL flag will determine if new bullets should be fired or not.<br />
Open <strong>HelloWorldLayer.m</strong> and in the init method register the object to receive update: calls:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span>self scheduleUpdate<span style="color: #002200;">&#93;</span>;</div></div>
<p>Unregister in dealloc:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span>self unscheduleUpdate<span style="color: #002200;">&#93;</span>;</div></div>
<p>Add a new method that shoots a bullet:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>shootBullet<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CGSize winSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span>.winSize;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; CCSprite <span style="color: #002200;">*</span>bullet <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;bullet.png&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; bullet.position <span style="color: #002200;">=</span> ccpAdd<span style="color: #002200;">&#40;</span>self.ship.position, ccp<span style="color: #002200;">&#40;</span>bullet.contentSize.width <span style="color: #002200;">/</span> 2.0f, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>bullet<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>bullet runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCSequence actions<span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#91;</span>CCMoveBy actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span> position<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span>winSize.width, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#91;</span>CCCallFuncN actionWithTarget<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>removeBullet<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>A new sprite is instantiated and added to the layer. A CCMoveBy action is used to animate the bullet until it is out of view. Then the removeBullet: method is called:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>removeBullet<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>node <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>node stopAllActions<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self removeChild<span style="color: #002200;">:</span>node cleanup<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>This method stops all active actions and removes the bullet from the layer.<br />
Add the upadte: method which simply shoots bullets if the shoot flag is YES:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>update<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span>dt<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>shoot<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self shootBullet<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>To set the values for the shoot flags we implement two DDHidLib delegate methods which handle button presses:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick buttonDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>buttonNumber<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method would be a good place to start shooting bullets</span><br />
&nbsp; &nbsp; shoot <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick buttonUp<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>buttonNumber<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method would be a good place to stop shooting bullets</span><br />
&nbsp; &nbsp; shoot <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Build and run. Now you can move your ship AND shoot bullets!</p>
<p>You can download the complete Xcode project here: <a href='http://www.martinhoeller.net/wp-content/uploads/2011/06/JoystickTest_Final.zip'>JoystickTest_Final.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2011/06/13/using-a-physical-joystick-in-a-cocos2d-for-mac-game-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2011/06/13/using-a-physical-joystick-in-a-cocos2d-for-mac-game-part-2/</feedburner:origLink></item>
		<item>
		<title>Using a physical Joystick in a Cocos2D (for Mac) Game (Part 1)</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/MYwss8cp7tw/</link>
		<comments>http://www.martinhoeller.net/2011/06/09/using-a-physical-joystick-in-a-cocos2d-for-mac-game/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 15:58:23 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[cocos2d]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=146</guid>
		<description><![CDATA[Quite a while ago I started playing around with Cocos2D for Mac and so far I love it. As a first test project I am making a retro style side-scrolling space shoot-em&#8217;-up. While processing keyboard input is quite easy, the ...]]></description>
				<content:encoded><![CDATA[<p>Quite a while ago I started playing around with Cocos2D for Mac and so far I love it. As a first test project I am making a retro style side-scrolling space shoot-em&#8217;-up.<br />
While processing keyboard input is quite easy, the only thing I was really missing is built-in support for joysticks and gamepads. I could not find anything related to this topic on the official Cocos2D forums. You only get plenty of articles describing how to implement a soft-gamepad on the iPhone or iPad. I guess this is because the Mac port of Cocos2D is quite new.</p>
<p>After doing some research I found out that a joystick is a HID (Human Interface Device) and there is a crude, low level C API on the Mac to interface HIDs called IOHIDLib. Then I found out about Dave Dribin&#8217;s excellent DDHidLib library, an Objective-C wrapper around IOHIDLib. You can download it here: <a href="http://www.dribin.org/dave/software/#ddhidlib">DDHidLib</a>. In the first part of the tutorial I&#8217;ll show you how to integrate it in your Cocos2D project (I assume you use Xcode 4).</p>
<h2>Building the Framework</h2>
<ol>
<li>Download and extract the source code of DDHidLib from the above link.</li>
<li>Open the DDHidLib Xcode project.</li>
<li>Select the DDHidLib scheme.</li>
<li>Edit the scheme so it uses the Release build configuration.</li>
<li>Build the framework. If you run into problems here make sure the Base SDK is set to &#8220;Latest Mac OS X&#8221; in the target&#8217;s build settings.</li>
<li>Locate the framework in Finder for later use.</li>
</ol>
<h2>Demo Project</h2>
<p>Now I&#8217;ll take you through the process of integrating DDHidLib into a Cocos2D project.<br />
Create a new project in Xcode using the template called &#8220;cocos2d_macosx&#8221; and name it &#8220;JoystickTest&#8221;. Build and run. This should just display a Hello World label. Now to the fun part.</p>
<h3>Adding the Framework</h3>
<ol>
<li>Drag <strong>DDHidLib.framework</strong> into the Frameworks group of your demo project and make sure &#8220;Copy items&#8221; is checked. The framework will now be linked to the target.</li>
<li>Go to you project settings, select the JoystickTest target.</li>
<li>Select the &#8220;Build Phases&#8221; tab and add a new &#8220;Copy Files&#8221; phase.</li>
<li>Select destination &#8220;Frameworks&#8221; and also rename the phase to &#8220;Frameworks&#8221;.</li>
<li>Drag <strong>DDHidLib.framework</strong> from the project navigator into the build phase.</li>
<li>Build and run the project to see if everything works.</li>
</ol>
<h3>Intercepting Joystick Events</h3>
<ol>
<li>Open <strong>HelloWorldLayer.h</strong> and add a member variable:
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> HelloWorldLayer <span style="color: #002200;">:</span> CCLayer<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span>joysticks;<br />
<span style="color: #002200;">&#125;</span></div></div>
</li>
<li>
Open <strong>HelloWorldLayer.m</strong> and add these pieces:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &lt;DDHidLib/DDHidLib.h&gt;</span></div></div>
<p>At the end of the init method add</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// allJoysticks returns an array of all connected joysticks</span><br />
joysticks <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>DDHidJoystick allJoysticks<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;<br />
<br />
<span style="color: #11740a; font-style: italic;">// we want to be the delegate of the joysticks</span><br />
<span style="color: #002200;">&#91;</span>joysticks makeObjectsPerformSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>setDelegate<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;<br />
<br />
<span style="color: #11740a; font-style: italic;">// start listening to all joystick events</span><br />
<span style="color: #002200;">&#91;</span>joysticks makeObjectsPerformSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>startListening<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</div></div>
</li>
<li>
Now we can receive delegate methods which are called when the user interacts with the joystick.<br />
Add the following code at the end of the file. Use these delegate methods to set states or trigger actions in your game logic. The methods are pretty self explanatory.</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#pragma mark - Joystick stuff</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick buttonDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>buttonNumber<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method would be a good place to start shooting bullets</span><br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button down: %d&quot;</span>, buttonNumber<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick buttonUp<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>buttonNumber<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method would be a good place to stop shooting bullets</span><br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button up: %d&quot;</span>, buttonNumber<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<br />
<span style="color: #6e371a;">#define THRESHOLD &nbsp; 2&lt;&lt;13 &nbsp; // this threshold defines the dead zone of analog sticks</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method gets called when an analog stick is moved on the x axis.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &lt; <span style="color: #002200;">-</span>THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stick left&quot;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &gt; THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stick right&quot;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x axis neutral zone&quot;</span><span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// this method gets called when an analog stick is moved on the y axis.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &lt; <span style="color: #002200;">-</span>THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stick up&quot;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value &gt; THRESHOLD<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stick down&quot;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;y axis neutral zone&quot;</span><span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; otherAxis<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>otherAxis<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valueChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;otherAxis: %u &nbsp; value: %d&quot;</span>, otherAxis, value<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>ddhidJoystick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>DDHidJoystick <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>joystick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stick<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>stick<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; povNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>povNumber<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valueChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>value<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// This method gets called when d-pad buttons are pressed. The value is direction in degrees * 100</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>value<span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad none&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad N&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">4500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad NE&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">9000</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad E&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">13500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad SE&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">18000</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad S&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">22500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad SW&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">27000</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad W&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">case</span> <span style="color: #2400d9;">31500</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;d-pad NW&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">default</span><span style="color: #002200;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #002200;">&#125;</span></div></div>
</li>
<li>Connect a joystick or pad to your Mac and try it out!</li>
</ol>
<p>You can download the finished Xcode project here: <a href='http://www.martinhoeller.net/wp-content/uploads/2011/06/JoystickTest.zip'>JoystickTest.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2011/06/09/using-a-physical-joystick-in-a-cocos2d-for-mac-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2011/06/09/using-a-physical-joystick-in-a-cocos2d-for-mac-game/</feedburner:origLink></item>
		<item>
		<title>Custom Tab Bars</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/CDTVwqVibL0/</link>
		<comments>http://www.martinhoeller.net/2011/05/07/custom-tab-bars/#comments</comments>
		<pubDate>Sat, 07 May 2011 16:19:02 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=114</guid>
		<description><![CDATA[For Gigster&#8217;s navigation concept it was necessary to implement a custom styled tab bar. Basically the screen is divided into three parts: Navigation Bar Tab Bar Content The navigation bar has a custom background image and a fixed title with ...]]></description>
				<content:encoded><![CDATA[<p>For Gigster&#8217;s navigation concept it was necessary to implement a custom styled tab bar. Basically the screen is divided into three parts: </p>
<ul>
<li>Navigation Bar</li>
<li>Tab Bar</li>
<li>Content</li>
</ul>
<p><img src="http://www.martinhoeller.net/wp-content/uploads/2011/05/gigster_nav_parts.png" alt="" title="gigster_nav_parts" width="439" height="547" class="aligncenter size-full wp-image-115" /></p>
<p>The navigation bar has a custom background image and a fixed title with custom font. The tab bar should be directly under the navigation bar and has complete custom styling.</p>
<h2>Basic Setup</h2>
<p>To start off I created a new Xcode project and added a UITabBarController with three static views. Although I have XIBs for each content view, I added everything else in code. Each view has a UINavigationController so it can easily contain its own navigation stack.</p>
<h2>Navigation Bar</h2>
<p>An easy technique to get a custom background in a UINavigationBar is to make a cateogory. Add this code (e.g. in YourAppDelegate.m):</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> UINavigationBar <span style="color: #002200;">&#40;</span>MyCustomNavBar<span style="color: #002200;">&#41;</span><br />
<span style="color: #a61390;">@end</span><br />
<span style="color: #a61390;">@implementation</span> UINavigationBar <span style="color: #002200;">&#40;</span>MyCustomNavBar<span style="color: #002200;">&#41;</span><br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>barImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;nav_bg.png&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>barImage drawInRect<span style="color: #002200;">:</span>rect<span style="color: #002200;">&#93;</span>; &nbsp; &nbsp;<br />
<span style="color: #002200;">&#125;</span><br />
<span style="color: #a61390;">@end</span></div></div>
<p>To get the fixed title with custom font I added a method that creates a UILabel:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UILabel <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_makeTitleLabel<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CGRect frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">44</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; UILabel <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; label.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; label.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Marker Felt&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">24</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; label.shadowColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span><span style="color: #2400d9;">0.0</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; label.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;<br />
&nbsp; &nbsp; label.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; label.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Custom Tab Bar&quot;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> label;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// add static title labels</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>tab1NavigationController.navigationBar addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self _makeTitleLabel<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>tab2NavigationController.navigationBar addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self _makeTitleLabel<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>tab3NavigationController.navigationBar addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self _makeTitleLabel<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; ...<br />
<span style="color: #002200;">&#125;</span></div></div>
<h2>Tab Bar</h2>
<p>A little bit of frame hackery was necessary to get the tab bar right beneath the navigation bar:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// disable autosizing of tabbar and move it to correct position</span><br />
&nbsp; &nbsp; tabBarController.tabBar.autoresizingMask <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; tabBarController.tabBar.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">44</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">65</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// fix frame of tabbarcontroller's view</span><br />
&nbsp; &nbsp; CGRect frame <span style="color: #002200;">=</span> tabBarController.view.frame;<br />
&nbsp; &nbsp; frame.size.height <span style="color: #002200;">+=</span> <span style="color: #2400d9;">29</span>;<br />
&nbsp; &nbsp; frame.origin.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">20</span>;<br />
&nbsp; &nbsp; tabBarController.view.frame <span style="color: #002200;">=</span> frame;</div></div>
<p>To have a custom layout for the tab bar, it is necessary to subclass UITabBarController. We don&#8217;t need UIKit&#8217;s tab bar at all, so we just hide it and create our own custom tabs:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// find the normal tab bar and hide it</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span>view <span style="color: #a61390;">in</span> self.view.subviews<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>view isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UITabBar class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self _setupCustomTabBar<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>First I added a UIImageView containing a background image:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>_setupCustomTabBar<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// background image</span><br />
&nbsp; &nbsp; self.bgImageView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tabs_bg.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; bgImageView.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">44</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">65</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>bgImageView<span style="color: #002200;">&#93;</span>;</div></div>
<p>Next we want to display a tab image to indicate which tab is active:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; self.contentView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">44</span>, <span style="color: #2400d9;">640</span>, <span style="color: #2400d9;">65</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>self.contentView<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// sliding tab image</span><br />
&nbsp; &nbsp; self.tabImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; CGRect frame <span style="color: #002200;">=</span> self.tabImage.frame;<br />
&nbsp; &nbsp; frame.origin <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span>BUTTON_X_OFFSET, BUTTON_Y<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; self.tabImage.frame <span style="color: #002200;">=</span> frame;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.contentView addSubview<span style="color: #002200;">:</span>self.tabImage<span style="color: #002200;">&#93;</span>;</div></div>
<p>Finally I added three buttons to trigger tab selection:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Make custom tab buttons and add them to the content view</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.contentView addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self _makeTabButtonWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 1&quot;</span> atIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.contentView addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self _makeTabButtonWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 2&quot;</span> atIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.contentView addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self _makeTabButtonWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 3&quot;</span> atIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Button creation method:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_makeTabButtonWithTitle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>title<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>index<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UIButton <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>BUTTON_X_OFFSET <span style="color: #002200;">+</span> index<span style="color: #002200;">*</span>BUTTON_WIDTH, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; button.tag <span style="color: #002200;">=</span> index;<br />
&nbsp; &nbsp; button.titleLabel.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Marker Felt&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">18</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; button.titleLabel.shadowColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; button.titleLabel.shadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>button setTitle<span style="color: #002200;">:</span>title forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>_buttonClicked<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> button;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Note that the buttons&#8217; action is _buttonClicked. There we need to set the currently selected tab index in the UITabBarController.</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>_buttonClicked<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; self.selectedIndex <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sender tag<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self _updateTabImage<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>We also want to update the tab image&#8217;s position after a tab was selected. This is what _updateTabImage is for:</p>
<div class="codecolorer-container objc railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>_updateTabImage<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CGRect targetRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>BUTTON_X_OFFSET <span style="color: #002200;">+</span> self.selectedIndex<span style="color: #002200;">*</span>BUTTON_WIDTH, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">0.3</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>UIView setAnimationCurve<span style="color: #002200;">:</span>UIViewAnimationCurveEaseInOut<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.tabImage setFrame<span style="color: #002200;">:</span>targetRect<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Now we have a fully functioning tab bar with custom layout. You can download the full Xcode project here: <a href='http://www.martinhoeller.net/wp-content/uploads/2011/05/MyCustomTabBar_final.zip'>MyCustomTabBar_final.zip</a>.</p>
<p>Gigster in the App Store: <a href="http://itunes.apple.com/us/app/gigster/id424607668?mt=8&#038;ls=1">Jump</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2011/05/07/custom-tab-bars/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2011/05/07/custom-tab-bars/</feedburner:origLink></item>
		<item>
		<title>Open run log automatically in Xcode 4</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/J3UNDdo9_gY/</link>
		<comments>http://www.martinhoeller.net/2011/03/25/open-run-log-automatically-in-xcode-4/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 10:42:13 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=105</guid>
		<description><![CDATA[When I switched to Xcode 4, I missed the ability to open a run log window automatically when I run a project. Instead, Xcode 4 has a debug area with log output on the bottom of the main window. Because ...]]></description>
				<content:encoded><![CDATA[<p>When I switched to Xcode 4, I missed the ability to open a run log window automatically when I run a project. Instead, Xcode 4 has a debug area with log output on the bottom of the main window. Because there is lots of log output in our projects and I don&#8217;t want to resize the debug area every time, I tried to reproduce the behavior of Xcode 3 (sort of). This article will show you how to do it.</p>
<p>A new feature in Xcode 4 are &#8220;Behaviors&#8221; which can be configured in the preferences window. Basically you can tell Xcode what to do after certain events. </p>
<ol>
<li>Open any project in Xcode 4.</li>
<li>Show the tab bar if it is not visible yet (View > Show Tab Bar)</li>
<li>Add a new tab by pressing CMD-T</li>
<li>Rename the new tab by double clicking its name. Call it &#8220;LOG&#8221; and drag it to the left in the tab bar.</li>
<li>Configure the tab to only show the information you need. I like to have all the available space for log output. Drag up the debug area all the way to the top. I also hide the utilities section on the right. Within the debug area I only have the console visible.
<div align="center">
<a href="http://www.martinhoeller.net/wp-content/uploads/2011/03/window_config.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2011/03/window_config.png" alt="" title="window_config" width="177" height="161" class="aligncenter size-full wp-image-97" /></a>
</div>
</li>
<li>Open the preferences window and select &#8220;Behaviors&#8221;</li>
<li>Because we want the log to open when an application is run, select &#8220;Run starts&#8221;.</li>
<li>Click the checkbox &#8220;Show Tab&#8221; and enter &#8220;LOG&#8221; in the textbox.
<div align="center">
<a href="http://www.martinhoeller.net/wp-content/uploads/2011/03/behaviors.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2011/03/behaviors-300x220.png" alt="" title="behaviors" width="300" height="220" class="aligncenter size-medium wp-image-96" /></a>
</div>
</li>
</ol>
<p>That&#8217;s it! From now on whenever you run your project, Xcode automatically jumps to the LOG tab if it exists.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2011/03/25/open-run-log-automatically-in-xcode-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2011/03/25/open-run-log-automatically-in-xcode-4/</feedburner:origLink></item>
		<item>
		<title>New Apps available</title>
		<link>http://feedproxy.google.com/~r/MartinsDevBlog/~3/ZwfOXwe6Fb4/</link>
		<comments>http://www.martinhoeller.net/2011/03/25/new-apps-available/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 10:32:59 +0000</pubDate>
		<dc:creator>mhoeller</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.martinhoeller.net/?p=94</guid>
		<description><![CDATA[Hi, Finally, both our new apps are now available in the App Stores. Gigster Setlists at your fingertips Create, play and share setlists for gigs and shows as a band, individual artist, manager or fan. CREATE. Enter your songs, along ...]]></description>
				<content:encoded><![CDATA[<p>Hi,</p>
<p>Finally, both our new apps are now available in the App Stores.</p>
<h2>Gigster</h2>
<p><a href="http://www.martinhoeller.net/wp-content/uploads/2011/03/gigster_icon.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2011/03/gigster_icon.png" alt="" title="gigster_icon" width="212" height="199" class="aligncenter size-full wp-image-95" /></a></p>
<p><em>Setlists at your fingertips</em><br />
Create, play and share setlists for gigs and shows as a band, individual artist, manager or fan.</p>
<p>CREATE.<br />
Enter your songs, along with relevant information such as tempo, key or your instrument&#8217;s settings. Create and save setlists of your songs for your tours and gigs.</p>
<p>PLAY.<br />
Gigster&#8217;s live view is the perfect tool for your rehearsal sessions and live on stage &#8211; let it guide you through your setlist in a beautifully designed interface, providing you with important notes and settings for each song.</p>
<p>SHARE.<br />
Tell the world about your band&#8217;s fabulous gig, or your friends about the great show you just attended, by sharing your setlist on Facebook.</p>
<p><a href="http://itunes.apple.com/us/app/gigster/id424607668?mt=8" target="_blank">iTunes Link</a></p>
<p><br/><br/><br/></p>
<h2>Savior</h2>
<p><a href="http://www.martinhoeller.net/wp-content/uploads/2011/03/Savior.png"><img src="http://www.martinhoeller.net/wp-content/uploads/2011/03/Savior.png" alt="" title="Savior" width="175" height="175" class="aligncenter size-full wp-image-100" /></a></p>
<p>SAVE EARLY, SAVE OFTEN<br />
Sometimes, even the most well-crafted applications crash. Let Savior auto-save all your opened documents and never lose unsaved changes again. Better to have saved than be sorry.</p>
<p>CONCISE<br />
You can easily configure how Savior behaves. Set the autosave interval, select which applications are being taken into account and whether only the currently active document is being saved.</p>
<p>LEAN AND CLEAR<br />
Savior is easy to use and provides relevant information at a glance. Use Growl notifications to keep informed about your documents&#8217; status.</p>
<p>ACTIVELY DEVELOPED<br />
Savior just works. Still, it is constantly being improved in order to extend compatibility with popular applications and introduce new, great features.</p>
<p><a href="http://itunes.apple.com/us/app/savior/id422587934?mt=12" target="_blank">iTunes Link</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinhoeller.net/2011/03/25/new-apps-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.martinhoeller.net/2011/03/25/new-apps-available/</feedburner:origLink></item>
	</channel>
</rss>
