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

<channel>
	<title>coders &#187; xcode</title>
	<atom:link href="http://www.gehacktes.net/category/programming/xcode/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gehacktes.net</link>
	<description>The Limits of My Language Mean the Limits of My World - Ludwig Wittgenstein</description>
	<lastBuildDate>Sat, 31 Jul 2010 12:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Animas newest iPhone App: Earth Defender</title>
		<link>http://www.gehacktes.net/2010/07/animas-newest-iphone-app-earth-defender/</link>
		<comments>http://www.gehacktes.net/2010/07/animas-newest-iphone-app-earth-defender/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 12:39:13 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[humor]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[productivity improvement]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=843</guid>
		<description><![CDATA[Defend the Earth against hordes of aliens in Earth Defender&#8482;, the highly addictive, classic arcade shooter for iPhone and iPod touch available on the App Store now! Check out the Video on YouTube, too! Get all the latestes news first and follow us on Twitter and Facebook Earth Defender is know available in the appstore(10th [...]]]></description>
			<content:encoded><![CDATA[<p id="ae">
	<img src="http://www.anima-entertainment.de/signature_ed.jpg" alt="" /><br />Defend the Earth against hordes of aliens in<strong> Earth  Defender&trade;</strong>, the highly addictive, classic arcade shooter for iPhone and  iPod touch available on the <strong><a moz-do-not-send="true" href="http://itunes.apple.com/de/app/earth-defender/id381290013?mt=8" title="http://itunes.apple.com/de/app/earth-defender/id381290013?mt=8">App Store</span></a></strong> now! Check out the </span></strong><strong><a moz-do-not-send="true" href="http://www.youtube.com/watch?v=KnPktbMqU-I" title="http://www.youtube.com/watch?v=nigfYp6pxUI">Video</a></strong> on YouTube, too!</p>
<p id="ae">
	<img style="margin-top:15px;margin-bottom:10px;" src="http://www.anima-entertainment.de/signature_ae.jpg" alt="ANIMA Entertainment" />
</p>
<p id="ae">Get all the latestes news first and follow us on <a href="http://twitter.com/animagames" target="_blank">Twitter</a> and <a href="http://www.facebook.com/pages/Bremen-Germany/ANIMA-Entertainment/105874927988?v=wall" target="_blank">Facebook</a></p>
<p><a href="http://itunes.apple.com/us/app/earth-defender/id381290013?mt=8">Earth Defender</a> is know available in the <a href="http://itunes.apple.com/us/app/earth-defender/id381290013?mt=8">appstore</a>(10th best Overall App in Germany)</p>
<p><img src="http://www.anima-entertainment.de/img/ed10.jpg" alt="10. Earth Defender" /></p>
<p>Would be nice to be notified in other countries, too. Its a fast paced action tap shooter packed with great sound and beautiful graphics: &#8220;Like Independence Day, but on a really small screen.&#8221;</p>
<p>MobileTechReview:<br />
Earth Defender positively radiant 3D graphics  that outshine many iOS 3D games. The environment looks stunning with detailed animated backgrounds and elaborate visual effects. Enemy design look like 3D Galaga and levels look very realistic. </p>
<p>PocketGamer:<br />
What makes Earth Defender stand out from similar games is the sheer feel of it. Taking on the enemy fleet looks and feels like you’re engaging in the kind of epic space battles seen in Babylon 5 or Battlestar Galactica.</p>
<p>trailer : http://www.youtube.com/watch?v=nigfYp6pxUI</p>
<p><a href="http://itunes.apple.com/us/app/earth-defender/id381290013?mt=8">Buy It !</a></p>
<p>Screenshots:<br />
<img src="http://www.anima-entertainment.de/img/EarthDefenderMenu.jpg" /><img src="http://www.anima-entertainment.de/img/EarthDefenderScreen.jpg" /><img src="http://www.anima-entertainment.de/img/EarthDefenderScreen2.jpg" /><img src="http://www.anima-entertainment.de/img/EarthDefenderScreen3.jpg" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/07/animas-newest-iphone-app-earth-defender/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Programming: How to make an Apple Push Notification Service using PHP</title>
		<link>http://www.gehacktes.net/2010/04/iphone-programming-how-to-make-an-apple-push-notification-service-using-php/</link>
		<comments>http://www.gehacktes.net/2010/04/iphone-programming-how-to-make-an-apple-push-notification-service-using-php/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 19:00:48 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[objective c]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>
		<category><![CDATA[APN]]></category>
		<category><![CDATA[Notification]]></category>
		<category><![CDATA[Push]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=805</guid>
		<description><![CDATA[Apple Push Notification Service (APNS) alows you to send messages to your iPhone-App even your app is not running. Once a user installed your iPhone-App and agreed to the push service and is somehow reachable by APNS your message will be deployed. Ok, here is how it works. Any iPhone device has a unique token [...]]]></description>
			<content:encoded><![CDATA[<p>Apple Push Notification Service (APNS) alows you to send messages to your iPhone-App even your app is not running. Once a user installed your iPhone-App and agreed to the push service and is somehow reachable by APNS your message will be deployed. <span id="more-805"></span></p>
<p>Ok, here is how it works. Any iPhone device has a unique <i>token</i> which is the identifier the APNS uses to send message to. A device token is a 64 byte identifier. Your iPhone App has to send the device token manualy to your server and if you want to send something to this device token your server code has to store it somehow. Later on when sending your notification message to your device token you create a package which is capable of 256 Byte in total including all informations (even the device token itself is included).</p>
<p>Within you XCode App you could optain your device token by adding the following application delegate to your app :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//////////////////////////////////////////////</span>
<span style="color: #11740a; font-style: italic;">// CLIENT SIDE CODE</span>
<span style="color: #11740a; font-style: italic;">//////////////////////////////////////////////</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>app didRegisterForRemoteNotificationsWithDeviceToken<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>devToken 
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">*</span>userDefaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// to make sure your device will be just registered once, we store</span>
    <span style="color: #11740a; font-style: italic;">// the state in userDefaults.</span>
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>userDefaults boolForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DeviceTokenRegistered&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>  <span style="color: #11740a; font-style: italic;">//if ( null, nil or &quot;false&quot; )</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>userDefaults setBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DeviceTokenRegistered&quot;</span><span style="color: #002200;">&#93;</span>; 
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Device Token: %@&quot;</span>, devToken<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// log the device token as ascii</span>
&nbsp;
        <span style="color: #11740a; font-style: italic;">// send it somehow to your server - apple says the best way is to</span>
        <span style="color: #11740a; font-style: italic;">// send it as bytes : ( assuming you wrote a &quot;sendProviderDeviceToken&quot; method )</span>
        <span style="color: #002200;">&#91;</span>self sendProviderDeviceToken<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>devToken bytes<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">/*
    UIRemoteNotificationTypeNone
    UIRemoteNotificationTypeBadge
    UIRemoteNotificationTypeSound
    UIRemoteNotificationTypeAlert 
    */</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> registerForRemoteNotificationTypes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; 
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Once you created a push notification package you have to authenticate your self as developer for the sandbox APNS-server or as distributor for the real APNS-server. This authentification is done by a so called pem file. To create one of these you have first generate an Apple Push Notification SSL certificate on your Mac.</p>
<ol>
<li>Ensure to have a valid and working AppID without wildcard at your iPhone Developer Center. You cannot push a notification to an appID with wildcards. Your app ID should look like <code>xxxxxxxxxx.com.yourCompanyName.yourAppName</code></li>
<li>Inside Apple Developer Center enable Push Notification your app from step 1.</li>
<li>Import your <code>apns_developer_identity.cer</code> into your keychain on your mac.</li>
<li>Launch the keychain assistant on your mac and go to certificates category and expand the option <code>Apple Develoment Push Services</code>.</li>
<li>If you right click on <code>Apple Development Push Services</code> choose export <code>Apple Development Push Services ID888</code> and save it as <code>apns-dev-cert.p12</code> file.</li>
<li>Do the same for the private key but save it as <code>apns-dev-key.p12</code>.</li>
<li>Do convert both files to PEM format via:
<pre>openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12</pre>
</li>
<li>to disable the passphrase execute:
<pre>openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem</pre>
</li>
<li>The last step is to combine the pem files via:
<pre>cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem
</pre>
</li>
</ol>
<p>Now put the pem file in a directory where you can access it within your php code &#8211; wich itself is pretty simple. First you have to open a connection to the APNS (<code>gateway.push.apple.com</code> for distribution or <code>gateway.sandbox.push.apple.com</code> for development). In this example I will use the sandbox server:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ctx</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #666666; font-style: italic;">//////////////////////////////////////////////</span>
<span style="color: #666666; font-style: italic;">// SERVER SIDE CODE</span>
<span style="color: #666666; font-style: italic;">//////////////////////////////////////////////</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// send our authentification file</span>
<span style="color: #990000;">stream_context_set_option</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ctx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ssl'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'local_cert'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'apns-dev.pem'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// open a connection to the sandbox server</span>
<span style="color: #000088;">$apnsConnection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_socket_client</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ssl://gateway.sandbox.push.apple.com:2195'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$err</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> STREAM_CLIENT_CONNECT<span style="color: #339933;">,</span> <span style="color: #000088;">$ctx</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// creating the notification message (256 Byte max)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// your device token here</span>
<span style="color: #000088;">$deviceToken</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'e6636942 e1d5cf29 924628c3 115004af 1caba414 d4e52098 023fef08 07954d6a'</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// your message goes here</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Your push notification text goes here.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define a sound file if you want here</span>
<span style="color: #000088;">$sound</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'mySoundFile.caf'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//name of the sound file inside the XCode project.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Construct the notification payload</span>
<span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aps'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'alert'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$badge</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// a number which will be displayed over the apps icon</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aps'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'badge'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$badge</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sound</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aps'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sound'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sound</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$payload</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// remove blanks and pack it.</span>
<span style="color: #000088;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'H*'</span><span style="color: #339933;">,</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$deviceToken</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$payload</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$payload</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// push the message through APNS</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$apnsConnection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">//close APNS connection</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$apnsConnection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/04/iphone-programming-how-to-make-an-apple-push-notification-service-using-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Programming iPhone Games with Cocos2D Part 4</title>
		<link>http://www.gehacktes.net/2010/03/programming-iphone-games-with-cocos2d-part-4/</link>
		<comments>http://www.gehacktes.net/2010/03/programming-iphone-games-with-cocos2d-part-4/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 15:15:18 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=693</guid>
		<description><![CDATA[Cocos2D gives you a huge amount of features and all that stuff you need and want in your game. But one thing is really missing &#8211; a simple collision detection between sprites. In this article I want to give you some hints how you could implement your own CCSprite collision detection routine without using box2d [...]]]></description>
			<content:encoded><![CDATA[<p>Cocos2D gives you a huge amount of features and all that stuff you need and want in your game. But one thing is really missing &#8211; a simple collision detection between sprites. In this article I want to give you some hints how you could implement your own CCSprite collision detection routine without using box2d or other physic libraries.<br />
<span id="more-693"></span></p>
<h1>Update Calls</h1>
<p>Ok, lets say you want to detect a collision between two objects A and B by checking one with the other. One way would be to enhance your CCSprite with an update routine which will detect its collisions by checking collision with all other children. The pseudo-code would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// assuming we are implementing an update routine </span>
<span style="color: #11740a; font-style: italic;">// for an extended CCSprite class, lets say: CCSpriteExt.m</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span> delta
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span> you <span style="color: #a61390;">in</span> children<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> you <span style="color: #002200;">==</span> self<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">continue</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self isCollidingWithObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> 
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// collision detected !</span>
			<span style="color: #002200;">&#91;</span>collisionDelegate performSelector<span style="color: #002200;">:</span>collisionSelector withObject<span style="color: #002200;">:</span>self withObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Lets say you have 5 Objects (A,B,C,D and E) and you use the above code to check the collisions, you will count 20 check calls where 10 calls are double :</p>
<pre>
AB, AC, AD, AE -> 4 checks, 0 double checks
BA, BC, BD, BE -> 4 checks, 1 double checks: AB
CA, CB, CD, CE -> 4 checks, 2 double checks: AC, BC
DA, DB, DC, DE -> 4 checks, 3 double checks: AD, BD, CD
EA, EB, EC, ED -> 4 checks, 4 double checks: AE, BE, CE, DE
20 total checks, 10 double checks
</pre>
<p>If you have many objects this really would take time. </p>
<p>Another solution would be one update call for all you objects. Like if you have a game scene or something which allready has an update call, you could place your collision detection there and write a more efficiant object loop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// assuming we are implementing an udate routine in a CCScene or CCLayer class</span>
..
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxMe<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; idxMe &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxMe<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	CCNode <span style="color: #002200;">*</span> me <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxMe<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxYou<span style="color: #002200;">=</span>idxMe<span style="color: #002200;">+</span><span style="color: #2400d9;">1</span>; idxYou &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxYou<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>		
		CCNode <span style="color: #002200;">*</span> you <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxYou<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self isCollidingObject<span style="color: #002200;">:</span>Me WithObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> 
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// collision detected !</span>
			<span style="color: #002200;">&#91;</span>collisionDelegate performSelector<span style="color: #002200;">:</span>collisionSelector withObject<span style="color: #002200;">:</span>me withObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So now you would&#8217;nt have any double check calls:
</pre>
<pre>
AB, AC, AD, AE -> 4 checks, 0 double checks
BC, BD, BE -> 3 checks, 0 double checks
CD, CE -> 2 checks, 0 double checks
DE -> 1 checks, 0 double checks
10 total checks, 0 double checks
</pre>
<p>if you have objects in your scene which you dont want to test in your collision detection or even better if you want to have collision groups you have to advance your CCSprite with a collisionGroupID. To be sure the collision detection will be as fast as possible you should check the collisionGroupID <b>before</b> you check the collision it self :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// assuming we are implementing an udate routine in a CCScene or CCLayer class</span>
..
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxMe<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; idxMe &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxMe<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	CCNode <span style="color: #002200;">*</span> meNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxMe<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> <span style="color: #002200;">&#91;</span>meNode isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCSpriteExt class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">continue</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	CCSpriteExt <span style="color: #002200;">*</span> me <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> meNode;
&nbsp;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxYou<span style="color: #002200;">=</span>idxMe<span style="color: #002200;">+</span><span style="color: #2400d9;">1</span>; idxYou &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxYou<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>		
		CCNode <span style="color: #002200;">*</span> youNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxYou<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> <span style="color: #002200;">&#91;</span>youNode isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCSpriteExt class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">continue</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		CCSpriteExt <span style="color: #002200;">*</span> you <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> youNode;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>me.collisionID <span style="color: #002200;">==</span> you.collisionID<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> 
		    <span style="color: #002200;">&#91;</span>self isCollidingObject<span style="color: #002200;">:</span>Me WithObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// collision detected !</span>
			<span style="color: #002200;">&#91;</span>collisionDelegate performSelector<span style="color: #002200;">:</span>collisionSelector withObject<span style="color: #002200;">:</span>me withObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now take a deep breath before we take a closer look into the method <b>isCollidingObject:WithNode:</b>. To keep the performance and avoid lags you should know how many collision calls you could have and how time intensive they are - even if you optimize your collision detection method pretty much. </p>
<h1>Collision Detection Method</h1>
<p>You should start by testing a bounding box even if you want a pixel by pixel collision detection to save cpu time. May be its a good idea to setup a collision type for each CCSpriteExt and when detecting the collision you could choose the best algorithm for it. Lets say there are 4 collision body types: BOUNDING_BOX, BOUNDING_SPHERE, POLYGON and PIXEL. All of them starts with bounding box since this is a really easy thing for the cpu. If you are not rotating your sprites you could use CCSprite.contentSize and CCSprite.anchorPoint to calculate your bounding box. You could also use CCSprite.boundingBox which returns the current transformed bounding box.</p>
<p>To keep all colliding stuff in one method you should shift the code "(me.collisionID == you.collisionID)" into this function, too and remove it from the collision test loop. To make this example not to difficult I will just test collisions with the same collision body type and I will not implement a pixel by pixel test routine here.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSpriteExt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>obj1 WithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSpriteExt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> obj1.collisionID <span style="color: #002200;">==</span> obj1.collisionID <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> obj1.collisionBodyType <span style="color: #002200;">!=</span> obj2.collisionBodyType<span style="color: #002200;">&#41;</span> 
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// not supported yet.</span>
			<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>obj1.collisionBodyType <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">case</span> BOUNDING_BOX <span style="color: #002200;">:</span> 
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>; 
&nbsp;
			<span style="color: #a61390;">case</span> BOUNDING_SPHERE <span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #11740a; font-style: italic;">// first boxed test!</span>
			<span style="color: #002200;">&#91;</span>self isCollidingSphere<span style="color: #002200;">:</span>obj1 WithSphere<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">case</span> POLYGON <span style="color: #002200;">:</span> 
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #11740a; font-style: italic;">// first boxed test!</span>
			<span style="color: #002200;">&#91;</span>self isCollidingPolygon<span style="color: #002200;">:</span>obj1 WithPolygon<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">case</span> PIXEL <span style="color: #002200;">:</span> 
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #11740a; font-style: italic;">// first boxed test!</span>
			<span style="color: #002200;">&#91;</span>self isCollidingPixel<span style="color: #002200;">:</span>obj1 WithPixel<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #11740a; font-style: italic;">// not supported type</span>
			<span style="color: #a61390;">break</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Since all collision tests need a boundingbox test first lets take a look into its method. Basically you want to check if a bounding box intersects with another bounding box. If you dont have transformed bounding boxes you could check the intersection even more efficiant. Two sprites are colliding if the vertical distances between anchor points and edges of both sprites are smaller than the vertical distance between them and the horizontal distances between anchor points and edges of both sprites are smaller than the horizontal distance between them.<br />
<a href="http://www.gehacktes.net/wp-content/uploads/2010/03/picture.png"><img src="http://www.gehacktes.net/wp-content/uploads/2010/03/picture.png" alt="" title="picture" width="380" height="320" class="alignnone size-full wp-image-697" /></a></p>
<p>Non-transformed bounding boxes goes here :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingBox <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj1 WithBox<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2 
<span style="color: #002200;">&#123;</span>	
	<span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> <span style="color: #a61390;">fabs</span><span style="color: #002200;">&#40;</span>obj2.position.x <span style="color: #002200;">-</span> obj1.position.x<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> <span style="color: #a61390;">fabs</span><span style="color: #002200;">&#40;</span>obj2.position.y <span style="color: #002200;">-</span> obj1.position.y<span style="color: #002200;">&#41;</span>;	
&nbsp;
	<span style="color: #a61390;">float</span> dx1;
	<span style="color: #a61390;">float</span> dx2;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>obj2.position.x &gt; obj1.position.x<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		dx1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">-</span>obj1.collisionSprite.anchorPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj1.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj1.collisionSprite.scaleX;
		dx2 <span style="color: #002200;">=</span> obj2.collisionSprite.anchorPoint.x <span style="color: #002200;">*</span> obj2.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj2.collisionSprite.scaleX;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> 
	<span style="color: #002200;">&#123;</span>
		dx1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>obj1.collisionSprite.anchorPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span>  obj1.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj1.collisionSprite.scaleX;
		dx2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">-</span>obj2.collisionSprite.anchorPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span>obj2.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj2.collisionSprite.scaleX;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">float</span> dy1;
	<span style="color: #a61390;">float</span> dy2;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>obj2.position.y &lt; obj1.position.y<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		dy1 <span style="color: #002200;">=</span> obj1.collisionSprite.anchorPoint.y <span style="color: #002200;">*</span> obj1.collisionSprite.contentSize.height <span style="color: #002200;">*</span> obj1.collisionSprite.scaleY;
		dy2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">-</span> obj2.collisionSprite.anchorPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj2.collisionSprite.contentSize.height <span style="color: #002200;">*</span> you.collisionSprite.scaleY;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> 
	<span style="color: #002200;">&#123;</span>
		dy1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">-</span> obj1.collisionSprite.anchorPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj1.collisionSprite.contentSize.height <span style="color: #002200;">*</span> obj1.collisionSprite.scaleY;
		dy2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>obj2.collisionSprite.anchorPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj2.collisionSprite.contentSize.height <span style="color: #002200;">*</span> obj2.collisionSprite.scaleY;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>dx &gt; <span style="color: #002200;">&#40;</span>dx1<span style="color: #002200;">+</span>dx2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>dy &gt; <span style="color: #002200;">&#40;</span>dy1<span style="color: #002200;">+</span>dy2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>But if you have transformed bounding boxes use cocos boundingbox function and CGRechtIntersection to test the collision - and even more it is human readable ;-) :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingBox <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj1 WithBox<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span>CGRectIsNull<span style="color: #002200;">&#40;</span> CGRectIntersection<span style="color: #002200;">&#40;</span>obj1.boundingBox, obj2.boundingBox<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;	
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The spherical collision detection needs a radius to test the collision. A collision is detected if the distance between two objects is smaller than the sum of radius. If you dont want to enhance CCSprite with a radius use CCSprite.contentSize.width/2 or CCSprite.boundingBox.width/2 or what ever.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingSphere<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSpriteExt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj1 WithSphere<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSprite <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> minDistance <span style="color: #002200;">=</span> object.radius <span style="color: #002200;">+</span> self.radius;
	<span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> obj2.position.x <span style="color: #002200;">-</span> obj1.position.x;
	<span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> obj2.position.y <span style="color: #002200;">-</span> obj1.position.y;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span> <span style="color: #002200;">&#40;</span>dx &gt; minDistance || dy &gt; minDistance<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">float</span> actualDistance <span style="color: #002200;">=</span> <span style="color: #a61390;">sqrt</span><span style="color: #002200;">&#40;</span> dx <span style="color: #002200;">*</span> dx <span style="color: #002200;">+</span> dy <span style="color: #002200;">*</span> dy <span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>actualDistance &lt; <span style="color: #002200;">=</span> minDistance<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>If you dont want mix up CCSprite with your collision detection code you could also try to create a new class "CCCollisionBody" and put all the collision attributes like "radius" and "collisionBodyType" there and also a reference to your sprite so you can access the position and dimensions of your sprite :</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CCCollisionBody.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * collision body types
 * @version 1.0
 */</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_NONE		0</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_POINT		1</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_LINE		2</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_SPHERE	3</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_RECT		4</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_POLY		5</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_PIXEL		6</span>
&nbsp;
<span style="color: #a61390;">@class</span> CCNode; <span style="color: #11740a; font-style: italic;">// pre decleration of CCNode, import will be in .m file</span>
<span style="color: #11740a; font-style: italic;">/** 
 * A class to define the collision body for a proxy game object.
 * collisionbodys are classified by types. Not all types are yet defined to collide with each other.
 * please use sphere and rect collisionbodys only to ensure proper functionallity
 */</span>
<span style="color: #a61390;">@interface</span> CCCollisionBody <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
	CCNode <span style="color: #002200;">*</span> proxy; <span style="color: #11740a; font-style: italic;">// proxy object </span>
	uint type; <span style="color: #11740a; font-style: italic;">// defines the collision body type (see above)</span>
	CGSize size; <span style="color: #11740a; font-style: italic;">// if used by the type : defines the dimensions of the collision body.</span>
	<span style="color: #a61390;">float</span> radius; <span style="color: #11740a; font-style: italic;">// if used by the type : defines the radial dimensions of the collision body.</span>
	<span style="color: #a61390;">BOOL</span> disabled; <span style="color: #11740a; font-style: italic;">// if true the collions are deactivated otherwise collisions with this body will be checked.</span>
	<span style="color: #a61390;">int</span> collisionGroupID; <span style="color: #11740a; font-style: italic;">// collision group ID - only objects of the same group IDs will collide</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">int</span> collisionGroupID;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> bool disabled;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> CCNode <span style="color: #002200;">*</span> proxy;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> uint type;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> CGSize size;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">float</span> radius;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * creates a spherical collision body specified by a given radius and a proxy game object.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsSphereWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Radius<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> sphereRadius;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * creates a rectangle collision body specified by a given size and a proxy game object.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsRectWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Size<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span> rectSize;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * returns if a collision body collides with another collision body.
 * v1.0 only supports sphereical and rectangle collisionbodys.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> collidesWith<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CollisionBody.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;CCCollisionBody.h&quot;</span>
<span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark CCCollisionBody Implementation</span>
<span style="color: #a61390;">@implementation</span> CCCollisionBody
<span style="color: #a61390;">@synthesize</span> radius, size, type, proxy, disabled, collisionGroupID;
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Initialisierung</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark COLLISIONBODYTYPE_NONE : no collision</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject
<span style="color: #002200;">&#123;</span>
	self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		self.type <span style="color: #002200;">=</span> COLLISIONBODYTYPE_NONE;
		self.proxy <span style="color: #002200;">=</span> proxyObject;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark COLLISIONBODYTYPE_RECT : bounding box</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsRectWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Size<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span> rectSize
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self initWithProxy<span style="color: #002200;">:</span>proxyObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		self.size <span style="color: #002200;">=</span> rectSize;
		self.type <span style="color: #002200;">=</span> COLLISIONBODYTYPE_RECT;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark COLLISIONBODYTYPE_SPHERE : bounding sphere</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsSphereWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Radius<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> sphereRadius;
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self initWithProxy<span style="color: #002200;">:</span>proxyObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		self.radius <span style="color: #002200;">=</span> sphereRadius;
		self.type <span style="color: #002200;">=</span> COLLISIONBODYTYPE_SPHERE;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark cleanup</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
	self.proxy <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark collision tests</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark Rect-Rect collision </span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isRectRectIntersection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span>CGRectIsNull<span style="color: #002200;">&#40;</span> CGRectIntersection<span style="color: #002200;">&#40;</span>proxy.boundingBox, object.proxy.boundingBox<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;	
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark Sphere-Sphere collision </span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isSphereSphereIntersection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> minDistance <span style="color: #002200;">=</span> object.radius <span style="color: #002200;">+</span> self.radius;
	<span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> object.proxy.position.x <span style="color: #002200;">-</span> self.proxy.position.x;
	<span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> object.proxy.position.y <span style="color: #002200;">-</span> self.proxy.position.y;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span> <span style="color: #002200;">&#40;</span>dx &gt; minDistance || dy &gt; minDistance<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">float</span> actualDistance <span style="color: #002200;">=</span> <span style="color: #a61390;">sqrt</span><span style="color: #002200;">&#40;</span> dx <span style="color: #002200;">*</span> dx <span style="color: #002200;">+</span> dy <span style="color: #002200;">*</span> dy <span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>actualDistance &lt; <span style="color: #002200;">=</span> minDistance<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark main collision detection</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> collidesWith<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>self.disabled <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span>object.disabled <span style="color: #002200;">&amp;&amp;</span> self.collisionGroupID <span style="color: #002200;">==</span> object.collisionGroupID<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_RECT <span style="color: #002200;">&amp;&amp;</span> object.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_RECT<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isRectRectIntersection<span style="color: #002200;">:</span>object<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_SPHERE <span style="color: #002200;">&amp;&amp;</span> object.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_SPHERE<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isSphereSphereIntersection<span style="color: #002200;">:</span>object<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// NOT SUPPORTED COLLISION TYPE!</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/03/programming-iphone-games-with-cocos2d-part-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Programming iPhone Games with Cocos2D Part 3</title>
		<link>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/</link>
		<comments>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 10:55:06 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[sprite]]></category>
		<category><![CDATA[zwoptex]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=613</guid>
		<description><![CDATA[As promised in this post I want to give you a detailed view into animated sprites with cocos2d. Lets say we have 24 png pictures as frames for our animation ( see below ). Since Cocos 0.8.x or something the zwoptex format is supported. Basically the zwoptex format is based on two files. One is [...]]]></description>
			<content:encoded><![CDATA[<p>As promised in <a title="Cocos2D Part 1" href="http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d/" target="_blank">this post</a> I want to give you a detailed view into animated sprites with cocos2d.</p>
<p><span id="more-613"></span>Lets say we have 24 png pictures as frames for our animation ( see below ). Since Cocos 0.8.x or something the zwoptex format is supported. Basically the zwoptex format is based on two files. One is a so called AtlasSprite png file containing all frames and sprites somhow arranged in it. The other is a plist file which has all the location data and sprite dimensions of each frame and sprites as xml formated readable text in it. Here is an example how the xml tree like :</p>
<pre>root&lt;NSDictionary&gt;
+-frames&lt;NSDictionary&gt;
|    |
|    +-Frame1.png&lt;NSDictionary&gt;
|    |    |
|    |    +-x&lt;NSInteger&gt;
|    |    +-y&lt;NSInteger&gt;
|    |    +-width&lt;NSInteger&gt;
|    |    +-height&lt;NSInteger&gt;
|    |    +-offsetX&lt;real&gt;
|    |    +-offsetY&lt;real&gt;
:    :
.    .
:    :
|    +-Frame9.png&lt;NSDictionary&gt;
|         |
|         +-x&lt;NSInteger&gt;
|         +-y&lt;NSInteger&gt;
|         +-width&lt;NSInteger&gt;
|         +-height&lt;NSInteger&gt;
|         +-offsetX&lt;real&gt;
|         +-offsetY&lt;real&gt;
|
+-texture&lt;NSDictionary&gt;
|
+-width&lt;NSInteger&gt;
+-height&lt;NSInteger&gt;</pre>
<p>There is a great tool online to create these two files : http://zwoptex.zwopple.com . Once Zwoptex started make sure the canvas is big enough and also not too big to import all your frames. Zwoptex will trim your png files so transparent spaces will be cutted off. If you dont want that, you can select all images and choose &#8220;untrim&#8221;. Once all frames are loaded and trimmed or untrimmed you can choose any autoalignment function or align them manually. Some times you get better or worse results when choosing an alignment function twice. Try it!</p>

<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_01/' title='blob_idle_01'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_01-133x150.png" class="attachment-thumbnail" alt="blob_idle_01" title="blob_idle_01" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_02/' title='blob_idle_02'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_02-133x150.png" class="attachment-thumbnail" alt="blob_idle_02" title="blob_idle_02" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_03/' title='blob_idle_03'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_03-133x150.png" class="attachment-thumbnail" alt="blob_idle_03" title="blob_idle_03" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_04/' title='blob_idle_04'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_04-133x150.png" class="attachment-thumbnail" alt="blob_idle_04" title="blob_idle_04" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_05/' title='blob_idle_05'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_05-133x150.png" class="attachment-thumbnail" alt="blob_idle_05" title="blob_idle_05" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_06/' title='blob_idle_06'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_06-133x150.png" class="attachment-thumbnail" alt="blob_idle_06" title="blob_idle_06" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_07/' title='blob_idle_07'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_07-133x150.png" class="attachment-thumbnail" alt="blob_idle_07" title="blob_idle_07" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_08/' title='blob_idle_08'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_08-133x150.png" class="attachment-thumbnail" alt="blob_idle_08" title="blob_idle_08" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_09/' title='blob_idle_09'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_09-133x150.png" class="attachment-thumbnail" alt="blob_idle_09" title="blob_idle_09" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_10/' title='blob_idle_10'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_10-133x150.png" class="attachment-thumbnail" alt="blob_idle_10" title="blob_idle_10" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_11/' title='blob_idle_11'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_11-133x150.png" class="attachment-thumbnail" alt="blob_idle_11" title="blob_idle_11" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_12/' title='blob_idle_12'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_12-133x150.png" class="attachment-thumbnail" alt="blob_idle_12" title="blob_idle_12" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_13/' title='blob_idle_13'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_13-133x150.png" class="attachment-thumbnail" alt="blob_idle_13" title="blob_idle_13" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_14/' title='blob_idle_14'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_14-133x150.png" class="attachment-thumbnail" alt="blob_idle_14" title="blob_idle_14" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_15/' title='blob_idle_15'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_15-133x150.png" class="attachment-thumbnail" alt="blob_idle_15" title="blob_idle_15" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_16/' title='blob_idle_16'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_16-133x150.png" class="attachment-thumbnail" alt="blob_idle_16" title="blob_idle_16" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_17/' title='blob_idle_17'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_17-133x150.png" class="attachment-thumbnail" alt="blob_idle_17" title="blob_idle_17" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_18/' title='blob_idle_18'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_18-133x150.png" class="attachment-thumbnail" alt="blob_idle_18" title="blob_idle_18" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_19/' title='blob_idle_19'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_19-133x150.png" class="attachment-thumbnail" alt="blob_idle_19" title="blob_idle_19" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_20/' title='blob_idle_20'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_20-133x150.png" class="attachment-thumbnail" alt="blob_idle_20" title="blob_idle_20" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_21/' title='blob_idle_21'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_21-133x150.png" class="attachment-thumbnail" alt="blob_idle_21" title="blob_idle_21" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_22/' title='blob_idle_22'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_22-133x150.png" class="attachment-thumbnail" alt="blob_idle_22" title="blob_idle_22" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_23/' title='blob_idle_23'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_23-133x150.png" class="attachment-thumbnail" alt="blob_idle_23" title="blob_idle_23" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/blob_idle_24/' title='blob_idle_24'><img width="133" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/blob_idle_24-133x150.png" class="attachment-thumbnail" alt="blob_idle_24" title="blob_idle_24" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/zwoptex_step1/' title='zwoptex_step1'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/zwoptex_step1-150x150.png" class="attachment-thumbnail" alt="zwoptex_step1" title="zwoptex_step1" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/zwoptex_step2/' title='zwoptex_step2'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/zwoptex_step2-150x150.png" class="attachment-thumbnail" alt="zwoptex_step2" title="zwoptex_step2" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/zwoptex_step3/' title='zwoptex_step3'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/zwoptex_step3-150x150.png" class="attachment-thumbnail" alt="zwoptex_step3" title="zwoptex_step3" /></a>
<a href='http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/zwoptex_step4/' title='zwoptex_step4'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2010/02/zwoptex_step4-150x150.png" class="attachment-thumbnail" alt="zwoptex_step4" title="zwoptex_step4" /></a>

<p>Ok, now we have two files <strong>blob.png</strong> containing 24 frames of our blob animation and a <strong>blob.plist</strong> containing the location and dimension information of all frames.</p>
<p>In cocos2d they changed pretty much since v0.8 especially how to create an animation. I will not explain how to create them in earlier version, but if some one is really interested in that, feel free to ask me here.</p>
<p>In Cocos2d all animation frames are stored in a so called animation cache ( CCSpriteFrameCache ). Once you loaded your plist file into that cache you can access the location and dimension information in it by using the former name of the png file. In this example this would be &#8220;blob_idle_01.png&#8221; for frame 1. If you want to change that name you have just to open the plist file in xcode and change it in what you want. But be sure you dont use any frame name twice.</p>
<p>Once you loaded the plist into the cache you can create an animation (CCAnimation) object and add all the frames you want into it. CCAnimation does support creating your frames based on files, but using a frame cache is much faster. After you have your animation object the best way you create the sprite is to use the frame cache creating your sprite, like &#8220;gimmi the first frame as sprite&#8221;. Finally you have to run the animation action. Here is a complete code of how to create your animated sprite :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CCSpriteFrameCache <span style="color: #002200;">*</span> cache <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSpriteFrameCache sharedSpriteFrameCache<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>cache addSpriteFramesWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blob.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
CCAnimation <span style="color: #002200;">*</span> animation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCAnimation alloc<span style="color: #002200;">&#93;</span> initWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;idle&quot;</span> delay<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">/</span><span style="color: #2400d9;">24.0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">1</span>; i &lt; <span style="color: #2400d9;">25</span>; <span style="color: #002200;">++</span>i <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// frame name format: blob_idle_01.png .. blob_idle_24.png</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> fname <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blob_idle_%02i.png&quot;</span>, i<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>animation addFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cache spriteFrameByName<span style="color: #002200;">:</span> fname<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
CCSprite <span style="color: #002200;">*</span> blob <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSprite spriteWithSpriteFrameName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blob_idle_01.png&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// deprecated since 0.9: CCSprite * blob = [cache createSpriteWithFrameName:@&quot;blob_idle_01.png&quot;];</span>
<span style="color: #a61390;">id</span> action <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCRepeatForever actionWithAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCAnimate actionWithAnimation<span style="color: #002200;">:</span>animation<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>blob runAction<span style="color: #002200;">:</span>action<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// position our blob</span>
blob.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// add blob to scene ( self should be a visible CCLayer or CCScene )</span>
<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>blob<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Finally : <img src="http://www.anima-entertainment.de/blob.gif" alt="Animated CCSprite" /></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-3/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Programming iPhone Games with Cocos2D Part 2</title>
		<link>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-2/</link>
		<comments>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-2/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 15:54:34 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>
		<category><![CDATA[Cocos]]></category>
		<category><![CDATA[Item]]></category>
		<category><![CDATA[Menu]]></category>
		<category><![CDATA[Radio]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=597</guid>
		<description><![CDATA[The huge Library of cocos2D gives you some really good start for nearly everything you want to program. But sometimes it still might not fit your needs as you wanted. In this post I will show you how to enhance the menu class of cocos2d to function as radio menu. A Menu in cocos2d grabs [...]]]></description>
			<content:encoded><![CDATA[<p>The huge Library of cocos2D gives you some really good start for nearly everything you want to program. But sometimes it still might not fit your needs as you wanted. In this post I will show you how to enhance the menu class of cocos2d to function as radio menu.<br />
<span id="more-597"></span><br />
A Menu in cocos2d grabs the touch events and delegates them to the menu items. You can hover over the menu items and when releasing the touch the current menu item under your finger will be executed. In cocos2D it is the same if you want to create a menu or a single button. If you just need one button you have to create a menu with one button. Keep in mind that you cannot slide from one menu into another, if the user does not release the touch the touch will be still captured by the first menu. But you can compose diffrent button types to one menu and also place them where you wanted. The menu class does have some align functions but you dont need to use them.</p>
<p>Ok, lets start with that what cocos2D have for you. First you have to create some menu items. In cocos2d they are called CCMenuItem (sounds reasonable). There a plenty of them but I will introduce here just one : CCMenuItemImage is an image button with diffrent images for each state: normal, selected and disabled. If you dont need disabled you dont have to set it up. Menu items support targets, so if you press a button a function will be called. Here is an example of how to create a CCMenuItemImage:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;MyScene.h&quot;</span>
<span style="color: #a61390;">@implementation</span> MyScene
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onButton1Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// TODO: something phenomenal </span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onButton2Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// TODO: something even more phenomenal </span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setupMenu
<span style="color: #002200;">&#123;</span>
	CCMenuItemImage <span style="color: #002200;">*</span> button1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemImage itemFromNormalImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b1_up.png&quot;</span> selectedImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b1_down.png&quot;</span> target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onButton1Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	CCMenuItemImage <span style="color: #002200;">*</span> button2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemImage itemFromNormalImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b2_up.png&quot;</span> selectedImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b2_down.png&quot;</span> disabledImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;b2_disabled.png&quot;</span> target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onButton2Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>To actually create the menu you have to put all your buttons into a CCMenu class. One really strange thing is you have to create the CCMenu with items, you are able to add more items (childs) later but you have to create it with at least one item. So here is the complete code including alignment of all buttons:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;MyScene.h&quot;</span>
<span style="color: #a61390;">@implementation</span> MyScene
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onButton1Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// TODO: something phenomenal </span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onButton2Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// TODO: something even more phenomenal </span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setupMenu
<span style="color: #002200;">&#123;</span>
	CCMenuItemImage <span style="color: #002200;">*</span> button1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemImage itemFromNormalImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b1_up.png&quot;</span> selectedImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b1_down.png&quot;</span> target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onButton1Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	CCMenuItemImage <span style="color: #002200;">*</span> button2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemImage itemFromNormalImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b2_up.png&quot;</span> selectedImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b2_down.png&quot;</span> disabledImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;b2_disabled.png&quot;</span> target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onButton2Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	CCMenu <span style="color: #002200;">*</span> menu <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenue menueWithItems<span style="color: #002200;">:</span>button1 , button2, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// you have to terminate the list with nil</span>
&nbsp;
	menu.position <span style="color: #002200;">=</span> ccp <span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span> <span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>menu alignItemsHorizontallyWithPadding<span style="color: #002200;">:</span>10.0f<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span> menu<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now when you just want to have a radio button menu instead, so that one item always is selected and if you select another you want to unselect the previous one. When hovering over the items you want to show both &#8211; the current selected one and the one you are targeting. Also if you cancel your hovering (like if the touch gets outside the screen) you want to unselect the one you are targetting and fallback to the one that was previously selected. All in all you need to enhance the CCMenu class. Lets start with the easy part, the header file of our new class called CCRadioMenu :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CCRadioMenu.h</span>
<span style="color: #11740a; font-style: italic;">//  Cocos2d Version used: 0.9.0 alpha</span>
<span style="color: #11740a; font-style: italic;">//  Created by Hans Hamm on 24.11.09.  </span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> CCRadioMenu <span style="color: #002200;">:</span> CCMenu <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> selectedItemIndex; <span style="color: #11740a; font-style: italic;">// will be the current targeted item </span>
	<span style="color: #a61390;">int</span> fallBackItemIndex; <span style="color: #11740a; font-style: italic;">// will be our fall back item (previously selected one)</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #a61390;">int</span> selectedItemIndex;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>CCMenuItem <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> itemForTouch<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> touch; <span style="color: #11740a; font-style: italic;">// will retrieve the current item for the touch</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Ok, there are several ways to implement such a menu, and probably this is not the best one. If someone has an improved version, feel free to post it.  My working code sounds like :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CCRadioMenu.m</span>
<span style="color: #11740a; font-style: italic;">// </span>
<span style="color: #11740a; font-style: italic;">//  Cocos2d Version used: 0.9.0 alpha</span>
<span style="color: #11740a; font-style: italic;">//  Created by Hans Hamm on 24.11.09.  </span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;CCRadioMenu.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> CCRadioMenu
&nbsp;
<span style="color: #a61390;">@synthesize</span> selectedItemIndex;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * will be called if a touch started.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> ccTouchBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> state <span style="color: #002200;">!=</span> kMenuStateWaiting <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>; <span style="color: #11740a; font-style: italic;">// do not track events if menu is busy</span>
&nbsp;
	selectedItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self itemForTouch<span style="color: #002200;">:</span>touch<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// any of our items was selected?</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> selectedItem <span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// if one of our items was selected</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// make it be selected</span>
		state <span style="color: #002200;">=</span> kMenuStateTrackingTouch; <span style="color: #11740a; font-style: italic;">// mark menu as busy</span>
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>; <span style="color: #11740a; font-style: italic;">// say : &quot;yes, we want more events from this touch&quot;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>; <span style="color: #11740a; font-style: italic;">// none of our items was selected, so we dont want to hear from this touch event anymore</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * will be called if a touch event ended.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ccTouchEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	NSAssert<span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> kMenuStateTrackingTouch, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[Menu ccTouchEnded] -- invalid state&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children <span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// unselect all items</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>item unselected<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>selectedItem<span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// if we have selected an item:</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// make it selected</span>
		<span style="color: #002200;">&#91;</span>selectedItem activate<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// an execute it</span>
		fallBackItemIndex <span style="color: #002200;">=</span> selectedItemIndex; <span style="color: #11740a; font-style: italic;">// the new fall back item is the current target</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// there is none selected</span>
	<span style="color: #002200;">&#123;</span>
		self.selectedItemIndex <span style="color: #002200;">=</span> fallBackItemIndex; <span style="color: #11740a; font-style: italic;">// so just fall back to the old one</span>
	<span style="color: #002200;">&#125;</span>	
&nbsp;
	state <span style="color: #002200;">=</span> kMenuStateWaiting; <span style="color: #11740a; font-style: italic;">// we are not busy anymore</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * will be called if a touch event was canceled. Normaly the user dont want
 * to execute the last selected one, so just fall back to the old one here.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ccTouchCancelled<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	NSAssert<span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> kMenuStateTrackingTouch, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[Menu ccTouchCancelled] -- invalid state&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children <span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// unselect all items</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>item unselected<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#125;</span>
	<span style="color: #11740a; font-style: italic;">// on cancel just fall back to the old one :</span>
	self.selectedItemIndex <span style="color: #002200;">=</span> fallBackItemIndex;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// we are not busy anymore</span>
	state <span style="color: #002200;">=</span> kMenuStateWaiting;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * will be called if a touch event moved.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ccTouchMoved<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	NSAssert<span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> kMenuStateTrackingTouch, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[Menu ccTouchMoved] -- invalid state&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// still one of our items selected?</span>
	CCMenuItem <span style="color: #002200;">*</span> currentItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self itemForTouch<span style="color: #002200;">:</span>touch<span style="color: #002200;">&#93;</span>; 
&nbsp;
	<span style="color: #11740a; font-style: italic;">// if not, what was the fallback item again?</span>
	CCMenuItem <span style="color: #002200;">*</span> fallBackItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCMenuItem <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children objectAtIndex<span style="color: #002200;">:</span>fallBackItemIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// if the new selected item changed and is not our fallback item</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>currentItem <span style="color: #002200;">!=</span> selectedItem <span style="color: #002200;">&amp;&amp;</span> currentItem <span style="color: #002200;">!=</span> fallBackItem<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>				
		<span style="color: #002200;">&#91;</span>selectedItem unselected<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// unselect the previous one</span>
		selectedItem <span style="color: #002200;">=</span> currentItem; <span style="color: #11740a; font-style: italic;">// this is our new targeted one ( could be nil ! ) </span>
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>selectedItem<span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// if there is a new one ( touch could be outside the menu )</span>
		<span style="color: #002200;">&#123;</span>		
			<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// select this new one</span>
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// there is no new one, so just fall back to the old one</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>children objectAtIndex<span style="color: #002200;">:</span>fallBackItemIndex<span style="color: #002200;">&#93;</span>selected<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * Returns the first menu item hit by a touch event.
 * Also we could update the selectedItemIndex here.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>CCMenuItem <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> itemForTouch<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> touch
<span style="color: #002200;">&#123;</span>
	CGPoint touchLocation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>touch view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// touch location</span>
	touchLocation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span> convertToGL<span style="color: #002200;">:</span> touchLocation<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// in gl coordinates</span>
	<span style="color: #a61390;">int</span> idx <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>; <span style="color: #11740a; font-style: italic;">// helper variable to determine the index of the item ( 0 = first item )</span>
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children <span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		idx<span style="color: #002200;">++</span>; <span style="color: #11740a; font-style: italic;">// in first step this would be zero ( 0 )</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// convert the touch to the local coordinates of the current item</span>
		<span style="color: #11740a; font-style: italic;">// and check if the location is inside the rect of it</span>
&nbsp;
		CGPoint local <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item convertToNodeSpace<span style="color: #002200;">:</span>touchLocation<span style="color: #002200;">&#93;</span>; 		
		CGRect r <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item rect<span style="color: #002200;">&#93;</span>;  
		r.origin <span style="color: #002200;">=</span> CGPointZero;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> CGRectContainsPoint<span style="color: #002200;">&#40;</span> r, local <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// if touch is inside it</span>
		<span style="color: #002200;">&#123;</span>
			selectedItemIndex <span style="color: #002200;">=</span> idx;  <span style="color: #11740a; font-style: italic;">// save the itemIndex and..</span>
			<span style="color: #a61390;">return</span> item;	 <span style="color: #11740a; font-style: italic;">// .. return the item</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// we found not item hit by that touch:</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * returns the current selected Item Index.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> selectedItemIndex
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> selectedItemIndex;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 *  sets the current Selected Item Index ( but does not activate it )
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectedItemIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> value 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children <span style="color: #002200;">&#41;</span>  <span style="color: #11740a; font-style: italic;">// unselect all</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>item unselected<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#125;</span>
&nbsp;
	selectedItemIndex <span style="color: #002200;">=</span> value;	<span style="color: #11740a; font-style: italic;">// store new index</span>
	selectedItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>children objectAtIndex<span style="color: #002200;">:</span>selectedItemIndex<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// get the item for that index	</span>
	fallBackItemIndex <span style="color: #002200;">=</span> selectedItemIndex; <span style="color: #11740a; font-style: italic;">// store as fallBackIndex, too.	</span>
	<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// mark as selected.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>So how to change your first &#8220;CCMenu-Code&#8221; to fit our CCRadioMenu thing ? All you have to do is to import your class, change CCMenu to CCRadioMenu and thats it. May be just one thing more. If you want a specific item be initially selected, you have to set <i>selectedItemIndex</i> once the menu is created:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">menu.selectedItemIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; <span style="color: #11740a; font-style: italic;">// first Item should be initially selected</span></pre></div></div>

<p>so the code should now look like this :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;MyScene.h&quot;</span>
<span style="color: #6e371a;">#import &quot;CCRadioMenu.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyScene
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onButton1Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// TODO: something phenomenal </span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onButton2Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// TODO: something even more phenomenal </span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setupMenu
<span style="color: #002200;">&#123;</span>
	CCMenuItemImage <span style="color: #002200;">*</span> button1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemImage itemFromNormalImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b1_up.png&quot;</span> selectedImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b1_down.png&quot;</span> target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onButton1Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	CCMenuItemImage <span style="color: #002200;">*</span> button2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemImage itemFromNormalImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b2_up.png&quot;</span> selectedImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b2_down.png&quot;</span> disabledImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;b2_disabled.png&quot;</span> target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>onButton2Pressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	CCRadioMenu <span style="color: #002200;">*</span> menu <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCRadioMenu menueWithItems<span style="color: #002200;">:</span>button1 , button2, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// you have to terminate the list with nil</span>
&nbsp;
	menu.selectedItemIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; <span style="color: #11740a; font-style: italic;">// first Item should be initially selected</span>
&nbsp;
	menu.position <span style="color: #002200;">=</span> ccp <span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span> <span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>menu alignItemsHorizontallyWithPadding<span style="color: #002200;">:</span>10.0f<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span> menu<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h1>Update to 0.99</h1>
<p>Since they changed some ivar names in 0.99 you have to use &#8220;children_&#8221; instead &#8220;children&#8221;. So if you are using 0.99 or later you have to use my updated code for CCRadioMenu class. </p>
<p>Updated code of CCRadioMenu class for cocos2d 0.99 stable and later :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CCRadioMenu.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> CCRadioMenu
&nbsp;
<span style="color: #a61390;">@synthesize</span> selectedItemIndex;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> ccTouchBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> state <span style="color: #002200;">!=</span> kMenuStateWaiting <span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
&nbsp;
	selectedItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self itemForTouch<span style="color: #002200;">:</span>touch<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> selectedItem <span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>;
		state <span style="color: #002200;">=</span> kMenuStateTrackingTouch;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ccTouchEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	NSAssert<span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> kMenuStateTrackingTouch, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[Menu ccTouchEnded] -- invalid state&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children_ <span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>item unselected<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>selectedItem<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>selectedItem activate<span style="color: #002200;">&#93;</span>;
		fallBackItemIndex <span style="color: #002200;">=</span> selectedItemIndex;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		self.selectedItemIndex <span style="color: #002200;">=</span> fallBackItemIndex;
	<span style="color: #002200;">&#125;</span>	
&nbsp;
	state <span style="color: #002200;">=</span> kMenuStateWaiting;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ccTouchCancelled<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	NSAssert<span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> kMenuStateTrackingTouch, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[Menu ccTouchCancelled] -- invalid state&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children_ <span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>item unselected<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#125;</span>
&nbsp;
	self.selectedItemIndex <span style="color: #002200;">=</span> fallBackItemIndex;
&nbsp;
	state <span style="color: #002200;">=</span> kMenuStateWaiting;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ccTouchMoved<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touch withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	NSAssert<span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> kMenuStateTrackingTouch, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[Menu ccTouchMoved] -- invalid state&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	CCMenuItem <span style="color: #002200;">*</span> currentItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self itemForTouch<span style="color: #002200;">:</span>touch<span style="color: #002200;">&#93;</span>;
	CCMenuItem <span style="color: #002200;">*</span> fallBackItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCMenuItem <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>fallBackItemIndex<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>currentItem <span style="color: #002200;">!=</span> selectedItem <span style="color: #002200;">&amp;&amp;</span> currentItem <span style="color: #002200;">!=</span> fallBackItem<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>				
		<span style="color: #002200;">&#91;</span>selectedItem unselected<span style="color: #002200;">&#93;</span>;
		selectedItem <span style="color: #002200;">=</span> currentItem;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>selectedItem<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>		
			<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>fallBackItemIndex<span style="color: #002200;">&#93;</span>selected<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>CCMenuItem <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> itemForTouch<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>UITouch <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> touch
<span style="color: #002200;">&#123;</span>
	CGPoint touchLocation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>touch view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	touchLocation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span> convertToGL<span style="color: #002200;">:</span> touchLocation<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">int</span> idx <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children_ <span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		idx<span style="color: #002200;">++</span>;
		CGPoint local <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item convertToNodeSpace<span style="color: #002200;">:</span>touchLocation<span style="color: #002200;">&#93;</span>;
&nbsp;
		CGRect r <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item rect<span style="color: #002200;">&#93;</span>;
		r.origin <span style="color: #002200;">=</span> CGPointZero;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> CGRectContainsPoint<span style="color: #002200;">&#40;</span> r, local <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			selectedItemIndex <span style="color: #002200;">=</span> idx;
			<span style="color: #a61390;">return</span> item;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> selectedItemIndex
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> selectedItemIndex;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectedItemIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> value 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> CCMenuItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> children_ <span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>item unselected<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#125;</span>
&nbsp;
	selectedItemIndex <span style="color: #002200;">=</span> value;	
	selectedItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>selectedItemIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
	fallBackItemIndex <span style="color: #002200;">=</span> selectedItemIndex;
&nbsp;
	<span style="color: #002200;">&#91;</span>selectedItem selected<span style="color: #002200;">&#93;</span>;      
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming iPhone Games with Cocos2D Part 1</title>
		<link>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d/</link>
		<comments>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 18:31:05 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=580</guid>
		<description><![CDATA[Hi, I want to talk about cocos2d since I really think this is an amazing graphics and sound engine especially for small games like run&#038;jump games or shooters. Cocos2D is meant to display 2D content only but for most games this is enough. Use some parallax layers to visually update your 2D to 2.5D, or [...]]]></description>
			<content:encoded><![CDATA[<p>Hi,<br />
I want to talk about cocos2d since I really think this is an amazing graphics and sound engine especially for small games like run&#038;jump games or shooters.<br />
<span id="more-580"></span><br />
Cocos2D is meant to display 2D content only but for most games this is enough. Use some parallax layers to visually update your 2D to 2.5D, or if you want to create real 3D games use unity3D.</p>
<p>If you want to start with cocos2d just download the current version <a href="http://www.cocos2d-iphone.org/">here</a> and seek for the install bash script to install the templates. Once you&#8217;ve done that you could start by creating a new Project in XCode and choose &#8220;cocos2d 0.99.0 Application&#8221;. In this case I give it the name &#8220;MyCocosGame&#8221;. After the project has been created you will find the start of everything in &#8220;MyCocosGameAppDelegate.m&#8221;.</p>
<p>Since version 0.9 alpha all the cocos2d stuff starts with CC prefix. Here some quick cocos2d classes you should know about:</p>
<h1>CCDirector</h1>
<p>This class directs the render process, handles scheduled timers and display settings. You could say this is the heart of cocos2d. To start up cocos2d you need to set a pixelformat, attach it to a UIWindow class and startup a CCScene which will lead us to the next important class.</p>
<h1>CCScene</h1>
<p>A Scene basically is a layer with all the content you want to render in it. You could use transition effects to blend from one scene to another (like crossblend, roll-in, etc.). CCDirector uses a stack of scenes so you can push and pop scenes to easily define a menue structure. But I recommend not to use this feature with many scenes since this could use lots of memory. Instead of &#8220;pushScene&#8221; and &#8220;popScene&#8221; use &#8220;replaceScene&#8221; which will release the current scene after the transition is done.</p>
<h1>CCNode</h1>
<p>CCNode is the root node of everything you can transform. So CCScene is basically a CCNode ( well if you look into the code you well see that actually CCScene is a CCLayer, but then CCLayer is a CCNode ). You could do interesting stuff with CCNodes like scaling, positioning and you can add child nodes to nodes. Nodes also have &#8220;anchorPoints&#8221; &#8211; others may call them &#8220;pivots&#8221;. Anchors could be inside the content ( between 0 and 1) or outside (like -1 and +2).<br />
The most interesting feature is to transform points to local coordinates and vice versa. </p>
<h1>CCSprite</h1>
<p>Sprites are a known feature and you will find every thing you expect from it. Simpliest way to create one is to call within a Scene :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CCSprite <span style="color: #002200;">*</span> mySprite <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;myFile.png&quot;</span><span style="color: #002200;">&#93;</span>;
mySprite.position <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span><span style="color: #002200;">&#41;</span>;
mySprite.rotation <span style="color: #002200;">=</span> <span style="color: #2400d9;">45</span>; <span style="color: #11740a; font-style: italic;">// Degrees</span>
<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>mySprite<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>You could create animated sprites and even more impressive are actions you could apply to them (actually to all CCNodes). Since this is a first introduction I will explain how to create animated sprites later.</p>
<h1>CCAction</h1>
<p>In my humble opinion actions are the most impressive thing in cocos2d. You can create complex actions and assign them to any node. There are half a dozen actions so I will introduce just a few.</p>
<table>
<tr>
<td>CCCallFunc<br />CCCallFuncN<br />CCCallFuncND</td>
<td>will call a function<br />will call a function with current Node as Attribute<br />will call a function with current Node and Data as attributes</td>
</tr>
<tr>
<td>CCDelayTime</td>
<td>waits a given time before next action will start</td>
</tr>
<tr>
<td>CCSequence</td>
<td>will call several actions one after another</td>
</tr>
<tr>
<td>CCSpawn</td>
<td>will call several actions at once</td>
</tr>
<tr>
<td>CCRepeat</td>
<td>will repeat an action for a duration or x times</td>
</tr>
<tr>
<td>CCRepeatForever</td>
<td>will repeat an action endless</td>
</tr>
<tr>
<td>CCFadeIn<br />CCFadeTo</td>
<td>will fade in an RGBProtocol supported node<br />will fade to a given value</td>
</tr>
<tr>
<td>CCMoveTo<br />CCMoveBy</td>
<td>will move a node over time to a given destination<br />will move a node by a given offset over a time</td>
</tr>
</table>
<p>Here is an example how to use them :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CCSprite <span style="color: #002200;">*</span> tveffect <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;televisionStreak.png&quot;</span><span style="color: #002200;">&#93;</span>; 
ccBlendFunc additiveBlendFunction <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>GL_SRC_ALPHA, GL_ONE<span style="color: #002200;">&#125;</span>;
tveffect.blendFunc <span style="color: #002200;">=</span> additiveBlendFunction;
&nbsp;
tveffect.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;	
<span style="color: #002200;">&#91;</span>tveffect setOpacity<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">id</span> fadeAction <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSequence actions<span style="color: #002200;">:</span>
				 <span style="color: #002200;">&#91;</span>CCFadeTo actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span> opacity<span style="color: #002200;">:</span><span style="color: #2400d9;">255</span><span style="color: #002200;">&#93;</span>,
				 <span style="color: #002200;">&#91;</span>CCDelayTime actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#93;</span>,
				 <span style="color: #002200;">&#91;</span>CCFadeTo actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span> opacity<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>,
				 <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">id</span> moveAction <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSequence actions<span style="color: #002200;">:</span>
				 <span style="color: #002200;">&#91;</span>CCMoveTo actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> position<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>,
				 <span style="color: #002200;">&#91;</span>CCMoveTo actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span> position<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">480</span><span style="color: #002200;">&#93;</span>,
				 <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>tveffect runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCRepeatForever actionWithAction<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>CCSpawn actions<span style="color: #002200;">:</span>fadeAction,moveAction, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>For the first introduction this should be it. In the next chapter I will talk about how to handle touch events and further more how to create your first menu.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/02/programming-iphone-games-with-cocos2d/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Change Company Name in XCode</title>
		<link>http://www.gehacktes.net/2009/11/xcodecompanyname/</link>
		<comments>http://www.gehacktes.net/2009/11/xcodecompanyname/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 13:00:36 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=560</guid>
		<description><![CDATA[Change __MyCompanyName__ in XCode]]></description>
			<content:encoded><![CDATA[<p>If you create a new file in XCode, it will autogenerate a header like :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyFileName.m</span>
<span style="color: #11740a; font-style: italic;">//  MyProjectName</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Hans Hamm on 24.11.09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 __MyCompanyName__. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span></pre></div></div>

<h1>XCode below Version 3.2</h1>
<p>To change <code>__MyCompanyName__</code> to your company, just enter the following command into your mac terminal:</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ &quot;ORGANIZATIONNAME&quot; = &quot;My Company&quot;;}'</pre></div></div>

<h1>XCode Version 3.2 and higher</h1>
<p>To change the company Name just open your project settings (e.g. by selecting your Project and hit the info button). In your project settings go to the &#8220;general&#8221; settings and enter your company into the field &#8220;Organization Name&#8221;<br />
<a href="http://www.gehacktes.net/wp-content/uploads/2009/11/projectname.png"><img src="http://www.gehacktes.net/wp-content/uploads/2009/11/projectname.png" alt="General Project Info" title="Genral Project Info" width="761" height="666" class="alignnone size-full wp-image-705" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/11/xcodecompanyname/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Setup XCode SDK 3.0 to work for OS 3.0.1</title>
		<link>http://www.gehacktes.net/2009/09/setup-xcode-sdk-3-0-to-work-for-os-3-0-1/</link>
		<comments>http://www.gehacktes.net/2009/09/setup-xcode-sdk-3-0-to-work-for-os-3-0-1/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:36:49 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=545</guid>
		<description><![CDATA[XCode SDK 3.0 does not yet support OS 3.0.1, so if you plug in an updated iPhone you will encounter an error. There is an Advisory at Apples developer portal which solves this problem. Since it seams there are no relevant changes from 3.0 to 3.0.1 the solution is to tell XCode to use OS [...]]]></description>
			<content:encoded><![CDATA[<p>XCode SDK 3.0 does not yet support OS 3.0.1, so if you plug in an updated iPhone you will encounter an error. There is an Advisory at Apples developer portal which solves this problem. Since it seams there are no relevant changes from 3.0 to 3.0.1 the solution is to tell XCode to use OS 3.0 Support for 3.0.1. OS Devices by linking a directory 3.0.1 to 3.0 within your DeviceSupport directory on your mac.<br />
To do so open your Mac Terminal and type in the following two simple commands :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">cd <span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>Utilities
ln <span style="color: #339933;">-</span>s <span style="color: #339933;">/</span>Developer<span style="color: #339933;">/</span>Platforms<span style="color: #339933;">/</span>iPhoneOS.<span style="color: #202020;">platform</span><span style="color: #339933;">/</span>DeviceSupport<span style="color: #339933;">/</span><span style="color:#800080;">3.0</span>\ \<span style="color: #009900;">&#40;</span>7A341\<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span>Developer<span style="color: #339933;">/</span>Platforms<span style="color: #339933;">/</span>iPhoneOS.<span style="color: #202020;">platform</span><span style="color: #339933;">/</span>DeviceSupport<span style="color: #339933;">/</span>3.0.1</pre></div></div>

<p>XCode should now work fine with OS 3.01</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/09/setup-xcode-sdk-3-0-to-work-for-os-3-0-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Part 6: Multiple Sounds with OpenAL</title>
		<link>http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/</link>
		<comments>http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 10:36:54 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=360</guid>
		<description><![CDATA[So today we want to do the hard stuff I talked about in the last post. We will do that in three steps. Writing an OpenAL sound file loader. Writing an OpenAL wrapper class wich is more comfortable than using the OpenAL functions directly. Writing the actual code to play some audio files. 1. Writing [...]]]></description>
			<content:encoded><![CDATA[<p>So today we want to do the hard stuff I talked about in the last post.<br />
We will do that in three steps.</p>
<ol>
<li>Writing an OpenAL sound file loader.</li>
<li>Writing an OpenAL wrapper class wich is more comfortable than using the OpenAL functions directly.</li>
<li>Writing the actual code to play some audio files.</li>
</ol>
<h5>1. Writing an OpenAL sound file loader</h5>
<p>Actually there is a sample code within the apples developers support files. While Apples iPhone SDK is not under NDA anymore I am able to post the code here:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
&nbsp;
    File: MyOpenALSupport.h
Abstract: OpenAL-related support functions
 Version: 1.3
&nbsp;
Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Inc. (&quot;Apple&quot;) in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms.  If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
&nbsp;
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
&quot;Apple Software&quot;), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple.  Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
&nbsp;
The Apple Software is provided by Apple on an &quot;AS IS&quot; basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
&nbsp;
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
&nbsp;
Copyright (C) 2008 Apple Inc. All Rights Reserved.
&nbsp;
 */</span>
&nbsp;
<span style="color: #339933;">#import &lt;OpenAL/al.h&gt;</span>
<span style="color: #339933;">#import &lt;OpenAL/alc.h&gt;</span>
<span style="color: #339933;">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span>
<span style="color: #339933;">#import &lt;AudioToolbox/ExtendedAudioFile.h&gt;</span>
&nbsp;
<span style="color: #993333;">typedef</span> ALvoid	AL_APIENTRY	<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>alBufferDataStaticProcPtr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALint bid<span style="color: #339933;">,</span> ALenum format<span style="color: #339933;">,</span> ALvoid<span style="color: #339933;">*</span> data<span style="color: #339933;">,</span> ALsizei size<span style="color: #339933;">,</span> ALsizei freq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ALvoid  alBufferDataStaticProc<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALint bid<span style="color: #339933;">,</span> ALenum format<span style="color: #339933;">,</span> ALvoid<span style="color: #339933;">*</span> data<span style="color: #339933;">,</span> ALsizei size<span style="color: #339933;">,</span> ALsizei freq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span><span style="color: #339933;">*</span> MyGetOpenALAudioData<span style="color: #009900;">&#40;</span>CFURLRef inFileURL<span style="color: #339933;">,</span> ALsizei <span style="color: #339933;">*</span>outDataSize<span style="color: #339933;">,</span> ALenum <span style="color: #339933;">*</span>outDataFormat<span style="color: #339933;">,</span> ALsizei<span style="color: #339933;">*</span>	outSampleRate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
&nbsp;
    File: MyOpenALSupport.c
Abstract: OpenAL-related support functions
 Version: 1.3
&nbsp;
Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Inc. (&quot;Apple&quot;) in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms.  If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
&nbsp;
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
&quot;Apple Software&quot;), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple.  Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
&nbsp;
The Apple Software is provided by Apple on an &quot;AS IS&quot; basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
&nbsp;
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
&nbsp;
Copyright (C) 2008 Apple Inc. All Rights Reserved.
&nbsp;
*/</span>
&nbsp;
<span style="color: #339933;">#include &quot;MyOpenALSupport.h&quot;</span>
&nbsp;
ALvoid  alBufferDataStaticProc<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALint bid<span style="color: #339933;">,</span> ALenum format<span style="color: #339933;">,</span> ALvoid<span style="color: #339933;">*</span> data<span style="color: #339933;">,</span> ALsizei size<span style="color: #339933;">,</span> ALsizei freq<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">static</span>	alBufferDataStaticProcPtr	proc <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        proc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>alBufferDataStaticProcPtr<span style="color: #009900;">&#41;</span> alcGetProcAddress<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALCchar<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">&quot;alBufferDataStatic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc<span style="color: #009900;">&#41;</span>
        proc<span style="color: #009900;">&#40;</span>bid<span style="color: #339933;">,</span> format<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> freq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span><span style="color: #339933;">*</span> MyGetOpenALAudioData<span style="color: #009900;">&#40;</span>CFURLRef inFileURL<span style="color: #339933;">,</span> ALsizei <span style="color: #339933;">*</span>outDataSize<span style="color: #339933;">,</span> ALenum <span style="color: #339933;">*</span>outDataFormat<span style="color: #339933;">,</span> ALsizei<span style="color: #339933;">*</span>	outSampleRate<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	OSStatus err <span style="color: #339933;">=</span> noErr<span style="color: #339933;">;</span>
	SInt64 theFileLengthInFrames <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	AudioStreamBasicDescription theFileFormat<span style="color: #339933;">;</span>
	UInt32 thePropertySize <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>theFileFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ExtAudioFileRef extRef <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">void</span><span style="color: #339933;">*</span> theData <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	AudioStreamBasicDescription theOutputFormat<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Open a file with ExtAudioFileOpen()</span>
	err <span style="color: #339933;">=</span> ExtAudioFileOpenURL<span style="color: #009900;">&#40;</span>inFileURL<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>extRef<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the audio data format</span>
	err <span style="color: #339933;">=</span> ExtAudioFileGetProperty<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> kExtAudioFileProperty_FileDataFormat<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>thePropertySize<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theFileFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>theFileFormat.<span style="color: #202020;">mChannelsPerFrame</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the client format to 16 bit signed integer (native-endian) data</span>
	<span style="color: #666666; font-style: italic;">// Maintain the channel count and sample rate of the original source format</span>
	theOutputFormat.<span style="color: #202020;">mSampleRate</span> <span style="color: #339933;">=</span> theFileFormat.<span style="color: #202020;">mSampleRate</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span> <span style="color: #339933;">=</span> theFileFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
&nbsp;
	theOutputFormat.<span style="color: #202020;">mFormatID</span> <span style="color: #339933;">=</span> kAudioFormatLinearPCM<span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mBytesPerPacket</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mFramesPerPacket</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mBytesPerFrame</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mBitsPerChannel</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mFormatFlags</span> <span style="color: #339933;">=</span> kAudioFormatFlagsNativeEndian <span style="color: #339933;">|</span> kAudioFormatFlagIsPacked <span style="color: #339933;">|</span> kAudioFormatFlagIsSignedInteger<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the desired client (output) data format</span>
	err <span style="color: #339933;">=</span> ExtAudioFileSetProperty<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> kExtAudioFileProperty_ClientDataFormat<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>theOutputFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theOutputFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the total frame count</span>
	thePropertySize <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>theFileLengthInFrames<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	err <span style="color: #339933;">=</span> ExtAudioFileGetProperty<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> kExtAudioFileProperty_FileLengthFrames<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>thePropertySize<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theFileLengthInFrames<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Read all the data into memory</span>
	UInt32		dataSize <span style="color: #339933;">=</span> theFileLengthInFrames <span style="color: #339933;">*</span> theOutputFormat.<span style="color: #202020;">mBytesPerFrame</span><span style="color: #339933;">;;</span>
	theData <span style="color: #339933;">=</span> malloc<span style="color: #009900;">&#40;</span>dataSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>theData<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		AudioBufferList		theDataBuffer<span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mNumberBuffers</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mBuffers</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mDataByteSize</span> <span style="color: #339933;">=</span> dataSize<span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mBuffers</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mNumberChannels</span> <span style="color: #339933;">=</span> theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mBuffers</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mData</span> <span style="color: #339933;">=</span> theData<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Read the data into an AudioBufferList</span>
		err <span style="color: #339933;">=</span> ExtAudioFileRead<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>UInt32<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>theFileLengthInFrames<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theDataBuffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err <span style="color: #339933;">==</span> noErr<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// success</span>
			<span style="color: #339933;">*</span>outDataSize <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ALsizei<span style="color: #009900;">&#41;</span>dataSize<span style="color: #339933;">;</span>
			<span style="color: #339933;">*</span>outDataFormat <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> AL_FORMAT_STEREO16 <span style="color: #339933;">:</span> AL_FORMAT_MONO16<span style="color: #339933;">;</span>
			<span style="color: #339933;">*</span>outSampleRate <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ALsizei<span style="color: #009900;">&#41;</span>theOutputFormat.<span style="color: #202020;">mSampleRate</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// failure</span>
			free <span style="color: #009900;">&#40;</span>theData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			theData <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// make sure to return NULL</span>
			<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
Exit<span style="color: #339933;">:</span>
	<span style="color: #666666; font-style: italic;">// Dispose the ExtAudioFileRef, it is no longer needed</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>extRef<span style="color: #009900;">&#41;</span> ExtAudioFileDispose<span style="color: #009900;">&#40;</span>extRef<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> theData<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h5>Writing an OpenAL wrapper class wich is more comfortable than using the OpenAL functions directly.</h5>
<p>What you really want is a singelton pattern here, or how apple named this pattern : shared class.</p>
<p>So lets first take a look how you would write a singelton class in objective c. Situations could arise where you want a singleton instance (created and controlled by the class factory method) but also have the ability to create other instances as needed through allocation and initialization. In these cases, you would not override allocWithZone: and the other methods following it as shown in the following code listing :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> MySingeltonClass <span style="color: #002200;">*</span>sharedSingeltonInstance <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MySingeltonClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedManager
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedSingeltonInstance <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// assignment not done here</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> sharedSingeltonInstance;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>allocWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedSingeltonInstance <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            sharedSingeltonInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super allocWithZone<span style="color: #002200;">:</span>zone<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">return</span> sharedSingeltonInstance;  <span style="color: #11740a; font-style: italic;">// assignment and return on first allocation</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>; <span style="color: #11740a; font-style: italic;">//on subsequent allocation attempts return nil</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>copyWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>retain
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>retainCount
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">UINT_MAX</span>;  <span style="color: #11740a; font-style: italic;">//denotes an object that cannot be released</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>release
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">//do nothing</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>autorelease
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now thats pretty straight forward &#8211; so lets create our OpenAL wrapper class. We need some functions to initialize the device, loading and control sound files. Your interface should look like this :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyOpenAL.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenAL/al.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenAL/alc.h&gt;</span>
<span style="color: #6e371a;">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyOpenAL <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	ALCcontext<span style="color: #002200;">*</span> mContext; <span style="color: #11740a; font-style: italic;">// stores the context (the 'air')</span>
	ALCdevice<span style="color: #002200;">*</span> mDevice; <span style="color: #11740a; font-style: italic;">// stores the device</span>
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span> bufferStorageArray; <span style="color: #11740a; font-style: italic;">// stores the buffer ids from openAL</span>
	<span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span> soundDictionary; <span style="color: #11740a; font-style: italic;">// stores our soundkeys</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// if you want to access directly the buffers or our sound dictionary</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span> bufferStorageArray;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span> soundDictionary;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init; <span style="color: #11740a; font-style: italic;">// init once</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> initOpenAL; <span style="color: #11740a; font-style: italic;">// no need to make it public, but I post it here to show you which methods we need. initOpenAL will be called within init process once.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> playSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey; <span style="color: #11740a; font-style: italic;">// play a sound by name</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> stopSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey; <span style="color: #11740a; font-style: italic;">// stop a sound by name</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isPlayingSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey; <span style="color: #11740a; font-style: italic;">// check if sound is playing by name</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> rewindSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey; <span style="color: #11740a; font-style: italic;">// rewind a sound by name so its playing again</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_soundKey File<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_file Ext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _ext Loop<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span>loops; <span style="color: #11740a; font-style: italic;">// load a sound and give it a name</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyOpenAL<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedMyOpenAL; <span style="color: #11740a; font-style: italic;">// access to our instance</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>So now the easy stuff is done we get to the more complex implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyOpenAL.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;MyOpenAL.h&quot;</span>
<span style="color: #6e371a;">#import &quot;MyOpenALSupport.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyOpenAL
&nbsp;
<span style="color: #a61390;">@synthesize</span> bufferStorageArray;
<span style="color: #a61390;">@synthesize</span> soundDictionary;
&nbsp;
<span style="color: #a61390;">static</span> MyOpenAL <span style="color: #002200;">*</span>sharedMyOpenAL <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> shutdownMyOpenAL <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedMyOpenAL <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>self dealloc<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// assignment not done here</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyOpenAL<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedMyOpenAL <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedMyOpenAL <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// assignment not done here</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> sharedMyOpenAL;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>allocWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedMyOpenAL <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            sharedMyOpenAL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super allocWithZone<span style="color: #002200;">:</span>zone<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">return</span> sharedMyOpenAL;  <span style="color: #11740a; font-style: italic;">// assignment and return on first allocation</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>; <span style="color: #11740a; font-style: italic;">//on subsequent allocation attempts return nil</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>copyWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>retain
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>retainCount
<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">UINT_MAX</span>;  <span style="color: #11740a; font-style: italic;">//denotes an object that cannot be released</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>release
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">//do nothing</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>autorelease
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// seek in audio file for the property 'size'</span>
<span style="color: #11740a; font-style: italic;">// return the size in bytes</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UInt32<span style="color: #002200;">&#41;</span>audioFileSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>AudioFileID<span style="color: #002200;">&#41;</span>fileDescriptor
<span style="color: #002200;">&#123;</span>
	MARK;
	UInt64 outDataSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	UInt32 thePropSize <span style="color: #002200;">=</span> <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>UInt64<span style="color: #002200;">&#41;</span>;
	OSStatus result <span style="color: #002200;">=</span> AudioFileGetProperty<span style="color: #002200;">&#40;</span>fileDescriptor, kAudioFilePropertyAudioDataByteCount, <span style="color: #002200;">&amp;</span>thePropSize, <span style="color: #002200;">&amp;</span>outDataSize<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>result <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cannot find file size&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>UInt32<span style="color: #002200;">&#41;</span>outDataSize;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// start up openAL</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> initOpenAL
<span style="color: #002200;">&#123;</span>
	MARK;
	<span style="color: #11740a; font-style: italic;">// Initialization</span>
	mDevice <span style="color: #002200;">=</span> alcOpenDevice<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// select the &quot;preferred device&quot;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>mDevice<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// use the device to make a context</span>
		mContext<span style="color: #002200;">=</span>alcCreateContext<span style="color: #002200;">&#40;</span>mDevice,<span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #11740a; font-style: italic;">// set my context to the currently active one</span>
		alcMakeContextCurrent<span style="color: #002200;">&#40;</span>mContext<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init <span style="color: #002200;">&#123;</span>
		MARK;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self initOpenAL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			self.bufferStorageArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span>init<span style="color: #002200;">&#93;</span>autorelease<span style="color: #002200;">&#93;</span>;
			self.soundDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span>init<span style="color: #002200;">&#93;</span>autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">return</span> self;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#91;</span>self release<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>
&nbsp;
		MARK;
	<span style="color: #11740a; font-style: italic;">// delete the sources</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> sourceNumber <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>soundDictionary allValues<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sourceNumber unsignedIntegerValue<span style="color: #002200;">&#93;</span>;
		alDeleteSources<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>sourceID<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	self.soundDictionary<span style="color: #002200;">=</span><span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// delete the buffers</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> bufferNumber <span style="color: #a61390;">in</span> bufferStorageArray<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		NSUInteger bufferID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>bufferNumber unsignedIntegerValue<span style="color: #002200;">&#93;</span>;
		alDeleteBuffers<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>bufferID<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	self.bufferStorageArray<span style="color: #002200;">=</span><span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// destroy the context</span>
	alcDestroyContext<span style="color: #002200;">&#40;</span>mContext<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// close the device</span>
	alcCloseDevice<span style="color: #002200;">&#40;</span>mDevice<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// the main method: grab the sound ID from the library</span>
<span style="color: #11740a; font-style: italic;">// and start the source playing</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>playSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey
<span style="color: #002200;">&#123;</span>
		MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
	alSourcePlay<span style="color: #002200;">&#40;</span>sourceID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey
<span style="color: #002200;">&#123;</span>
	MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
	alSourceStop<span style="color: #002200;">&#40;</span>sourceID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> rewindSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey <span style="color: #002200;">&#123;</span>
&nbsp;
	MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
	alSourceRewind <span style="color: #002200;">&#40;</span>sourceID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isPlayingSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey <span style="color: #002200;">&#123;</span>
&nbsp;
	MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	ALenum state;
&nbsp;
    alGetSourcei<span style="color: #002200;">&#40;</span>sourceID, AL_SOURCE_STATE, <span style="color: #002200;">&amp;</span>state<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> AL_PLAYING<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_soundKey File<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_file Ext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _ext Loop<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span>loops<span style="color: #002200;">&#123;</span>
&nbsp;
	START_TIMER;
&nbsp;
	ALvoid <span style="color: #002200;">*</span> outData;
	ALenum  error <span style="color: #002200;">=</span> AL_NO_ERROR;
	ALenum  format;
	ALsizei size;
	ALsizei freq;
&nbsp;
	<span style="color: #400080;">NSBundle</span> <span style="color: #002200;">*</span> bundle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get some audio data from a wave file</span>
	CFURLRef fileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>bundle pathForResource<span style="color: #002200;">:</span>_file ofType<span style="color: #002200;">:</span>_ext<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>fileURL<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		END_TIMER<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file not found.&quot;</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	outData <span style="color: #002200;">=</span> MyGetOpenALAudioData<span style="color: #002200;">&#40;</span>fileURL, <span style="color: #002200;">&amp;</span>size, <span style="color: #002200;">&amp;</span>format, <span style="color: #002200;">&amp;</span>freq<span style="color: #002200;">&#41;</span>;
&nbsp;
	CFRelease<span style="color: #002200;">&#40;</span>fileURL<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>error <span style="color: #002200;">=</span> alGetError<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> AL_NO_ERROR<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;error loading sound: %x<span style="color: #2400d9;">\n</span>&quot;</span>, error<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">exit</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;getting a free buffer from openAL.&quot;</span><span style="color: #002200;">&#41;</span>;
	NSUInteger bufferID;
	<span style="color: #11740a; font-style: italic;">// grab a buffer ID from openAL</span>
	alGenBuffers<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>bufferID<span style="color: #002200;">&#41;</span>;
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;loading audio data into openAL buffer.&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// load the awaiting data blob into the openAL buffer.</span>
	alBufferData<span style="color: #002200;">&#40;</span>bufferID,format,outData,size,freq<span style="color: #002200;">&#41;</span>; 
&nbsp;
	<span style="color: #11740a; font-style: italic;">// save the buffer so we can release it later</span>
	<span style="color: #002200;">&#91;</span>bufferStorageArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithUnsignedInteger<span style="color: #002200;">:</span>bufferID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;getting a free source from openAL.&quot;</span><span style="color: #002200;">&#41;</span>;
	NSUInteger sourceID;
	<span style="color: #11740a; font-style: italic;">// grab a source ID from openAL</span>
	alGenSources<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>sourceID<span style="color: #002200;">&#41;</span>; 
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;attatching the buffer to the source and setting up preferences&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// attach the buffer to the source</span>
	alSourcei<span style="color: #002200;">&#40;</span>sourceID, AL_BUFFER, bufferID<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// set some basic source prefs</span>
	alSourcef<span style="color: #002200;">&#40;</span>sourceID, AL_PITCH, 1.0f<span style="color: #002200;">&#41;</span>;
	alSourcef<span style="color: #002200;">&#40;</span>sourceID, AL_GAIN, 1.0f<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>loops<span style="color: #002200;">&#41;</span> alSourcei<span style="color: #002200;">&#40;</span>sourceID, AL_LOOPING, AL_TRUE<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// store this for future use</span>
	<span style="color: #002200;">&#91;</span>soundDictionary setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithUnsignedInt<span style="color: #002200;">:</span>sourceID<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>_soundKey<span style="color: #002200;">&#93;</span>;	
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;free %i bytes of temporary allocated memory.&quot;</span>, size<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// clean up the buffer</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>outData<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>outData<span style="color: #002200;">&#41;</span>;
		outData <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	END_TIMER<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Audiofile successfully loaded.&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>You may noticed that I use some macros like &#8220;ALog&#8221; and &#8220;Mark&#8221; &#8211; you can find the definitions <a href="http://www.gehacktes.net/2009/02/macros-for-iphone-programming/">here</a>.</p>
<h5>Writing the actual code to play some audio files.</h5>
<p>Notice that you can not load any file format. I successfully used aif and caf files &#8211; so maybe you want use them, too.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;music&quot;</span> File<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;backgroundMusic&quot;</span> Ext<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;aif&quot;</span> Loop<span style="color: #002200;">:</span><span style="color: #a61390;">true</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;beep&quot;</span> File<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;Sound1&quot;</span> Ext<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;aif&quot;</span> Loop<span style="color: #002200;">:</span><span style="color: #a61390;">false</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> playSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;music&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> playSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;beep&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>That&#8217;s it. I hope this post was useful although I know its not going deep into the achitechture of openAL.</p>
<p>I like to announce my first Game in Appstore : <a href="http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/">Bomb Commander</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/feed/</wfw:commentRss>
		<slash:comments>87</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Part 5 : Audio</title>
		<link>http://www.gehacktes.net/2009/02/iphone-programming-part-5-audio/</link>
		<comments>http://www.gehacktes.net/2009/02/iphone-programming-part-5-audio/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 15:37:06 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=339</guid>
		<description><![CDATA[In this post I will talk about programming audio output on iPhone devices. This task comes like the arcade apothegm : &#8220;easy to learn but difficult to master&#8221;. First I have to say that this article is based one this article. The quickest (and easiest) way to make the iPhone spit out some sound is [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will talk about programming audio output on iPhone devices. This task comes like the arcade apothegm : &#8220;easy to learn but difficult to master&#8221;.</p>
<p><span id="more-339"></span><br />
First I have to say that this article is based one <a href="http://benbritten.com/2008/11/06/openal-sound-on-the-iphone/">this</a> article.</p>
<p>The quickest (and easiest) way to make the iPhone spit out some sound is to use the audio system services:</p>
<p>First you have to add the AudioToolbox Framework to your application and import the header files:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #6e371a;">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span></pre></div></div>

<p>to load and play the sound :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;soundName&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;caf&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span> afUrl <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
UInt32 soundID;
AudioServicesCreateSystemSoundID<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span>afUrl,<span style="color: #002200;">&amp;</span>amp;soundID<span style="color: #002200;">&#41;</span>;
AudioServicesPlaySystemSound <span style="color: #002200;">&#40;</span>soundID<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Allthough this is good enough to make some UI interaction sounds you probably want control on preloading files and play the sound synchronously to an action, especially on game development. On the other hand you want to keep your code as portable as possible. So lets go through a super quick introduction to openAL which is conform to all these aspects.</p>
<p>There are five consistently appearing terms openAL you may want to know more about :</p>
<ul>
<li>LISTENER</li>
<li>SOURCE</li>
<li>BUFFER</li>
<li>DEVICE</li>
<li>CONTEXT or SESSION</li>
</ul>
<p>The <strong>LISTENER</strong> is you. Any sound the listener can &#8216;hear&#8217; comes out the speakers. OpenAL allows you to specify where the listener is in relation to the sources, but for this example we do not care, we are going to bare minimum static sound, so just keep in mind that there is a concept of &#8216;listener&#8217; and that you could move this object around if you wanted to do more complicated stuff.</p>
<p>The <strong>SOURCE</strong> is something analogous to a speaker. It generates sound which the listener can &#8216;hear&#8217;. Like the listener, you can move the sources around.</p>
<p>The <strong>BUFFER</strong> contains the raw audio data of the sound that will be played.</p>
<p>The <strong>DEVICE</strong> is the actuial bit of harware that will be playing the sound.</p>
<p>The <strong>CONTEXT</strong> or <strong>SESSION</strong> is something like a room containing all the sources and the listener. Metaphorically speaking the sources are musicians the listener is a microphone and the <strong>CONTEXT</strong> is something like <em>air</em> the sound is going through. You will setup positions of Listener and Sources here and you can specify a category for this:</p>
<ul>
<li><em>kAudioSessionCategory_UserInterfaceSoundEffects</em>:Use this category for sound effects such as touch feedback, explosions, etc.</li>
<li><em>kAudioSessionCategory_AmbientSound</em> : Use this category for backgound sounds such as rain, car engine noise, etc.  Mixes with other music.</li>
<li><em>kAudioSessionCategory_SoloAmbientSound</em> : Use this category for backgound sounds.  Other music will stop playing.</li>
<li><em>kAudioSessionCategory_MediaPlayback</em> : Use this category for music tracks.</li>
<li><em>kAudioSessionCategory_LiveAudio</em> : Use this category for interactive music such as playing an instrument on the screen.</li>
<li><em>kAudioSessionCategory_RecordAudio</em> : Use this category when recording audio.</li>
<li><em>kAudioSessionCategory_PlayAndRecord</em> : Use this category when recording and playing back audio.</li>
</ul>
<p>To get this all work you need five tasks to be done:</p>
<ol>
<li>get the device</li>
<li>make a context with the device</li>
<li>put some data into a buffer</li>
<li>attach the buffer to a source</li>
<li>play the source</li>
</ol>
<p>Audioformats I successfully tested :</p>
<ul>
<li>.CAF (16-Bit PCM) Not Compressed</li>
<li>.AIF (IMA4 16-Bit PCM) Compressed </li>
</ul>
<p><b>.CAF</b><br />
To convert a audiofile to this format simply open a terminal on your mac and type:</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">usr/bin/afconvert -f caff -d LEI16@44100 inputSoundFile.aiff outputSoundFile.caf</pre></div></div>

<p><b>.AIF</b><br />
To convert an audiofile to this format use export on quicktime pro.</p>
<p>In the <a href="http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/">next post</a> I will discuss how to use openAL with multiple sound sources. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/02/iphone-programming-part-5-audio/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
