<?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; programming</title>
	<atom:link href="http://www.gehacktes.net/category/programming/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 underline UILabel</title>
		<link>http://www.gehacktes.net/2010/05/iphone-programming-uilabel-underline/</link>
		<comments>http://www.gehacktes.net/2010/05/iphone-programming-uilabel-underline/#comments</comments>
		<pubDate>Tue, 18 May 2010 17:09:37 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=829</guid>
		<description><![CDATA[With UIKit you get a set of useful things to just click your way to your own application. But there are still some features missing you wont expect. So happend to me when I wanted to have a text look like a hyperlink, so the user gets ants in his pants to click on it. [...]]]></description>
			<content:encoded><![CDATA[<p>With UIKit you get a set of useful things to just click your way to your own application. But there are still some features missing you wont expect. So happend to me when I wanted to have a text look like a hyperlink, so the user gets ants in his pants to click on it. Easily said but, what&#8217;s that? UILabel does not support underline Text. And UIButton doesn&#8217;t either. So here is a working solution to self draw your line.<br />
<span id="more-829"></span><br />
Just enhance your favorite UI class like UIButton and add the following code to it. It should also work if you are have left or right aligned text.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect 
<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Get the Render Context</span>
	CGContextRef ctx <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Measure the font size, so the line fits the text.</span>
	<span style="color: #11740a; font-style: italic;">// Could be that &quot;titleLabel&quot; is something else in other classes like UILable, dont know.</span>
	<span style="color: #11740a; font-style: italic;">// So make sure you fix it here if you are enhancing UILabel or something else..</span>
	CGSize fontSize <span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>self.titleLabel.text sizeWithFont<span style="color: #002200;">:</span>self.titleLabel.font 
											   forWidth<span style="color: #002200;">:</span>self.bounds.size.width
										  lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeTailTruncation<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// Get the fonts color. </span>
	<span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> <span style="color: #002200;">*</span> colors <span style="color: #002200;">=</span> CGColorGetComponents<span style="color: #002200;">&#40;</span>self.titleLabel.textColor.CGColor<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// Sets the color to draw the line</span>
	CGContextSetRGBStrokeColor<span style="color: #002200;">&#40;</span>ctx, colors<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, colors<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, colors<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>, 1.0f<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Format : RGBA</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Line Width : make thinner or bigger if you want</span>
	CGContextSetLineWidth<span style="color: #002200;">&#40;</span>ctx, 1.0f<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate the starting point (left) and target (right)	</span>
	<span style="color: #a61390;">float</span> fontLeft <span style="color: #002200;">=</span> self.titleLabel.center.x <span style="color: #002200;">-</span> fontSize.width<span style="color: #002200;">/</span><span style="color: #2400d9;">2.0</span>;
	<span style="color: #a61390;">float</span> fontRight <span style="color: #002200;">=</span> self.titleLabel.center.x <span style="color: #002200;">+</span> fontSize.width<span style="color: #002200;">/</span><span style="color: #2400d9;">2.0</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Add Move Command to point the draw cursor to the starting point</span>
	CGContextMoveToPoint<span style="color: #002200;">&#40;</span>ctx, fontLeft, self.bounds.size.height <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Add Command to draw a Line</span>
	CGContextAddLineToPoint<span style="color: #002200;">&#40;</span>ctx, fontRight, self.bounds.size.height <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Actually draw the line.</span>
	CGContextStrokePath<span style="color: #002200;">&#40;</span>ctx<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// should be nothing, but who knows...</span>
	<span style="color: #002200;">&#91;</span>super drawRect<span style="color: #002200;">:</span>rect<span style="color: #002200;">&#93;</span>;   
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/05/iphone-programming-uilabel-underline/feed/</wfw:commentRss>
		<slash:comments>2</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>Tip: how not to step into code in visual studio</title>
		<link>http://www.gehacktes.net/2010/03/tip-how-not-to-step-into-code-in-visual-studio/</link>
		<comments>http://www.gehacktes.net/2010/03/tip-how-not-to-step-into-code-in-visual-studio/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 10:18:37 +0000</pubDate>
		<dc:creator>fschaper</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[productivity improvement]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=801</guid>
		<description><![CDATA[At my job, I heavily rely on working with the STL and the Boost libraries. During debugging, which as we all know takes up a certain amount of ones time, I found myself seemingly more often than not looking at STL or Boost code while stepping through. At some point I really got tired of [...]]]></description>
			<content:encoded><![CDATA[<p>At my job, I heavily rely on working with the STL and the Boost libraries.<br />
During debugging, which as we all know takes up a certain amount of ones time, I found myself seemingly more often than not looking at STL or Boost code while stepping through. </p>
<p>At some point I really got tired of hitting SHIFT+F11 to jump out of another section of code that I knew, wasn&#8217;t really the problem.</p>
<p>I found an interesting article about preventing the visual studio debugger from stepping into certain areas of your code, that I would like to share with you:<br />
<a href="http://blogs.msdn.com/andypennell/archive/2004/02/06/69004.aspx">http://blogs.msdn.com/andypennell/archive/2004/02/06/69004.aspx</a></p>
<p>Note that <b>in case you use a x64 based system</b> you will need to place the registry information into the HKLM\Software\Wow6432Node\Microsoft\VisualStudio\8.0\NativeDE entry (you can read more about this at <a href="http://support.microsoft.com/kb/896459">http://support.microsoft.com/kb/896459</a>).</p>
<p>So this is what I use on my Windows7 x64 installation running Visual Studio 2005:</p>

<div class="wp_syntax"><div class="code"><pre class="reg" style="font-family:monospace;"><span style="color: #0000FF;">Windows Registry Editor Version 5.00</span>
&nbsp;
<span style="color: #000000;">&#91;</span><span style="color: #800000;">HKEY_LOCAL_MACHINE</span>\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\8.0\NativeDE\StepOver<span style="color: #000000;">&#93;</span>
<span style="color: #0000FF;">&quot;no boost&quot;</span><span style="color: #000000;">=</span><span style="">&quot;boost\\:\\:.*=NoStepInto&quot;</span>
<span style="color: #0000FF;">&quot;no stl&quot;</span><span style="color: #000000;">=</span><span style="">&quot;std\\:\\:.*=NoStepInto&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/03/tip-how-not-to-step-into-code-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>singleton dependencies</title>
		<link>http://www.gehacktes.net/2010/03/singleton-dependencies/</link>
		<comments>http://www.gehacktes.net/2010/03/singleton-dependencies/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 13:57:35 +0000</pubDate>
		<dc:creator>fschaper</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=708</guid>
		<description><![CDATA[The singleton pattern, while not without debate[1], has many friends. Some reasons for that may be because it&#8217;s convenient to use, easy to understand and seemingly easy to implement. The subject I want to discuss today is singleton dependencies in C++. the problem Say you have a singleton implementation that you use to retrieve pooled database [...]]]></description>
			<content:encoded><![CDATA[<p>The singleton pattern, while not without debate<a href="#ref_1" class="ref">[1]</a>, has many friends. Some reasons for that may be because it&#8217;s convenient to use, easy to understand and <em>seemingly</em> easy to implement.</p>
<p>The subject I want to discuss today is <em>singleton dependencies</em> in C++.</p>
<h2>the problem</h2>
<p>Say you have a singleton implementation that you use to retrieve pooled database connections. Imagine you have yet another singleton that handles object serialization to a database. Depending on the order of initialization of both singletons, the database connection pool might have already been destroyed even though you still require it during the destruction of your singleton which handles the serialization<a href="#ref_2" class="ref">[2]</a>. The C++ standard does not define an order of initialization of global static objects, but it does specify that the objects have to be destroyed in the reverse order of their creation. If we could assure that the database singleton object would be created <em>before</em> the serialization object, it would be still around during the shutdown of our serialization singleton.</p>
<h2>a basic singleton implementation</h2>
<p>A singleton implementation that I&#8217;m quite fond of is the &#8220;<em>Meyers Singleton</em>&#8220;<a href="#ref_3" class="ref">[3]</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> my_singleton <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">static</span> my_singleton<span style="color: #000040;">&amp;</span> instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> my_singleton instance<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> instance<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  my_singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// since the destructor is also private we don't have to</span>
  <span style="color: #666666;">// declare the copy constructor as private but it underlines the idea.</span>
  my_singleton<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> my_singleton<span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  ~my_singleton<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The object will be instantiated by the first usage of the singleton (lazy instantiation). While it&#8217;s not thread-safe<a href="#ref_4" class="ref">[4]</a> you could work around that by initializing the singleton before you create multiple threads or use an alternative implementation. Since we use lazy instantiation (the object <code>my_singleton</code> will only be created when you first call the <code>my_singleton::instance()</code> class method) we elegantly navigated ourselves around the unordered initialization problem. The problem with the first constructed, last destroyed problem however still persists. We get to that later &#8230;</p>
<h2>a take on a generic solution</h2>
<p>The following is my approach to an C++ Template for this pattern. Here I use a template idiom known as the <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" target="_blank">CRTP</a> (Curiously Recurring Template Pattern) to inherit the singleton template from the class that we want to implement the singleton pattern.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> derived_T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> singleton <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">static</span> derived_T<span style="color: #000040;">&amp;</span> instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> derived_T instance<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> instance<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
  singleton <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  singleton<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> singleton<span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  ~singleton <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// example of an class using this pattern.</span>
<span style="color: #666666;">// we use CRTP to inherit from the singleton template.</span>
<span style="color: #0000ff;">class</span> my_consumer <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>my_consumer<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>my_consumer<span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">void</span> do_something_amazing<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  my_consumer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<h2>another look at the problem</h2>
<p>let&#8217;s now see how our example from the beginning would look in code:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #666666;">// -- header file 1</span>
<span style="color: #339900;">#include &lt;singleton.hpp&gt; // our singleton template</span>
&nbsp;
<span style="color: #0000ff;">class</span> database_connection_pool <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>database_connection_pool<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>database_connection_pool<span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> 
  shared_connection_type connect<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> connection_string <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// ...</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  database_connection_pool<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// -- header file 2</span>
<span style="color: #339900;">#include &lt;singleton.hpp&gt; // our singleton template</span>
<span style="color: #339900;">#include &quot;database_connection_pool.hpp&quot;</span>
&nbsp;
<span style="color: #0000ff;">class</span> object_serializer <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>object_serializer<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>object_serializer<span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> object_T<span style="color: #000080;">&gt;</span>
  <span style="color: #0000ff;">void</span> serialize<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> object_T<span style="color: #000040;">&amp;</span> obj <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    database_connection_pool<span style="color: #000040;">&amp;</span> db_pool <span style="color: #000080;">=</span> database_connection_pool<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>    
    <span style="color: #666666;">// ...</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  object_serializer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>in case the <code>object_serializer</code> is called first from the code it will also be the last object to be destroyed. If we wanted to serialize a set of registered objects from within the destructor of the <code>object_serializer</code> we would then have to live with the fact that the <code>database_connection_pool</code> would be re-constructed for this one time serialization. Which might not be a terrible thing &#8211; but then again it would not be your <em>singleton</em> again, right? While the pattern description only limits the number of instances to one, in my opinion you should also always have to interact with the &#8216;same&#8217; object. Imagine now that you might not be dealing with a connection manager here but an singleton which provides you with pooled memory and functions to retrieve and free memory. Here you might well pass an already freed pointer to a free method of an singleton which is not really the &#8220;singleton&#8221; you expected it to be.</p>
<p>Now that we have defined our problem, let&#8217;s find an solution. Which comes in the form of an policy class.</p>
<h2>compile time dependency specification with the help of a policy class</h2>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #0000ff;">namespace</span> policy <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">class</span> no_dependency <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> dep1_T<span style="color: #000080;">&gt;</span>
  <span style="color: #0000ff;">class</span> singleton_dependency <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    singleton_dependency<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      dep1_T<span style="color: #000040;">&amp;</span> dep1 <span style="color: #000080;">=</span> dep1_T<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> dep1_T, <span style="color: #0000ff;">class</span> dep2_T<span style="color: #000080;">&gt;</span>
  <span style="color: #0000ff;">class</span> singleton_dependency_2 <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    singleton_dependency_2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      dep1_T<span style="color: #000040;">&amp;</span> dep1 <span style="color: #000080;">=</span> dep1_T<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      dep2_T<span style="color: #000040;">&amp;</span> dep2 <span style="color: #000080;">=</span> dep2_T<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> derived_T, <span style="color: #0000ff;">class</span> dependency_T <span style="color: #000080;">=</span> policy<span style="color: #008080;">::</span><span style="color: #007788;">no_dependency</span> <span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> singleton <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">static</span> derived_T<span style="color: #000040;">&amp;</span> instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> dependency_T dependency<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">static</span> derived_T instance<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> instance<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
  singleton <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  singleton<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> singleton<span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  ~singleton <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>what has changed?<br />
the default behavior of our singleton class hasn&#8217;t changed really. Because we pass the <code>policy::no_dependency</code> class as a default template parameter we won&#8217;t have to change our existing code. The method <code>singleton::instance</code> has also changed slightly, there is an additional call to the default created constructor of the no_dependency class that the compiler will most likely be able to optimize away from the code. But if we wanted to express now that there is a dependency relationship, we could easily do so by passing the additional template parameter along. As we can see in the next example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #0000ff;">class</span> call_before_test <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// ...</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  call_before_test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;before_test constructor called&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> test <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>test, policy<span style="color: #008080;">::</span><span style="color: #007788;">singleton_dependency</span><span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>test, policy<span style="color: #008080;">::</span><span style="color: #007788;">singleton_dependency</span><span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// ...</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;test constructor called&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  test<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>you will also be able to create dependency hierarchies with this, just be careful not to create cyclic dependencies.<br />
As you can see from the example if you would try it yourself <code>call_before_test</code> will be instantiated before <code>test</code>.</p>
<h2>complete example</h2>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">namespace</span> policy <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">class</span> no_dependency <span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> dep1_T<span style="color: #000080;">&gt;</span>
	<span style="color: #0000ff;">class</span> singleton_dependency <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		singleton_dependency<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			dep1_T<span style="color: #000040;">&amp;</span> dep1 <span style="color: #000080;">=</span> dep1_T<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> dep1_T, <span style="color: #0000ff;">class</span> dep2_T<span style="color: #000080;">&gt;</span>
	<span style="color: #0000ff;">class</span> singleton_dependency_2 <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		singleton_dependency_2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			dep1_T<span style="color: #000040;">&amp;</span> dep1 <span style="color: #000080;">=</span> dep1_T<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			dep2_T<span style="color: #000040;">&amp;</span> dep2 <span style="color: #000080;">=</span> dep2_T<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> derived_T, <span style="color: #0000ff;">class</span> dependency_T <span style="color: #000080;">=</span> policy<span style="color: #008080;">::</span><span style="color: #007788;">no_dependency</span> <span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> singleton <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">static</span> derived_T<span style="color: #000040;">&amp;</span> instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">static</span> dependency_T dependency<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">static</span> derived_T instance<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> instance<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
	singleton <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
        singleton<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> singleton<span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	~singleton <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> call_before_test <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #666666;">// ...</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	call_before_test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;call_before_test constructor called&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	~call_before_test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;call_before_test destructor called&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> test <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> singleton<span style="color: #000080;">&lt;</span>test, policy<span style="color: #008080;">::</span><span style="color: #007788;">singleton_dependency</span><span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">class</span> singleton<span style="color: #000080;">&lt;</span>test, policy<span style="color: #008080;">::</span><span style="color: #007788;">singleton_dependency</span><span style="color: #000080;">&lt;</span>call_before_test<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #666666;">// ...</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;test constructor called&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	~test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;test destructor called&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	test<span style="color: #008080;">::</span><span style="color: #007788;">instance</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>sideline discussion</h2>
<p><a name="ref_1">[1]</a> &#8211; From an OO perspective one could argue against this pattern that it hides dependencies in the code. It makes also unit testing a challenge since you have to deal with global state.  In my opinion the singleton may be a valid solution for a given problem (logging for example) but it is certainly no silver bullet.<br/></p>
<p><a name="ref_2">[2]</a> &#8211; In C++ global static objects are initialized in undefined order and destroyed in the reverse order of their creation. Since the standard does not define a certain order you can not rely on it. Even when you may get away in assuming a fixed order initialization of static objects in one compilation unit for one given compiler set, the same order for another build environment would be based on sheer luck as would be the correct initialization over multiple compilation units.</p>
<p><a name="ref_3">[3]</a> &#8211; More Effective C++: S.Meyers.</p>
<p><a name="ref_4">[4]</a> &#8211; even though you can&#8217;t see it from the C++ code, there is a non-thread-safe test to check if the <code>static my_singleton instance;</code> needs to be initialized or not as shown in the assembly output below:<br/><br />
<a href="http://www.gehacktes.net/wp-content/uploads/2010/03/singleton1.jpg" style="margin: 2 0 0 2;"><img src="http://www.gehacktes.net/wp-content/uploads/2010/03/singleton1.jpg" alt="" title="singleton" width="707" height="262" class="alignnone size-full wp-image-729" /></a></p>
<p>Further reading:<br />
- <a href="http://www.boostcookbook.com/Recipe:/1235044" target="_blank">Thread-safe lazy singleton template class</a> &#8211; a thread safe singleton class based on boost library support.<br />
- <a href="http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf#search=%22meyers%20double%20checked%20locking%22" target="_blank">C++ and the Perils of Double-Checked Locking</a> &#8211; a interesting paper that discusses some problems with this pattern when it comes to multi-threading.<br />
- <a href="http://www.boost.org/doc/libs/1_36_0/boost/pool/detail/singleton.hpp" target="_blank">Cleary, Stephen. boost/pool/detail/singleton.hpp. 2000. 16 Mar 2009</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/03/singleton-dependencies/feed/</wfw:commentRss>
		<slash:comments>0</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>Understanding OpenGL Blend Function</title>
		<link>http://www.gehacktes.net/2010/01/alphablending-with-opengl/</link>
		<comments>http://www.gehacktes.net/2010/01/alphablending-with-opengl/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 18:19:08 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=564</guid>
		<description><![CDATA[I really don&#8217;t know who and why some one create a so complex thing like alpha blending modes in openGL. When I first saw these GL_ONE, GL_ONE_MINUS_SRC_ALPHA stuff I really did not get it. I did not get on second time, and the same when using it for thousand times. Long story short, here is [...]]]></description>
			<content:encoded><![CDATA[<p>I really don&#8217;t know who and why some one create a so complex thing like alpha blending modes in openGL. When I first saw these <strong>GL_ONE</strong>, <strong>GL_ONE_MINUS_SRC_ALPHA</strong> stuff I really did not get it. I did not get on second time, and the same when using it for thousand times. Long story short, here is what you need in the most cases: cross blending, additive blending, multiplied blending.<br />
<span id="more-564"></span></p>
<h1>Cross Blending</h1>
<p>Alpha of source will control the blending. If alpha is 1.0 you will only see the source, if its 0.0 you will see the target. Use : <strong>glBlendFunc( GL_SRC_ONE_MINUS_ALPHA, GL_SRC_ALPHA );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/crossblend.png"><img class="alignnone size-thumbnail wp-image-608" title="crossblend" src="http://www.gehacktes.net/wp-content/uploads/2010/01/crossblend-150x150.png" alt="OpenGL Crossblending" width="150" height="150" /></a><br />
</strong></p>
<pre>Rn = Rz * (1.0 - Aq) + Rq * Aq;
Gn = Gz * (1.0 - Aq) + Gq * Aq;
Bn = Bz * (1.0 - Aq) + Bq * Aq;
An = Az * (1.0 - Aq) + Aq * Aq;
</pre>
<h1>Additive Color Blending</h1>
<p>You need this to lighten up things. Use: <strong>glBlendFunc( GL_ONE, GL_ONE );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive.png"><img class="alignnone size-thumbnail wp-image-610" title="additive" src="http://www.gehacktes.net/wp-content/uploads/2010/01/additive-150x150.png" alt="Additive Color Blending" width="150" height="150" /></a></strong></p>
<pre>Rn = Rz + Rq;
Gn = Gz + Gq;
Bn = Bz + Bq;
An = Az + Aq;</pre>
<h1>Additive Alpha Blending</h1>
<p>Its same like additive color blending, but it uses alpha channel of source to define the blending strength. Use <strong>glBlendFunc( GL_ONE, GL_SRC_ALPHA );</strong></p>
<p><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive.png"><strong></strong></a><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive_color.png"><img class="alignnone size-thumbnail wp-image-609" title="additive_color" src="http://www.gehacktes.net/wp-content/uploads/2010/01/additive_color-150x150.png" alt="Additive Color Blending" width="150" height="150" /></a></strong><strong><br />
</strong></p>
<pre>Rn = Rz + Rq * Aq;
Gn = Gz + Gq * Aq;
Bn = Bz + Bq * Aq;
An = Az + Aq * Aq;</pre>
<h1>Multiplied Blending</h1>
<p>You just multiply the color values. You could use this to colorize a scene. etc. Use: <strong>glBlendFunc( GL_ZERO, GL_SRC_COLOR );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/multiply.png"><img class="alignnone size-thumbnail wp-image-611" title="multiply" src="http://www.gehacktes.net/wp-content/uploads/2010/01/multiply-150x150.png" alt="Multiplied Blending" width="150" height="150" /></a><br />
</strong></p>
<pre>Rn = Rz * Rq;
Gn = Gz * Gq;
Bn = Bz * Bq;
An = Az * Aq;
</pre>
<p>Hope this is useful for some one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/01/alphablending-with-opengl/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
