<?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</title>
	<atom:link href="http://www.gehacktes.net/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>Mon, 08 Mar 2010 12:02:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 Math</h1>
<p>There are many collision detections you may want to test, lets say if you have fast moving projectiles, you probably want to test if a line (motion between two points of one projectile) collides with another (motion between two other points of a second projectile). Since this is a lot of stuff I will put that into my next post. </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>0</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 a [...]]]></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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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="" 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>7</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 the [...]]]></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>0</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 if you [...]]]></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>1</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_DST_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>0</slash:comments>
		</item>
		<item>
		<title>Change Company Name in XCode</title>
		<link>http://www.gehacktes.net/2009/11/xcodecompanyname/</link>
		<comments>http://www.gehacktes.net/2009/11/xcodecompanyname/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 13:00:36 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[xcode]]></category>

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

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

<p>To change <code>__MyCompanyName__</code> to your company, just enter the following command into your console:</p>

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

]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/11/xcodecompanyname/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setup XCode SDK 3.0 to work for OS 3.0.1</title>
		<link>http://www.gehacktes.net/2009/09/setup-xcode-sdk-3-0-to-work-for-os-3-0-1/</link>
		<comments>http://www.gehacktes.net/2009/09/setup-xcode-sdk-3-0-to-work-for-os-3-0-1/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:36:49 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

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

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

<p>XCode should now work fine with OS 3.01</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/09/setup-xcode-sdk-3-0-to-work-for-os-3-0-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>calling functions from inside the watch window</title>
		<link>http://www.gehacktes.net/2009/08/calling-functions-from-inside-the-watch-window/</link>
		<comments>http://www.gehacktes.net/2009/08/calling-functions-from-inside-the-watch-window/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 01:53:59 +0000</pubDate>
		<dc:creator>fschaper</dc:creator>
				<category><![CDATA[productivity improvement]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=503</guid>
		<description><![CDATA[It sometimes becomes handy to be able to call functions or methods while debugging. Say you have some complex type that you want to visualize on the console or to your debug output.
The VisualStudio expression engine makes this an easy task (no need to brush up your assembler knowledge on this one) just enter the [...]]]></description>
			<content:encoded><![CDATA[<p>It sometimes becomes handy to be able to call functions or methods while debugging. Say you have some complex type that you want to visualize on the console or to your debug output.</p>
<p>The VisualStudio expression engine makes this an easy task (no need to brush up your assembler knowledge on this one) just enter the function signature into your watch window. Note that you will need to fully qualify any namespaces.</p>
<p>If your project consists of multiple modules, or when you use the same function signature in different compilation units you will have to tell the debugger where to find the symbol.<br />
For this the context operator <kbd>{[function],[source],[module] } location</kbd> is defined.<br />
You should have a look at the reference<a href="#ref_1" class="ref">[1]</a> over at MSDN to familiarize yourself with the syntax.</p>
<p><a name="ref_1">[1]</a> &#8211; The context operator &#8211; <a href="http://msdn.microsoft.com/en-us/library/wztycb7f%28VS.80%29.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/wztycb7f%28VS.80%29.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/08/calling-functions-from-inside-the-watch-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bomb Commander on iPhone and iPod Touch</title>
		<link>http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/</link>
		<comments>http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 14:25:31 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=466</guid>
		<description><![CDATA[

Bomb Commander  Deploys on App Store
Fast-paced Space Combat Title Released for Download Today
Prepare yourself for an intense battle to save the Earth from total annihilation as Bomb Commander releases today in the App Store.
Developed by ANIMA Entertainment, Bomb Commander rests the fate of the world on your fingers as you seek to end a [...]]]></description>
			<content:encoded><![CDATA[<div style="width:512px;">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=324167937"><img src="http://www.bomb-commander.com/img/BC_Logo_Coders.jpg" alt=""/></a><br />
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=324167937"><strong>Bomb Commander  Deploys on App Store</strong></a></p>
<p>Fast-paced Space Combat Title Released for <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=324167937">Download</a> Today</p>
<p>Prepare yourself for an intense battle to save the Earth from total annihilation as Bomb Commander releases today in the App Store.</p>
<p>Developed by <a href="http://www.anima-entertainment.com">ANIMA Entertainment</a>, <a href="http://www.bomb-commander.com">Bomb Commander</a> rests the fate of the world on your fingers as you seek to end a massive alien invasion from completely eliminating the human race.  Armed with the fiercest arsenal of bombs ever to hit mobile gaming, battle treacherous foes in outer space using both smart tactics and quick, precise maneuvers on your iPhone/iPod Touch.</p>
<p>Features:</p>
<ul>
<li>Ultimate Bombing Power — Use blast-bombs, lightning bombs and black-hole bombs to create chain reactions and conquer your foes</li>
<li>Unique Gaming Mechanics — Providing a whole-new spin on the “space invasion” genre, take advantage of precision touch controls that one can only experience on the iPhone!</li>
<li>Beautiful Space Environments — Marvel at the chaos and destruction as you bomb your way through the scenic depths of space.</li>
<li>Track Scores via Online Leaderboards — Show off your bombing skills to the world through online leaderboards and the tracking of your scores.</li>
</ul>
<p>Think. Bomb. Win.<br />
<div style="text-align:center;">
	<script type='text/javascript'>
		var flashvars = {slidePHP:"http://www.gehacktes.net/wp-content/plugins/made-by-simple-slideshow/_mbs_/slidecontent.php?id%3D466%26t%3D3%26",slideSWF:"http://www.gehacktes.net/wp-content/plugins/made-by-simple-slideshow/_mbs_/assets/mbs_slideplayer_small.swf",slideAlign:"horizontal",ttlAlign:"center",backcolor:"0x000000"};
		var params = {bgcolor:"#000000",scale:"noscale",menu:"true",allowfullscreen:"true",wmode:"transparent"};
		var attributes = {};
		attributes.align = 'middle';
		swfobject.embedSWF("http://www.gehacktes.net/wp-content/plugins/made-by-simple-slideshow/_mbs_/assets/mbs_slideplayer_loader.swf","slideshow_466","500","333","9.0.28", "assets/swfobject/expressinstall.swf", flashvars, params, attributes);
	</script>
	<div id='slideshow_466'>

<a href='http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/bombcommander_1/' title='BombCommander_1'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2009/08/BombCommander_1-150x150.jpg" class="attachment-thumbnail" alt="" title="BombCommander_1" /></a>
<a href='http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/bombcommander_2/' title='BombCommander_2'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2009/08/BombCommander_2-150x150.jpg" class="attachment-thumbnail" alt="" title="BombCommander_2" /></a>
<a href='http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/bombcommander_3/' title='BombCommander_3'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2009/08/BombCommander_3-150x150.jpg" class="attachment-thumbnail" alt="" title="BombCommander_3" /></a>
<a href='http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/bombcommander_4/' title='BombCommander_4'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2009/08/BombCommander_4-150x150.jpg" class="attachment-thumbnail" alt="" title="BombCommander_4" /></a>
<a href='http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/bombcommander_5/' title='BombCommander_5'><img width="150" height="150" src="http://www.gehacktes.net/wp-content/uploads/2009/08/BombCommander_5-150x150.jpg" class="attachment-thumbnail" alt="" title="BombCommander_5" /></a>
	</div>
</div>

</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>visual studio breakpoint dependency hierarchy</title>
		<link>http://www.gehacktes.net/2009/06/visual-studio-breakpoint-dependency-hierarchy/</link>
		<comments>http://www.gehacktes.net/2009/06/visual-studio-breakpoint-dependency-hierarchy/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 16:27:28 +0000</pubDate>
		<dc:creator>fschaper</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=402</guid>
		<description><![CDATA[A serious amount of time during software development is spent with debugging. Visual Studio makes the task of debugging native applications already a lot more comfortable but as with all things there is still room for improvement.
One feature that I was missing of late was the concept of hierarchical breakpoints.
Often during debugging I want the debugger [...]]]></description>
			<content:encoded><![CDATA[<p>A serious amount of time during software development is spent with debugging. Visual Studio makes the task of debugging native applications already a lot more comfortable but as with all things there is still room for improvement.</p>
<p>One feature that I was missing of late was the concept of <em>hierarchical breakpoints</em>.<br />
Often during debugging I want the debugger to break in some method or function but only coming from an specific path.</p>
<p>To give you an example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> some_function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #ff0000; font-style: italic;">/* breakpoint here */</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> some_other_function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #ff0000; font-style: italic;">/* breakpoint here */</span>
    some_function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</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>
    some_function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    some_other_function<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>What I&#8217;d like to do now is to break in &#8220;some_function&#8221; but only coming from &#8220;some_other_function&#8221;.<br />
In this easy example I could happily press F5 a couple of times and be done with it. I could also use the breakpoints hit-counter to accomplish that goal.<br />
But for more complex software this quickly becomes unpractical.</p>
<p>Luckily for me Visual Studio provides an rich extension framework.</p>
<p><a href="http://www.gehacktes.net/wp-content/uploads/2009/06/breakpoints.jpg"><img class="alignnone size-full wp-image-461" title="breakpoints" src="http://www.gehacktes.net/wp-content/uploads/2009/06/breakpoints.jpg" alt="breakpoints" width="631" height="793" /></a></p>
<p>You see two breakpoints in the image above. By Draging&#8217;n'Droping the breakpoint in line 7 on the one in line 11 the one in line 7 got disabled. Once the program hits line 11 during a debug run the AddIn will in turn enable all child breakpoints (the one in line 7 in this case).</p>
<h2>tutorial</h2>
<p>We have created a quick tutorial video <a href="http://www.gehacktes.net/bdh-tutorial/" target="_self">here</a>.</p>
<h2>download</h2>
<p>You can download the project (source only) from <a href="https://sourceforge.net/projects/bdh/" target="_blank">sourceforge</a>. Drop me a visit at <a href="https://www.ohloh.net/p/BDH" target="_blank">ohloh</a> if you like the software.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/06/visual-studio-breakpoint-dependency-hierarchy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
