<?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>JohnnyWorks &#187; iPhone Development</title>
	<atom:link href="http://johnehartzog.com/category/iphonedev/feed/" rel="self" type="application/rss+xml" />
	<link>http://johnehartzog.com</link>
	<description>Programming projects and sample code</description>
	<lastBuildDate>Tue, 18 Oct 2011 21:28:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Easy To Create Buttons with Cocos2D</title>
		<link>http://johnehartzog.com/2009/10/easy-to-create-buttons-with-cocos2d/</link>
		<comments>http://johnehartzog.com/2009/10/easy-to-create-buttons-with-cocos2d/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 20:55:43 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[cocos2d iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=396</guid>
		<description><![CDATA[Those of you who use cocos2d a lot might understand why I created this class as some point. Hopefully it may save some of you those nasty 5 line blobs that you normally need to create a simple button. Usage &#8230; <a href="http://johnehartzog.com/2009/10/easy-to-create-buttons-with-cocos2d/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Those of you who use cocos2d a lot might understand why I created this class as some point. Hopefully it may save some of you those nasty 5 line blobs that you normally need to create a simple button. Usage is simple, just do:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>Button buttonWithText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;back&quot;</span> atPosition<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</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>back<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>Button buttonWithImage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;openFeint.png&quot;</span> atPosition<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">400</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</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>openOpenFeint<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>You&#8217;ll need to create your own button.png and button_p.png (the second one is the image shown when you are touching the button). Also you&#8217;ll need to choose your own font. Here is the code&#8230;</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;">//  Button.h</span>
<span style="color: #11740a; font-style: italic;">//  StickWars - Siege</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by EricH on 8/3/09.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Button <span style="color: #002200;">:</span> Menu <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>text atPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>position target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>file atPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>position target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ButtonItem <span style="color: #002200;">:</span> MenuItem <span style="color: #002200;">&#123;</span>
	Sprite <span style="color: #002200;">*</span>back;
	Sprite <span style="color: #002200;">*</span>backPressed;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>text target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>file target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>text target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>file target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector;
<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;">//  Button.m</span>
<span style="color: #11740a; font-style: italic;">//  StickWars - Siege</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by EricH on 8/3/09.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;Button.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> Button
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>text atPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>position target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector <span style="color: #002200;">&#123;</span>
	Menu <span style="color: #002200;">*</span>menu <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Menu menuWithItems<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ButtonItem buttonWithText<span style="color: #002200;">:</span>text target<span style="color: #002200;">:</span>target selector<span style="color: #002200;">:</span>selector<span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	menu.position <span style="color: #002200;">=</span> position;
	<span style="color: #a61390;">return</span> menu;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>file atPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>position target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector <span style="color: #002200;">&#123;</span>
	Menu <span style="color: #002200;">*</span>menu <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Menu menuWithItems<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ButtonItem buttonWithImage<span style="color: #002200;">:</span>file target<span style="color: #002200;">:</span>target selector<span style="color: #002200;">:</span>selector<span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	menu.position <span style="color: #002200;">=</span> position;
	<span style="color: #a61390;">return</span> menu;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ButtonItem
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>text target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> initWithText<span style="color: #002200;">:</span>text target<span style="color: #002200;">:</span>target selector<span style="color: #002200;">:</span>selector<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>buttonWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>file target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>file target<span style="color: #002200;">:</span>target selector<span style="color: #002200;">:</span>selector<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>text target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithTarget<span style="color: #002200;">:</span>target selector<span style="color: #002200;">:</span>selector<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		back <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Sprite spriteWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button.png&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		back.anchorPoint <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
		backPressed <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Sprite spriteWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button_p.png&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		backPressed.anchorPoint <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>back<span style="color: #002200;">&#93;</span>;
&nbsp;
		self.contentSize <span style="color: #002200;">=</span> back.contentSize;
&nbsp;
		Label<span style="color: #002200;">*</span> textLabel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Label labelWithString<span style="color: #002200;">:</span>text fontName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;take_out_the_garbage&quot;</span> fontSize<span style="color: #002200;">:</span><span style="color: #2400d9;">22</span><span style="color: #002200;">&#93;</span>;
		textLabel.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span>self.contentSize.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>, self.contentSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
		textLabel.anchorPoint <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.5</span>, <span style="color: #2400d9;">0.3</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>textLabel z<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>file target<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithTarget<span style="color: #002200;">:</span>target selector<span style="color: #002200;">:</span>selector<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		back <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Sprite spriteWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button.png&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		back.anchorPoint <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
		backPressed <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Sprite spriteWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button_p.png&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		backPressed.anchorPoint <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>back<span style="color: #002200;">&#93;</span>;
&nbsp;
		self.contentSize <span style="color: #002200;">=</span> back.contentSize;
&nbsp;
		Sprite<span style="color: #002200;">*</span> image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Sprite spriteWithFile<span style="color: #002200;">:</span>file<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>image z<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
		image.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span>self.contentSize.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>, self.contentSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> selected <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self removeChild<span style="color: #002200;">:</span>back cleanup<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>backPressed<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super selected<span style="color: #002200;">&#93;</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> unselected <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self removeChild<span style="color: #002200;">:</span>backPressed cleanup<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>back<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super unselected<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// this prevents double taps</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>activate <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super activate<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setIsEnabled<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self schedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>resetButton<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> interval<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span><span style="color: #002200;">&#93;</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>resetButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span>dt <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self unschedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>resetButton<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setIsEnabled<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>back release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>backPressed release<span style="color: #002200;">&#93;</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: #a61390;">@end</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/10/easy-to-create-buttons-with-cocos2d/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>2D Scrolling Game with Cocos2D TileMap with Zoom!</title>
		<link>http://johnehartzog.com/2009/10/2d-scrolling-game-with-cocos2d-tilemap-with-zoom/</link>
		<comments>http://johnehartzog.com/2009/10/2d-scrolling-game-with-cocos2d-tilemap-with-zoom/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 14:39:14 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[cocos2d iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=391</guid>
		<description><![CDATA[A common question I see on the cocos2d forums is &#8216;when I want to make my game scroll, do I move the camera or the layer?&#8217; or some variant of that. I also got some more detailed questions about how &#8230; <a href="http://johnehartzog.com/2009/10/2d-scrolling-game-with-cocos2d-tilemap-with-zoom/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>A common question I see on the cocos2d forums is &#8216;when I want to make my game scroll, do I move the camera or the layer?&#8217; or some variant of that. I also got some more detailed questions about how to make a functioning 2D scroller, so I&#8217;m going to described how I got it to work.</p>
<p>First of all, the answer to the above question is you move the layer. If you follow the examples in the cocos2d download, you have a &#8220;GameScene&#8221; and a &#8220;GameLayer&#8221;. Well, everything that needs to be moved when your game scrolls should be added as a child to that GameLayer. If you are using a TileMap as a background, this includes that tilemap. The only thing that you don&#8217;t add as a child to the GameLayer is stuff that does not move with the scrolling view, such as your HUDLayer that has text that shows your characters health. Other than that, your character, the background, other characters, should all be added to the GameLayer.</p>
<p>You have to remember which objects are absolute (attached to your GameScene or other layers) versus those that are relative (a child of your GameLayer) when you set up your touch handling code. For my HUD that has buttons you can press at any time, say to pause the game, you want to add the touch handling object to your GameScene or HUDLayer class, since it doesn&#8217;t move. But if you want to be able to touch objects that scroll along with your view in the game itself, your touch handling code needs to be in an object that is a child of your GameLayer.</p>
<p>This might be a little confusing, so let&#8217;s see some code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	gameLayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>GameLayer node<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>gameScene addChild<span style="color: #002200;">:</span>gameLayer z<span style="color: #002200;">:</span>zOrder_GameLayer<span style="color: #002200;">&#93;</span>;
&nbsp;
	hudLayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>HUDLayer node<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>gameScene addChild<span style="color: #002200;">:</span>hudLayer z<span style="color: #002200;">:</span>zOrder_HudLayer<span style="color: #002200;">&#93;</span>;
&nbsp;
	tileMap <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>BGTileMap node<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>gameLayer addChild<span style="color: #002200;">:</span>tileMap z<span style="color: #002200;">:-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>gameScene addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>PauseGameButton node<span style="color: #002200;">&#93;</span> z<span style="color: #002200;">:</span>zOrder_GameButtons<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>gameLayer addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>FireGunAtTouchPoint node<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The pause game button is always on your screen, while the point at which your character fires the gun depends on how how far your view has been scrolled (by moving GameLayer).</p>
<p>Let&#8217;s see some of the code that actually moves this game layer:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setViewpointCenter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point <span style="color: #002200;">&#123;</span>
	CGPoint centerPoint <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">240</span>, <span style="color: #2400d9;">160</span><span style="color: #002200;">&#41;</span>;
	viewPoint <span style="color: #002200;">=</span> ccpSub<span style="color: #002200;">&#40;</span>centerPoint, point<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// dont scroll so far so we see anywhere outside the visible map which would show up as black bars</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>point.x <span style="color: #002200;">&amp;</span>lt; centerPoint.x<span style="color: #002200;">&#41;</span>
		viewPoint.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>point.y <span style="color: #002200;">&amp;</span>lt; centerPoint.y<span style="color: #002200;">&#41;</span>
		viewPoint.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// while zoomed out, don't adjust the viewpoint</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>isZoomedOut<span style="color: #002200;">&#41;</span>
		gameLayer.position <span style="color: #002200;">=</span> viewPoint;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When do you call that method? Well, it depends on what you want, but generally these scrolling games follow around the movement of your &#8216;main&#8217; character, right? So whatever character the you want to follow, add this to override the standard CocosNode setPosition method so you update your viewpoint whenever the character moves</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StandardGameController sharedSingleton<span style="color: #002200;">&#93;</span> setViewpointCenter<span style="color: #002200;">:</span>point<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super setPosition<span style="color: #002200;">:</span>point<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Note that the StandardGameController is a construct of mine that I use to separate the game logic out from the display code. It doesn&#8217;t matter exactly how you do it, you just need a way to have your main character object call back to something that contains a reference to GameLayer so it can adjust the position of your GameLayer.</p>
<p>Now remember, for your background to scroll properly, you need to add your background tileMap as a child of your GameLayer that is being moved around.</p>
<p>That being said, I found that an important method was missing from the cocos2d tilemap that I need to use in order to detect collisions based on the types of tiles encountered. I created a subclass of TMXTiledMap and added in these methods:</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>CGPoint<span style="color: #002200;">&#41;</span>coordinatesAtPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>point.x <span style="color: #002200;">/</span> self.tileSize.width<span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>self.mapSize.height <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>point.y <span style="color: #002200;">/</span> self.tileSize.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>getGIDAtPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>layer tileGIDAt<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self coordinatesAtPosition<span style="color: #002200;">:</span>point<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>That way it&#8217;s easy to figure out what tile any individual object is colliding with. For example, in my main character object I can have code that runs in step: function with this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	BGTileMap<span style="color: #002200;">*</span> tileMap <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>StandardGameController sharedSingleton<span style="color: #002200;">&#93;</span>.tileMap;
	CGPoint coordinate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tileMap coordinatesAtPosition<span style="color: #002200;">:</span>self.position<span style="color: #002200;">&#93;</span>;
	BBLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Right now on tile %d&quot;</span>,<span style="color: #002200;">&#91;</span>tileMap.layer tileGIDAt<span style="color: #002200;">:</span>coordinate<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Now I know what type of tile I am overlapping, and I can respond to the environment accordingly.</p>
<p>This is really all the code that you need to make a scrolling game view&#8230;I think some people overthink it and try adjusting the position of every object individually with some offset, but it&#8217;s not necessary since your objects can use relative positions with their parent.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>I have one last bit of code to add, and this is something kind of fun. It&#8217;s not complete, but at least it&#8217;s a start. What this allows you to do is &#8216;zoom out&#8217; so you can see your entire map with ALL the objects shrunk down, and then zoom back in to your character. The only tricky part is when you zoom back in, you have to slowly adjust your viewpoint in steps so the zoom in action is centered on your character, instead of jumping at the end.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define ZOOM_BACK_IN_INTERVALS 10</span>
<span style="color: #6e371a;">#define ZOOM_OUT_RATE 0.3</span>
<span style="color: #11740a; font-style: italic;">// TODO need to refine this so for each step it uses the new viewpoint</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setZoom<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>zoomedIn <span style="color: #002200;">&#123;</span>
	BBLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Zooming in %d&quot;</span>, zoomedIn<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// this scales it out so the whole height of the tilemap is in the screen</span>
	<span style="color: #a61390;">float</span> zoomScaleFactor <span style="color: #002200;">=</span> <span style="color: #2400d9;">320</span> <span style="color: #002200;">/</span> <span style="color: #002200;">&#40;</span>tileMap.mapSize.height <span style="color: #002200;">*</span> tileMap.tileSize.height<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>zoomedIn<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>gameLayer runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ScaleTo actionWithDuration<span style="color: #002200;">:</span>ZOOM_OUT_RATE scale<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>gameLayer runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>MoveTo actionWithDuration<span style="color: #002200;">:</span>ZOOM_OUT_RATE position<span style="color: #002200;">:</span>viewPoint<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self schedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>setZoomedBackIn<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> interval<span style="color: #002200;">:</span>ZOOM_OUT_RATE<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// need this for the transistion</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>gameLayer runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>ScaleTo actionWithDuration<span style="color: #002200;">:</span>ZOOM_OUT_RATE scale<span style="color: #002200;">:</span>zoomScaleFactor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>gameLayer runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>MoveTo actionWithDuration<span style="color: #002200;">:</span>ZOOM_OUT_RATE position<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		isZoomedOut <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// need this small correction at the end to account of the player is moving and the viewpoint has changed to avoid jitter</span>
<span style="color: #6e371a;">#define ZOOM_OUT_CORRECTION_RATE 0.3</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setZoomedBackIn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span>dt <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self unschedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>setZoomedBackIn<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>gameLayer runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>MoveTo actionWithDuration<span style="color: #002200;">:</span>ZOOM_OUT_CORRECTION_RATE position<span style="color: #002200;">:</span>viewPoint<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self schedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>setZoomedBackInFinished<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> interval<span style="color: #002200;">:</span>ZOOM_OUT_CORRECTION_RATE<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// need this for the transistion</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>setZoomedBackInFinished<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span>dt <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self unschedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>setZoomedBackInFinished<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	isZoomedOut <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now you see why my viewPoint variable was a class member&#8230;you&#8217;ll need it for these methods to work.</p>
<p>The reason this code isn&#8217;t complete is when it starts zooming in, it creates the actions to zoom in on the current viewpoint, but if the player is moving by the time the zoom animation is done, that viewpoint is changed and needs to &#8216;snap&#8217; to the new viewpoint. That is the reason for the setZoomedBackIn: method, which really shouldn&#8217;t have to move the gameLayer anymore. However, I haven&#8217;t yet written the code to continuously create smaller move actions to take into account a moving viewpoint as the animation continues, but doing so shouldn&#8217;t be that hard. If you want to see that bit when I finish, post in the comments and I&#8217;ll add it in.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/10/2d-scrolling-game-with-cocos2d-tilemap-with-zoom/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Using Box2D Physics Engine with Cocos2D iPhone</title>
		<link>http://johnehartzog.com/2009/07/using-box2d-physics-engine-with-cocos2d-iphone/</link>
		<comments>http://johnehartzog.com/2009/07/using-box2d-physics-engine-with-cocos2d-iphone/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 03:47:55 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[cocos2d iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=381</guid>
		<description><![CDATA[Starting work on my new project, I&#8217;ve found Box2D to be a far superior physics engine than chipmunk. It is more mature, the API more flexible, and it seems to even perform faster. However, the cocos2d code for it was &#8230; <a href="http://johnehartzog.com/2009/07/using-box2d-physics-engine-with-cocos2d-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Starting work on my new project, I&#8217;ve found Box2D to be a far superior physics engine than chipmunk. It is more mature, the API more flexible, and it seems to even perform faster. However, the cocos2d code for it was rather space, so here is a sort of helper file I had to create to make it work with my game.</p>
<p>Keep in mind this game is in progress and I&#8217;ve only been using this for a few days, so it may have issues that might pop up later on. It seems to be working very well for now though, with 40-50 objects on screen moving around with ~40 FPS.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Here is the header</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;">//  Box2DEngine.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by EricH on 7/22/09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 __MyCompanyName__. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;Box2D.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// made this an extern constant to avoid obj c lookup overhead</span>
<span style="color: #a61390;">extern</span> b2World <span style="color: #002200;">*</span>bb_world;
&nbsp;
<span style="color: #a61390;">@interface</span> Box2DEngine <span style="color: #002200;">:</span> CocosNode <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>Box2DEngine <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedSingleton;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>createWorld<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>deleteWorld;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>runSimulation;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>The .mm file</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;">//  Box2DEngine.mm</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by EricH on 7/22/09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 __MyCompanyName__. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;HookActor.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #6e371a;">#import &quot;Box2DEngine.h&quot;</span>
<span style="color: #6e371a;">#import &quot;SuperBox2DActor.h&quot;</span>
&nbsp;
b2World<span style="color: #002200;">*</span> bb_world;
&nbsp;
<span style="color: #a61390;">const</span> float32 timeStep <span style="color: #002200;">=</span> 1.0f <span style="color: #002200;">/</span> 60.0f;
<span style="color: #a61390;">const</span> int32 velocityIterations <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;
<span style="color: #a61390;">const</span> int32 positionIterations <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;
&nbsp;
<span style="color: #6e371a;">#define MAX_NUM_COLLISIONS 2048 // TODO make sure this buffer is the right size</span>
b2ContactResult contactResultCache<span style="color: #002200;">&#91;</span>MAX_NUM_COLLISIONS<span style="color: #002200;">&#93;</span>;
int32 contactResultCount <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
class MyContactListener <span style="color: #002200;">:</span> public b2ContactListener <span style="color: #002200;">&#123;</span>
public<span style="color: #002200;">:</span>
	<span style="color: #a61390;">void</span> Add<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> b2ContactPoint<span style="color: #002200;">*</span> point<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">void</span> Persist<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> b2ContactPoint<span style="color: #002200;">*</span> point<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">void</span> Remove<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> b2ContactPoint<span style="color: #002200;">*</span> point<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">void</span> Result<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> b2ContactResult<span style="color: #002200;">*</span> point<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// TODO we are making a deep copy of every contact point here</span>
		<span style="color: #11740a; font-style: italic;">// check the box2d contact masks to make sure we minimize unwanted contact results</span>
		contactResultCache<span style="color: #002200;">&#91;</span>contactResultCount<span style="color: #002200;">++</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">*</span>point; 
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #a61390;">void</span> handleCachedContactResults<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	b2ContactResult contactResult;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; contactResultCount; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
<span style="color: #6e371a;">#ifdef BBDEBUG</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>contactResultCount &gt;<span style="color: #002200;">=</span> MAX_NUM_COLLISIONS<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;RAN OUT OF BUFFER&quot;</span><span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">assert</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
<span style="color: #6e371a;">#endif</span>
		contactResult <span style="color: #002200;">=</span> contactResultCache<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>;
		SuperBox2DActor<span style="color: #002200;">*</span> actorOne <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SuperBox2DActor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contactResult.shape1<span style="color: #002200;">-</span>&gt;GetBody<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;GetUserData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
		SuperBox2DActor<span style="color: #002200;">*</span> actorTwo <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SuperBox2DActor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contactResult.shape2<span style="color: #002200;">-</span>&gt;GetBody<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;GetUserData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>actorOne.isDead <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span>actorTwo.isDead<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>actorOne collisionResultOne<span style="color: #002200;">:&amp;</span>contactResult withActor<span style="color: #002200;">:</span>actorTwo<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>actorTwo collisionResultTwo<span style="color: #002200;">:&amp;</span>contactResult withActor<span style="color: #002200;">:</span>actorOne<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// clear the collision cache</span>
	contactResultCount <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> removeDeadActors<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	b2Body<span style="color: #002200;">*</span> node <span style="color: #002200;">=</span> bb_world<span style="color: #002200;">-</span>&gt;GetBodyList<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>node<span style="color: #002200;">&#41;</span>	<span style="color: #002200;">&#123;</span>
		b2Body<span style="color: #002200;">*</span> b <span style="color: #002200;">=</span> node;
		node <span style="color: #002200;">=</span> node<span style="color: #002200;">-</span>&gt;GetNext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		SuperBox2DActor<span style="color: #002200;">*</span> actor <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SuperBox2DActor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>b<span style="color: #002200;">-</span>&gt;GetUserData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>actor.isDead<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
			<span style="color: #11740a; font-style: italic;">// remove from physics  engine</span>
			bb_world<span style="color: #002200;">-</span>&gt;DestroyBody<span style="color: #002200;">&#40;</span>b<span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #11740a; font-style: italic;">// remove from game engine (cocos2d)</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StandardGameController sharedSingleton<span style="color: #002200;">&#93;</span> removeGameActor<span style="color: #002200;">:</span>actor<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> Box2DEngine
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>Box2DEngine<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedSingleton <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> Box2DEngine<span style="color: #002200;">*</span> sharedSingleton;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>sharedSingleton<span style="color: #002200;">&#41;</span>
		sharedSingleton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Box2DEngine alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> sharedSingleton;
<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>createWorld<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size <span style="color: #002200;">&#123;</span>
	b2AABB worldAABB;
	worldAABB.lowerBound.Set<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
	worldAABB.upperBound.Set<span style="color: #002200;">&#40;</span>size.width <span style="color: #002200;">*</span> BOX2D_SCALE_FACTOR_INVERSE, size.height <span style="color: #002200;">*</span> BOX2D_SCALE_FACTOR_INVERSE<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// TODO this shouldnt be inverse?</span>
&nbsp;
	b2Vec2 gravity<span style="color: #002200;">&#40;</span>0.0f, <span style="color: #002200;">-</span>30.0f<span style="color: #002200;">&#41;</span>;
	bool doSleep <span style="color: #002200;">=</span> <span style="color: #a61390;">true</span>;
&nbsp;
	bb_world <span style="color: #002200;">=</span> new b2World<span style="color: #002200;">&#40;</span>worldAABB, gravity, doSleep<span style="color: #002200;">&#41;</span>;
	bb_world<span style="color: #002200;">-</span>&gt;SetContactListener<span style="color: #002200;">&#40;</span>new MyContactListener<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>deleteWorld <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self unschedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>step<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	delete bb_world;
	bb_world <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</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>runSimulation <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self schedule<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>step<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</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>step<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span>dt <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// step the world</span>
	bb_world<span style="color: #002200;">-</span>&gt;Step<span style="color: #002200;">&#40;</span>dt, velocityIterations, positionIterations<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// TODO do i use timestep or dt here?</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//BBLog(@&quot;Num of contact results is %d&quot;,contactResultCount);</span>
	<span style="color: #11740a; font-style: italic;">// do stuff with collisions</span>
	handleCachedContactResults<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// remove all actors that are marked as dead</span>
	removeDeadActors<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// update cocosnode positions</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>b2Body<span style="color: #002200;">*</span> b <span style="color: #002200;">=</span> bb_world<span style="color: #002200;">-</span>&gt;GetBodyList<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>; b; b <span style="color: #002200;">=</span> b<span style="color: #002200;">-</span>&gt;GetNext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>b<span style="color: #002200;">-</span>&gt;GetUserData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			SuperBox2DActor <span style="color: #002200;">*</span>actor <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SuperBox2DActor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>b<span style="color: #002200;">-</span>&gt;GetUserData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
			b2Vec2 position <span style="color: #002200;">=</span> b<span style="color: #002200;">-</span>&gt;GetPosition<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
			actor.position <span style="color: #002200;">=</span> b2toCGPoint<span style="color: #002200;">&#40;</span>position<span style="color: #002200;">&#41;</span>;
			actor.rotation <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">*</span> CC_RADIANS_TO_DEGREES<span style="color: #002200;">&#40;</span>b<span style="color: #002200;">-</span>&gt;GetAngle<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
			<span style="color: #11740a; font-style: italic;">//NSLog(@&quot;obj %@ %4.2f %4.2f\n&quot;,actor, position.x, position.y);</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/07/using-box2d-physics-engine-with-cocos2d-iphone/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Create multi-line labels with cocos 2d iphone</title>
		<link>http://johnehartzog.com/2009/06/create-multi-line-labels-with-cocos-2d-iphone/</link>
		<comments>http://johnehartzog.com/2009/06/create-multi-line-labels-with-cocos-2d-iphone/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 20:03:18 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[cocos2d iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=363</guid>
		<description><![CDATA[Update: This code is obsolete now. You can just do Label *messageLabel = &#91;Label labelWithString:message dimensions:CGSizeMake&#40;380, 120&#41; alignment:UITextAlignmentCenter fontName:@&#34;your_custom_font&#34; fontSize:26&#93;; by using the new FontManager class. For example, run this once in your app delegate when your program first loads &#8230; <a href="http://johnehartzog.com/2009/06/create-multi-line-labels-with-cocos-2d-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Update: This code is obsolete now. You can just do</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Label <span style="color: #002200;">*</span>messageLabel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Label labelWithString<span style="color: #002200;">:</span>message dimensions<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">380</span>, <span style="color: #2400d9;">120</span><span style="color: #002200;">&#41;</span> alignment<span style="color: #002200;">:</span>UITextAlignmentCenter fontName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;your_custom_font&quot;</span> fontSize<span style="color: #002200;">:</span><span style="color: #2400d9;">26</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p> by using the new FontManager class. For example, run this once in your app delegate when your program first loads</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FontManager sharedManager<span style="color: #002200;">&#93;</span> loadFont<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;your_custom_font&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>It can take a long NSString and create multiple labels without breaking up a word. I&#8217;ll eventually use this for my help screen, replacing the current 6 different 480&#215;320 png images that I load for each one <img src='http://johnehartzog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>The code is simple, but hopefully it might save somebody the time it took me to write it. I had to look up some very basic elements of ObjC here, so if there is a much easier way to do this, please let me know but don&#8217;t make too much fun of me.</p>
<p>You can easily switch out the BitmapFontAtlas for just a normal Label and it would work just fine.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setTipString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>str <span style="color: #002200;">&#123;</span>
&nbsp;
	NSInteger lineChars <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">BOOL</span> isSpace <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
	NSInteger index <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	NSInteger numLines <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>line <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithCapacity<span style="color: #002200;">:</span>LINE_LENGTH<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>index &lt;<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str length<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>index <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>str length<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			BitmapFontAtlas <span style="color: #002200;">*</span>tip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>BitmapFontAtlas bitmapFontAtlasWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span>line<span style="color: #002200;">&#93;</span> 
																																 fntFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text.fnt&quot;</span>
																															 alignment<span style="color: #002200;">:</span>UITextAlignmentLeft<span style="color: #002200;">&#93;</span> 
															retain<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>tip setPosition<span style="color: #002200;">:</span> cpv<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">30</span>,<span style="color: #2400d9;">210</span> <span style="color: #002200;">-</span> <span style="color: #2400d9;">20</span> <span style="color: #002200;">*</span> numLines<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>tip<span style="color: #002200;">&#93;</span>;	
			<span style="color: #a61390;">return</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tmp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str substringWithRange<span style="color: #002200;">:</span>NSMakeRange<span style="color: #002200;">&#40;</span>index, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>line appendString<span style="color: #002200;">:</span>tmp<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>tmp isEqual<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
			isSpace <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
		<span style="color: #a61390;">else</span>
			isSpace <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>lineChars &gt;<span style="color: #002200;">=</span> LINE_LENGTH <span style="color: #002200;">&amp;&amp;</span> isSpace<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			BitmapFontAtlas <span style="color: #002200;">*</span>tip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>BitmapFontAtlas bitmapFontAtlasWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span>line<span style="color: #002200;">&#93;</span> 
																																 fntFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text.fnt&quot;</span>
																															 alignment<span style="color: #002200;">:</span>UITextAlignmentLeft<span style="color: #002200;">&#93;</span> 
															retain<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>tip setPosition<span style="color: #002200;">:</span> cpv<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">30</span>,<span style="color: #2400d9;">210</span> <span style="color: #002200;">-</span> <span style="color: #2400d9;">20</span> <span style="color: #002200;">*</span> numLines<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>tip<span style="color: #002200;">&#93;</span>;	
			lineChars <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
			<span style="color: #002200;">&#91;</span>line setString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>;
			numLines<span style="color: #002200;">++</span>;
		<span style="color: #002200;">&#125;</span>
		lineChars<span style="color: #002200;">++</span>;
		index<span style="color: #002200;">++</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/06/create-multi-line-labels-with-cocos-2d-iphone/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Developer-Friendly AppStore</title>
		<link>http://johnehartzog.com/2009/06/the-developer-friendly-appstore/</link>
		<comments>http://johnehartzog.com/2009/06/the-developer-friendly-appstore/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 17:04:31 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Distribution]]></category>
		<category><![CDATA[StickWars]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=348</guid>
		<description><![CDATA[Lacking a better medium to communicate to all my users how difficult Apple is making it to provide them with the level of service I&#8217;d want to,  I&#8217;ll get my rant out here on my blog where it&#8217;s safe. I &#8230; <a href="http://johnehartzog.com/2009/06/the-developer-friendly-appstore/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Lacking a better medium to communicate to all my users how difficult Apple is making it to provide them with the level of service I&#8217;d want to,  I&#8217;ll get my rant out here on my blog where it&#8217;s safe.</p>
<p>I had a bugfix version of StickWars ready the same day the buggy version 1.4 was released. That day was May 27th. Is is June 18th and I&#8217;m still waiting patiently for Apple take a look at the update. It has been two weeks since I submitted a corrected update, and I haven&#8217;t got a lick of feedback back. In the mean time, my app has dropped from #3 to #9, and my users are sitting there wondering what I&#8217;m doing when in fact I have two more full-content updates ready for release, completed in the time it took Apple to approve a bugfix release. Keep in mind that my app has been in the top 10 paid apps for months now&#8211;this is one of Apple&#8217;s moneymaking apps. I would think they would want to work quickly to approve an update which would improve the game experience for hundreds of thousands of players.</p>
<p>Now I am not absolving myself of the responsibility of releasing a version with some problems. The fault for that lies solely on me, and was due to a somewhat chaotic situation as I was moving homes and starting a new full-time job that led to me not having as much time as I expected to test the release. This is no excuse, however, as I should have just rejected the update and resubmitted later after more testing. But the problem was magnified greatly by the simple fact that Apple has no agreement or desire to protect the ability of developers to have control over the applications they are producing. We can&#8217;t respond directly to user complaints or support requests posted on the AppStore. Most importantly, our update schedule is 100% subject to the whims and scheduling of Apple, so an update that we kill ourselves to complete on a rapid basis could be live in 3 days, or in 30. And we never know what to expect until after the fact.</p>
<p>Before a couple weeks ago when Apple first started emailing me to let me know that they approved my update, do you know how I would find out my update was live? My as-it-happens google alert would send me an email that a new cracked version of StickWars was available on some blog somewhere. I also rely 100% on third-party tools to tell me how people are responding to my applications. Check out <a href="http://www.appfigures.com" target="_blank">www.appfigures.com</a> to see what I&#8217;m talking about&#8211;from that site, you can read reviews from all countries. I received a lot of valueable feedback reading through the reviews of other english-speaking nations. Apple provides no way (that I know of) for developers to see the status of their application in the AppStore (if it is featured anywhere, it&#8217;s rank in other countries, etc&#8230;).</p>
<p>In addition, I&#8217;ve only received two of the seven payments for April 2009 that I should have been paid by now. Granted, one of those received was from the US, and therefore was the largest one, but they are sitting there wasting interest on a lot money and I still have not recieved a single response as to why. Three weeks ago I dug through their contact form and sent a polite email asking them why I got the financial report for a few thousands but never received the deposit, but I have yet to receive even as much as an automated reply confirming receipt of my message. I have sent more inquries weekly, and just started sending them to a more broad range of email address, and will update this at some point when I get a response.</p>
<p>Don&#8217;t get me wrong, I think Apple has done an amazing job with the AppStore, and building a system like this from scratch is bound to be difficult, with problems like this sprouting up and slowly being fixed month by month. But right now, I seem to find myself trapped at a criticial point, where if another week goes by without Apple looking at my update and StickWars drops off the top 10, and then the many more features and improvements I&#8217;ve made in the meantime won&#8217;t matter as all the exposure in the AppStore revolves around the top 10 and top 100 lists. I understand that applications have a limited shelf life at the top of the lists, but I didn&#8217;t expect mine to be cut short at the end by random and unpredictable update approval practices by the iTunes Store.</p>
<p>I believe that complaining without recommending a course of action to fix the problem is a waste of words, so here are some ideas that I would love to see put into the AppStore.</p>
<ul>
<li>A priority queue for update approval based on popularity. The apps that are getting constant updates pushed out to hundreds of thousands of players should have more personal attention from Apple staffers. On the other hand, it should be balanced so that even an unpopular app which has gone two weeks with an update in the queue is bumped up to the top priority.</li>
<li>A developer portal into iTunes Store &#8220;Insights&#8221; that gives you access to any information related to the status of your applications, including reviews and rankings (in all categories) from all nations, along with any places where the application is featured.</li>
<li>More analytic information, so I can see how many users download my Lite version first, and then download the Paid version.</li>
<li>Allow developers to post short &#8216;responses&#8217; to user reviews. This way, when somebody complains &#8220;the game was too easy wish i could make it harder!!@#&#8221; a developer who cares can go through and add a response such as &#8220;just hit the options button and set the difficulty to hard&#8221; (real example).</li>
</ul>
<p>Update: I did eventually get paid by Apple like 1 or 2 months late, and about 2 weeks after I got paid I got responses to my multiple emails (sent over a month before) saying &#8220;we show your account as paid in full&#8230;&#8221;. To be fair to Apple, my banking situation was a little complex and I understand that it takes time to go through all that, but I would have appreciated a brief response (or even an automated confirmation) to my inquiries long before almost 2 months have gone by.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/06/the-developer-friendly-appstore/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Secret to Success on the App Store Part 1</title>
		<link>http://johnehartzog.com/2009/05/secret-to-success-on-the-app-store-part-1/</link>
		<comments>http://johnehartzog.com/2009/05/secret-to-success-on-the-app-store-part-1/#comments</comments>
		<pubDate>Mon, 25 May 2009 02:12:46 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=300</guid>
		<description><![CDATA[The first tip which I can not emphasize strongly enough: listen to all your users, and respond to their concerns <a href="http://johnehartzog.com/2009/05/secret-to-success-on-the-app-store-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>First of all, the title of this post is a joke. This is by no means a secret, and it will in no way guarantee the success of your app. However, I get asked this question so much that I&#8217;m slowly going to build up this post with some tips and suggestions that may or may not be obvious, but are important enough that you can&#8217;t afford to not know them. I don&#8217;t really have time to do the full post now, but I&#8217;ll add to it over time.</p>
<p>The first tip which I can not emphasize strongly enough: listen to all your users, and respond to their concerns (if they are legitimate). I know this is a major shortfall of the AppStore, in that it is tough to go back and forth with your users. Developers can not add a comment to a review? Seriously? A third of the one star reviews on StickWars are people too dumb to press the giant options button before complaining about &#8216;not being able to turn off the music&#8217; or some other issue I fixed long ago. A one sentence reply by me would help them, and they would raise the review of my game, helping me.</p>
<p>That being said, you must create your own forums for users to ask you questions and receive answers. And no, don&#8217;t you dare add in a generic &#8216;contact us&#8217; form and call that your support feedback. You are not some spineless, monolithic company who can afford to treat their customers like crap because you are too big to have any real competitors. Create a forum where people can post questions and you can answer them, and other people can see those answers without having to ask themselves. For every person who asked a question, there are probably 10 more who typed the same thing into google looking for an answer but was too lazy to ask the question themselves.</p>
<p>Best of all, these forums can be a valuable tool to get ideas how to improve your product. I&#8217;m not exaggerating when I say that 70% of the enhancements I push out with every update started as user suggestions.It&#8217;s not that I do what every user asks, but I consider every suggestion even if I initially considered the issue and decided against it. For many of the features in StickWars, there were features I really did not want to add, but many people asked for them, and after I took the trouble to add them in, the response was overwhelmingly positive. If you didn&#8217;t get the important point so far, I&#8217;ll keep it simple&#8230;<strong>listen to your customers</strong>. Separate the stupid input (example: &#8216;hey can you plz ad zombies, ufos, monkies, nukes, rifles, cows&#8230;.&#8217;) from legitimate feedback and suggestions and weigh all of it carefully when deciding what to add to your application.</p>
<p>I&#8217;ve read every single review posted on the AppStore about StickWars and StickWars Lite, for all versions. In case you don&#8217;t realize how many there are, right now the number stands at about 2000+ combined. I&#8217;ve found that iTunes does some really bizarre caching of them, so I use my iPhone to read reviews each day until I read one I recognize. It&#8217;s not the best system, but it works. I don&#8217;t know whether or not most developers already do this, but either way, you should. Oftentimes I&#8217;d read 100 retarded reviews to find one guy who had a truly original and easy-to-add idea to add to StickWars. Twenty-five minutes later, I finish adding this feature that I later get hundreds of &#8216;awesome idea!&#8217; reviews for. Seriously, this type of stuff happens, and with all these people offering feedback (even if 95% is ridiculous), you need to read all of it.</p>
<p>I&#8217;ve started using a fantastic service from www.appfigures.com that allows you to read reviews of your application from every worldwide app store. I went through and read every non-US review that was in english, and found for some bizzare reason, like 30 people all outside of the US asked where to find a link to my forums. Nobody in the US had ever asked this question, but I realized it might be a good idea to manually throw it in there (as the generic iTunes &#8220;StickWars Support&#8221; link isn&#8217;t really informative to the user).</p>
<p>That&#8217;s it for now&#8230;later on I&#8217;ll be adding some more comments about some of my App Store lessons learned.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/05/secret-to-success-on-the-app-store-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>StickWars Drops to #2 After 12 Days as Top Paid App</title>
		<link>http://johnehartzog.com/2009/05/stickwars-drops-to-2-after-12-days-as-top-paid-app/</link>
		<comments>http://johnehartzog.com/2009/05/stickwars-drops-to-2-after-12-days-as-top-paid-app/#comments</comments>
		<pubDate>Mon, 04 May 2009 20:49:37 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[iPhone Development]]></category>
		<category><![CDATA[StickWars]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=270</guid>
		<description><![CDATA[After 12 days in the top spot, StickWars has dropped to #2 in the overall paid app rankings. However, the 1.2 update was sent to Apple last Wednesday and should be live this week, and I&#8217;m expecting a great user &#8230; <a href="http://johnehartzog.com/2009/05/stickwars-drops-to-2-after-12-days-as-top-paid-app/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After 12 days in the top spot, StickWars has dropped to #2 in the overall paid app rankings. However, the 1.2 update was sent to Apple last Wednesday and should be live this week, and I&#8217;m expecting a great user response. Hopefully it will get bumped back up to #1!</p>
<p>In any case, it is still the #1 game, and I&#8217;m still working around the clock on bug fixes and enhancements. I just completed a massive graphics overhaul that ended up with the graphics look exactly the same (with maybe a few compression artifacts), but the game now runs around 20% faster. I went from using 160 separate png files for all the animations to 8 atlas sheets in the compressed pvr format. Now I can work on adding some cool fire particle-effects, since I can spare the processor usage needed.</p>
<p>I just finished my finals at college and am graduating in a few days, so StickWars is now getting all of the attention it deserves. There are some really cool new features coming, some ideas that are new to the genre but I think will really make it fun. I&#8217;ll give a hint: if you like the idea behind &#8216;Bowman&#8217;, you&#8217;ll love the new wizard spell <img src='http://johnehartzog.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> .</p>
<p>Also, after realizing that like 80% of the google searches for StickWars that land on my website contain the terms &#8220;cheat codes&#8221; I&#8217;m going to satisfy those people who don&#8217;t have the patience to put in the time to get to level 30 before trying out all the cool powerups. This will be a surprise, with hints posted on my forums.</p>
<p>All these new  improvements will be included in version 1.3.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/05/stickwars-drops-to-2-after-12-days-as-top-paid-app/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Using Cocos2D AtlasSpriteManager</title>
		<link>http://johnehartzog.com/2009/05/using-cocos2d-atlasspritemanager/</link>
		<comments>http://johnehartzog.com/2009/05/using-cocos2d-atlasspritemanager/#comments</comments>
		<pubDate>Sat, 02 May 2009 16:42:00 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[cocos2d iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=262</guid>
		<description><![CDATA[After having a lot of trouble dealing with the issue myself and seeing others having trouble on the cocos2d discussions, I&#8217;m going to go ahead and post my solution which so far is working pretty well for me. Please take &#8230; <a href="http://johnehartzog.com/2009/05/using-cocos2d-atlasspritemanager/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
After having a lot of trouble dealing with the issue myself and seeing others having trouble on the cocos2d <a href="http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/f4897c6cc56a083f?hl=en" target="_blank">discussions</a>, I&#8217;m going to go ahead and post my solution which so far is working pretty well for me. Please take into account I&#8217;m new with Objective C when looking this over.</p>
<p>The problem is each AtlastSpriteManager can 1) refer only to a single sheet image, and 2) each AtlasSprite can only be a child of at AtlasSpriteManager. So what happens if you a few game characters that you want to share certain animations, but not all?</p>
<p>I took the super-class of all my game Sprites, changed its type from a Sprite (or AtlasSpriteManager, which I tried but failed to well) to a generic CocosNode. I added in this to the header. Note that I only needed my Sprites to switch between two different sprite sheets as most.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> Touchable <span style="color: #002200;">:</span> CocosNode &lt;Collideable&gt; <span style="color: #002200;">&#123;</span>
	AtlasSprite <span style="color: #002200;">*</span>activeSprite;
	AtlasSprite <span style="color: #002200;">*</span>sprite1;
	AtlasSprite <span style="color: #002200;">*</span>sprite2;
...
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>sizeOne two<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>sizeTwo;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager1;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager2;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSprite<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>newstate;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setActiveSpriteOne;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setActiveSpriteTwo;
...</pre></div></div>

<p>To the implementation file&#8230;</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>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager1 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager2 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>sizeOne two<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>sizeTwo <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>
		sprite1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AtlasSprite spriteWithRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>sizeOne.width, sizeOne.height, sizeOne.width, sizeOne.height<span style="color: #002200;">&#41;</span> 
														spriteManager<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>self spriteManager1<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self spriteManager1<span style="color: #002200;">&#93;</span> addChild<span style="color: #002200;">:</span>sprite1<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><span style="color: #002200;">&#91;</span>self spriteManager1<span style="color: #002200;">&#93;</span> parent<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>LevelController sharedSingleton<span style="color: #002200;">&#93;</span> gameLayer<span style="color: #002200;">&#93;</span> addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self spriteManager1<span style="color: #002200;">&#93;</span> z<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>sprite1 setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		sprite2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AtlasSprite spriteWithRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>sizeTwo.width, sizeTwo.height, sizeTwo.width, sizeTwo.height<span style="color: #002200;">&#41;</span> 
														spriteManager<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>self spriteManager2<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self spriteManager2<span style="color: #002200;">&#93;</span> addChild<span style="color: #002200;">:</span>sprite2<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><span style="color: #002200;">&#91;</span>self spriteManager2<span style="color: #002200;">&#93;</span> parent<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>LevelControler sharedSingleton<span style="color: #002200;">&#93;</span> gameLayer<span style="color: #002200;">&#93;</span> addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self spriteManager2<span style="color: #002200;">&#93;</span> z<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>sprite2 setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
&nbsp;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSprite<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>newstate <span style="color: #002200;">&#123;</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>setActiveSpriteOne <span style="color: #002200;">&#123;</span>
	activeSprite <span style="color: #002200;">=</span> sprite1;
	<span style="color: #002200;">&#91;</span>sprite2 setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>sprite1 setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</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>setActiveSpriteTwo<span style="color: #002200;">&#123;</span> 
	activeSprite <span style="color: #002200;">=</span> sprite2;
	<span style="color: #002200;">&#91;</span>sprite2 setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>sprite1 setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
A few methods of my superclass &#8216;Sprite&#8217; object that I modify slightly to take changes into account. You can no longer want to run the actions (which include animations) on yourself, but on the active sprite.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setState<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">enum</span> ActionFigureState<span style="color: #002200;">&#41;</span>newstate<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>activeSprite stopAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>stateActions objectAtIndex<span style="color: #002200;">:</span>state<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setSprite<span style="color: #002200;">:</span>newstate<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>activeSprite runAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>stateActions objectAtIndex<span style="color: #002200;">:</span>newstate<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	state <span style="color: #002200;">=</span> newstate;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>pos <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super setPosition<span style="color: #002200;">:</span>pos<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>activeSprite setPosition<span style="color: #002200;">:</span>pos<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So what we have now is a superclass that is calling a instance method [self spriteManager1] in order to manage attach its two different sprites. Notice that no animation specific code is here in the superclass, except for the fact that a single &#8216;Sprite&#8217; can work with 2 AtlasSheetManagers at most. You could easily take away this restriction, but I had no need to complicate things.</p>
<p>This gets helpful when I get into a child class that actually knows how to draw itself. Here is my &#8220;SmallStick.m&#8221; which has an empty header file except for being a child of Touchable. This implementation code also contains all the this object&#8217;s interaction with the game, such as collision detection, how to handle if it is touched, and how to add itself to the Chipmunk space.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define width1 79</span>
<span style="color: #6e371a;">#define height1 68</span>
<span style="color: #6e371a;">#define columns1 7</span>
&nbsp;
<span style="color: #6e371a;">#define width2 79</span>
<span style="color: #6e371a;">#define height2 68</span>
<span style="color: #6e371a;">#define columns2 7</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> SmallStick
&nbsp;
<span style="color: #a61390;">static</span> AtlasSpriteManager <span style="color: #002200;">*</span>spriteManager1 <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #a61390;">static</span> AtlasSpriteManager <span style="color: #002200;">*</span>spriteManager2 <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager1 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>spriteManager1<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> <span style="color: #11740a; font-style: italic;">// I dont actually know if synchronized is needed here</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>spriteManager1<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>Texture2D saveTexParameters<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>Texture2D setAliasTexParameters<span style="color: #002200;">&#93;</span>;
			spriteManager1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AtlasSpriteManager alloc<span style="color: #002200;">&#93;</span> initWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;smallstick_common.png&quot;</span> capacity<span style="color: #002200;">:</span><span style="color: #2400d9;">50</span><span style="color: #002200;">&#93;</span>;	
			<span style="color: #002200;">&#91;</span>Texture2D restoreTexParameters<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">return</span> spriteManager1;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager2 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>spriteManager2<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>spriteManager2<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>Texture2D saveTexParameters<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>Texture2D setAliasTexParameters<span style="color: #002200;">&#93;</span>;
			spriteManager2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AtlasSpriteManager alloc<span style="color: #002200;">&#93;</span> initWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;smallstick_weaponless.png&quot;</span> capacity<span style="color: #002200;">:</span><span style="color: #2400d9;">50</span><span style="color: #002200;">&#93;</span>;	
			<span style="color: #002200;">&#91;</span>Texture2D restoreTexParameters<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">return</span> spriteManager2;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager1 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>SmallStick spriteManager1<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>AtlasSpriteManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spriteManager2 <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>SmallStick spriteManager2<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init <span style="color: #002200;">&#123;</span>
	self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span>width1, height1<span style="color: #002200;">&#41;</span> two<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span>width2, height2<span style="color: #002200;">&#41;</span><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>
          ...
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setSprite<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>newstate <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">switch</span>	<span style="color: #002200;">&#40;</span>newstate<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">case</span> fState_charging<span style="color: #002200;">:</span>
		<span style="color: #a61390;">case</span> fState_attacking<span style="color: #002200;">:</span>
		<span style="color: #a61390;">case</span> fState_recovering<span style="color: #002200;">:</span>
			<span style="color: #002200;">&#91;</span>self setActiveSpriteTwo<span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">break</span>;
&nbsp;
		<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #002200;">&#91;</span>self setActiveSpriteOne<span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">break</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadActions <span style="color: #002200;">&#123;</span>
	AtlasAnimation <span style="color: #002200;">*</span>chargeA <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>AtlasAnimation animationWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;weaponless_charge&quot;</span> delay<span style="color: #002200;">:</span>0.05f<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;">0</span>;i&lt;<span style="color: #2400d9;">18</span>;i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">int</span> x<span style="color: #002200;">=</span> i <span style="color: #002200;">%</span> columns2;
		<span style="color: #a61390;">int</span> y<span style="color: #002200;">=</span> i <span style="color: #002200;">/</span> columns2;
		<span style="color: #002200;">&#91;</span>chargeA addFrameWithRect<span style="color: #002200;">:</span> CGRectMake<span style="color: #002200;">&#40;</span>x<span style="color: #002200;">*</span>width2, y<span style="color: #002200;">*</span>height2, width2, height2<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	Action <span style="color: #002200;">*</span>charge <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>RepeatForever actionWithAction<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>Animate actionWithAnimation<span style="color: #002200;">:</span> chargeA<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        ...
&nbsp;
	stateActions <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
									charge, dying, ..., <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The part I like about this is when I want to reuse the &#8216;smallstick_common.png&#8217; atlas sheet I can just not override the class method +(AtlasSpriteManager *)spriteManager2 in my child classes.</p>
<p>Don&#8217;t forget to remove the sprite from the AtlasManager when you&#8217;re done.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self spriteManager1<span style="color: #002200;">&#93;</span> removeChild<span style="color: #002200;">:</span>sprite1 cleanup<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self spriteManager2<span style="color: #002200;">&#93;</span> removeChild<span style="color: #002200;">:</span>sprite2 cleanup<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>First of all, I hope this doesn&#8217;t have some hidden downside. As I said, I&#8217;m new to the platform. But I hope somebody finds this useful.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-1151609318119170";
/* 468x60, created 3/31/10 */
google_ad_slot = "7808045430";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/05/using-cocos2d-atlasspritemanager/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Integrating Chipmunk into Objective C iPhone Games</title>
		<link>http://johnehartzog.com/2009/04/integrating-chipmunk-into-objective-c/</link>
		<comments>http://johnehartzog.com/2009/04/integrating-chipmunk-into-objective-c/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 17:42:37 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[cocos2d iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=177</guid>
		<description><![CDATA[When working with Cocos 2d iPhone, a lot of the sample code provided that shows how to hook into the Chipmunk physics engine using static functions outside of any Objective C object. While this is the most straightforward way to &#8230; <a href="http://johnehartzog.com/2009/04/integrating-chipmunk-into-objective-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When working with Cocos 2d iPhone, a lot of the sample code provided that shows how to hook into the Chipmunk physics engine using static functions outside of any Objective C object. While this is the most straightforward way to get the engine up and running, it restricts what you can do later on.</p>
<p>Instead, you need to create a static function which takes two parameters, the object being updated and the objects container. Here&#8217;s some code:</p>
<p>This is the static function which goes outside your class implementation. Notice that we don&#8217;t actually do any of the object updates here. This is good, because we can&#8217;t access any of the internal state in the container in this method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> updateEachShapeCallback<span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>ptr, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>parent<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	GameController <span style="color: #002200;">*</span>parentObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GameController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parent;
	<span style="color: #002200;">&#91;</span>parentObject updateShape<span style="color: #002200;">:</span> ptr<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Note that my container, called GameController, is a CocosNode so when were ready to start the game we can call</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self schedule<span style="color: #002200;">:</span> <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>step<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>to update the shapes.<br />
This goes inside the implementation of your container.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> step<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;">int</span> steps <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
    cpFloat dt <span style="color: #002200;">=</span> delta<span style="color: #002200;">/</span><span style="color: #002200;">&#40;</span>cpFloat<span style="color: #002200;">&#41;</span>steps;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; iactiveShapes, <span style="color: #002200;">&amp;</span>amp;updateEachShapeCallback, self<span style="color: #002200;">&#41;</span>;
    cpSpaceHashEach<span style="color: #002200;">&#40;</span>space<span style="color: #002200;">-&amp;</span>gt;staticShapes, <span style="color: #002200;">&amp;</span>amp;updateEachShapeCallback, self<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Add in the updateShape:(void *)ptr method to your GameController interface file and then implement as</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> updateShape<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ptr <span style="color: #002200;">&#123;</span>
	cpShape <span style="color: #002200;">*</span>shape <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>cpShape<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> ptr;
	Touchable <span style="color: #002200;">*</span>obj <span style="color: #002200;">=</span> shape<span style="color: #002200;">-&amp;</span>gt;data;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>obj<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		cpBody <span style="color: #002200;">*</span>body <span style="color: #002200;">=</span> shape<span style="color: #002200;">-&amp;</span>gt;body;
		<span style="color: #002200;">&#91;</span>obj setPosition<span style="color: #002200;">:</span> cpv<span style="color: #002200;">&#40;</span> body<span style="color: #002200;">-&amp;</span>gt;p.x, body<span style="color: #002200;">-&amp;</span>gt;p.y<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>obj setRotation<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> CC_RADIANS_TO_DEGREES<span style="color: #002200;">&#40;</span> <span style="color: #002200;">-</span>body<span style="color: #002200;">-&amp;</span>gt;a <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// do whatever else you want here! you have all the the state you should need!</span>
		<span style="color: #11740a; font-style: italic;">// just avoiding creating objects anywhere from here as that will slow your game down</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>I adopted this solution from instructions I found at <a href="http://www.eisbox.net/2008/10/11/375-using-native-objective-c-methods-for-c-callbacks">Using native Objective-C methods for C Callbacks</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/04/integrating-chipmunk-into-objective-c/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Unofficial App Store Rejection Criteria</title>
		<link>http://johnehartzog.com/2009/04/unofficial-app-store-rejection-criteria/</link>
		<comments>http://johnehartzog.com/2009/04/unofficial-app-store-rejection-criteria/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 16:36:37 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Distribution]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://johnehartzog.com/?p=175</guid>
		<description><![CDATA[I found this post to be very informative, and possibly helped to prevent my App from being rejected the first round (I had enabled constant vibration while the player was under attack).]]></description>
			<content:encoded><![CDATA[<p>I found <a href="http://10base-t.com/unofficial-appstore-rejection-criteria/">this post</a> to be very informative, and possibly helped to prevent my App from being rejected the first round (I had enabled constant vibration while the player was under attack).</p>
]]></content:encoded>
			<wfw:commentRss>http://johnehartzog.com/2009/04/unofficial-app-store-rejection-criteria/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

