<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Ray Wenderlich</title>
	
	<link>http://www.raywenderlich.com</link>
	<description>Tutorials for iPhone / iOS Developers and Gamers</description>
	<lastBuildDate>Fri, 03 Feb 2012 15:00:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RayWenderlich" /><feedburner:info uri="raywenderlich" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>How To Build a Monkey Jump Game Using Cocos2D, PhysicsEditor &amp; TexturePacker Part 2</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/rvH6a_U2y3I/how-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2</link>
		<comments>http://www.raywenderlich.com/7884/how-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2#comments</comments>
		<pubDate>Fri, 03 Feb 2012 15:00:15 +0000</pubDate>
		<dc:creator>aloew</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[box2D]]></category>
		<category><![CDATA[cocos2D]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=7884</guid>
		<description><![CDATA[TweetThis is a post by special contributor Andreas Loew, the creator of TexturePacker and PhysicsEditor. Welcome back to the Monkey [...]<p><a href="http://www.raywenderlich.com/7884/how-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2">How To Build a Monkey Jump Game Using Cocos2D, PhysicsEditor &#038; TexturePacker Part 2</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton7884" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FzM6vy5&amp;via=rwenderlich&amp;text=How%20To%20Build%20a%20Monkey%20Jump%20Game%20Using%20Cocos2D%2C%20PhysicsEditor%20%26%23038%3B%20TexturePacker%20Part%202&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7884%2Fhow-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><i>This is a post by special contributor <a href="http://twitter.com/CodeAndWeb">Andreas Loew</a>, the creator of TexturePacker and PhysicsEditor.</i></p>
<div id="attachment_9722" class="wp-caption alignright" style="width: 260px"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/02/MonkeyJumpIntro.png" alt="Create this vertical scrolling platformer with Cocos2D!" title="Create this vertical scrolling platformer with Cocos2D!" width="250" height="250" class="size-full wp-image-9722" /><p class="wp-caption-text">Create this vertical scrolling platformer with Cocos2D!</p></div>
<p>Welcome back to the Monkey Jump tutorial!  In this series, we are creating a fun vertical scrolling platformer with Cocos2D, <a href=“http://www.texturepacker.com/”>TexturePacker</a> and <a href="http://www.physicseditor.de">PhysicsEditor</a>. </p>
<p>In <a href="http://www.raywenderlich.com/7261/monkey-jump">Part One</a> on the tutorial series, we introduced the MonkeyJump! game design, created the sprite sheets and shapes we needed, and began coding the game.</p>
<p>Before we stopped for a break, we had all of our game layers set up and had just finished making random objects drop from the sky, with sound effects.</p>
<p>In this second part of the tutorial, we will add our hero to the game, make him move and jump, and start adding some gameplay.</p>
<p>We&#8217;ll be starting with the project where we left off last time.  If you don&#8217;t have it already, grab the <a href="http://www.raywenderlich.com/downloads/monkeyjump.zip">source code</a> for this tutorial series and open up 3-DraggingObjects.</p>
<p>Without further ado, let&#8217;s get back to (monkey) business!  :]</p>
<p><span id="more-7884"></span></p>
<h2>Getting Started</h2>
<p>We created the monkey&#8217;s shape in PhysicsEditor in <a href="http://www.raywenderlich.com/7261/monkey-jump">Part One</a> of this tutorial, but haven&#8217;t added the monkey to the game yet.  Let&#8217;s do that now!</p>
<p>Add the <em>Monkey</em> class by creating a new file with the iOS\Cocoa Touch\Objective-C class template. Name the class Monkey, and make it a subclass of GB2Sprite. (Again, remember to change extension for the Monkey.m file to .mm)</p>
<p>The monkey will react to different events in the game world: he&#8217;ll put up his hands when something drops from above, push items, jump, etc. This is why you won&#8217;t be using Cocos2d&#8217;s standard animation routines, and instead implement some of your own.</p>
<p>For this, you need some member variables to store the additional data. Paste this code into <em>Monkey.h</em> replacing what&#8217;s there already:</p>

<div class="wp_codebox"><table><tr id="p788435"><td class="code" id="p7884code35"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#pragma once</span>
&nbsp;
    <span style="color: #6e371a;">#import &quot;Cocos2d.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GB2Sprite.h&quot;</span>
&nbsp;
    <span style="color: #a61390;">@class</span> GameLayer;
&nbsp;
    <span style="color: #a61390;">@interface</span> Monkey <span style="color: #002200;">:</span> GB2Sprite
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">float</span> direction;      <span style="color: #11740a; font-style: italic;">// keeps monkey's direction (from accelerometer)</span>
        <span style="color: #a61390;">int</span> animPhase;        <span style="color: #11740a; font-style: italic;">// the current animation phase</span>
        ccTime animDelay;     <span style="color: #11740a; font-style: italic;">// delay until the next animation phase is stated</span>
        GameLayer <span style="color: #002200;">*</span>gameLayer; <span style="color: #11740a; font-style: italic;">// weak reference</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> initWithGameLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameLayer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gl;
    <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> walk<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>direction;
&nbsp;
    <span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Now switch to <em>Monkey.mm</em> and replace it with the following lines:</p>

<div class="wp_codebox"><table><tr id="p788436"><td class="code" id="p7884code36"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;Monkey.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GB2Contact.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GMath.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;Object.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;SimpleAudioEngine.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GameLayer.h&quot;</span>
&nbsp;
    <span style="color: #6e371a;">#define JUMP_IMPULSE 6.0f</span>
    <span style="color: #6e371a;">#define WALK_FACTOR 3.0f</span>
    <span style="color: #6e371a;">#define MAX_WALK_IMPULSE 0.2f</span>
    <span style="color: #6e371a;">#define ANIM_SPEED 0.3f</span>
    <span style="color: #6e371a;">#define MAX_VX 2.0f</span>
&nbsp;
    <span style="color: #a61390;">@implementation</span> Monkey
&nbsp;
    <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithGameLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GameLayer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gl
    <span style="color: #002200;">&#123;</span>   
        <span style="color: #11740a; font-style: italic;">// 1 - Initialize the monkey </span>
        self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithDynamicBody<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey&quot;</span>
                    spriteFrameName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/idle/1.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <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: #11740a; font-style: italic;">// 2 - Do not let the monkey rotate</span>
            <span style="color: #002200;">&#91;</span>self setFixedRotation<span style="color: #002200;">:</span><span style="color: #a61390;">true</span><span style="color: #002200;">&#93;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">// 3 - The monkey uses continuous collision detection</span>
            <span style="color: #11740a; font-style: italic;">// to avoid getting stuck inside fast-falling objects</span>
            <span style="color: #002200;">&#91;</span>self setBullet<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
           <span style="color: #11740a; font-style: italic;">// 4 - Store the game layer</span>
            gameLayer <span style="color: #002200;">=</span> gl;
        <span style="color: #002200;">&#125;</span>
&nbsp;
        <span style="color: #a61390;">return</span> self;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Let&#8217;s go through the <em>initWithGameLayer</em> method step-by-step:</p>
<ol>
<li>First, we initialize the monkey. The monkey&#8217;s movement will be affected by the physics engine, so make him a dynamic object. We&#8217;ll use the idle frame as both the first frame of the monkey animation and the monkey&#8217;s physics shape.</li>
<li>The monkey should stand straight up all the time, so we set his rotation to fixed. This means the monkey is moved by Box2d, but does not rotate or tilt.</li>
<li>Set the monkey to bullet mode. Bullet mode enables continuous collision detection on an object. Without it, Box2d moves objects and then performs the collision checks. With fast-moving objects it&#8217;s possible that an object will pass through another without any collision detection at all or that an object will get stuck in another. Continuous collision detection calculates collisions all the way from an object&#8217;s current position to its new position &#8211; not just for the end point.</li>
<li>Finally, you need to store the game layer – keep it as a weak reference and just assign the value.</li>
</ol>
<div id="attachment_8206" class="wp-caption alignnone" style="width: 349px"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/MonkeyBullet.png" alt="A monkey bullet!  Bullet mode enables continuous collision detection." title="A monkey bullet!  Bullet mode enables continuous collision detection." width="339" height="226" class="size-full wp-image-8206" /><p class="wp-caption-text">A monkey bullet!  Bullet mode enables continuous collision detection.</p></div>
<div style="clear:both"></div>
<p>With regards to bullet mode for step #3, if you were coding a project with only a few objects, you could set the Box2d engine to run continuous collision detection for all game objects. However, when a game has a lot of objects that would add a lot of CPU overhead. So, for our game we&#8217;ll set the continuous mode on just the monkey and fast-moving (dropping) objects.</p>
<p>To enable the monkey, add him to the GameLayer. Open <em>GameLayer.h</em> and add the following lines at the top, just below the import statements:</p>

<div class="wp_codebox"><table><tr id="p788437"><td class="code" id="p7884code37"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">@class</span> Monkey;</pre></td></tr></table></div>

<p>Now, add the following member variable to the GameLayer class:</p>

<div class="wp_codebox"><table><tr id="p788438"><td class="code" id="p7884code38"><pre class="objc" style="font-family:monospace;">    Monkey <span style="color: #002200;">*</span>monkey;</pre></td></tr></table></div>

<p>Then switch to <em>GameLayer.mm</em> and import Monkey.h at the top of the file:</p>

<div class="wp_codebox"><table><tr id="p788439"><td class="code" id="p7884code39"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;Monkey.h&quot;</span></pre></td></tr></table></div>

<p>At the end of the <em>init</em> selector in <em>GameLayer.mm</em>, initialize the monkey, add him to the game layer and set a starting position for the monkey:</p>

<div class="wp_codebox"><table><tr id="p788440"><td class="code" id="p7884code40"><pre class="objc" style="font-family:monospace;">    monkey <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Monkey alloc<span style="color: #002200;">&#93;</span> initWithGameLayer<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>objectLayer addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>monkey ccNode<span style="color: #002200;">&#93;</span> z<span style="color: #002200;">:</span><span style="color: #2400d9;">10000</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>monkey setPhysicsPosition<span style="color: #002200;">:</span>b2Vec2FromCC<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">240</span>,<span style="color: #2400d9;">150</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Compile and run, and you&#8217;ll see the following:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/mj2-01.png" alt="" title="mj2-01" width="700" height="372" class="aligncenter size-full wp-image-8188" style="background:none"/></p>
<p>The monkey is in the house! Objects drop onto him and he gets pushed away – perfect. That&#8217;s exactly what we want.</p>
<h2>Do the Monkey Walk</h2>
<p>Our next goal is to make the monkey walk, using the accelerometer as input.</p>
<p>Go back to <em>GameLayer.mm</em> and add the following code at the end of the init method:</p>

<div class="wp_codebox"><table><tr id="p788441"><td class="code" id="p7884code41"><pre class="objc" style="font-family:monospace;">    self.isAccelerometerEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</pre></td></tr></table></div>

<p>This will ensure that for each change in the built-in accelerometer values, the GameLayer class gets an automatic notification. The notification handler has to be added to <em>GameLayer.mm</em> at the end of the file, before the @end marker:</p>

<div class="wp_codebox"><table><tr id="p788442"><td class="code" id="p7884code42"><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>accelerometer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAccelerometer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>accelerometer 
            didAccelerate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAcceleration<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>acceleration
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// forward accelerometer value to monkey</span>
        <span style="color: #002200;">&#91;</span>monkey walk<span style="color: #002200;">:</span>acceleration.y<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The accelerometer handler calls the walk method of the monkey object with the y-axis value of the accelerometer. This method will handle actually moving the monkey back and forth based on the accelerometer input.</p>
<p>So, move to <em>Monkey.mm</em> and add the walk method to the end of the file (before the @end marker). This method simply stores the new movement direction for the monkey in a member variable.</p>

<div class="wp_codebox"><table><tr id="p788443"><td class="code" id="p7884code43"><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> walk<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>newDirection
    <span style="color: #002200;">&#123;</span>
        direction <span style="color: #002200;">=</span> newDirection;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Try compiling and running the code now … Surprise! Nothing new happens. This is due to the fact that while the direction value has been stored, it has not been applied to the physics simulation yet. In order to update the physics simulation based on the new movement direction, we need to override the updateCCFromPhysics selector, which is called by the GB2Engine on every frame for a GB2Node object to update the physics.</p>
<p><em>Update Monkey Physics</em></p>
<p>Add the following code to <em>Monkey.mm</em>:</p>

<div class="wp_codebox"><table><tr id="p788444"><td class="code" id="p7884code44"><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> updateCCFromPhysics
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// 1- Call the super class</span>
        <span style="color: #002200;">&#91;</span>super updateCCFromPhysics<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// 2 - Apply the directional impulse</span>
        <span style="color: #a61390;">float</span> impulse <span style="color: #002200;">=</span> clamp<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>direction<span style="color: #002200;">*</span>WALK_FACTOR, 
                              <span style="color: #002200;">-</span>MAX_WALK_IMPULSE, 
                              MAX_WALK_IMPULSE<span style="color: #002200;">&#41;</span>;            
        <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:-</span>b2Vec2<span style="color: #002200;">&#40;</span>impulse,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;        
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the above, you first call the selector of the super class. This will update the monkey&#8217;s sprite based on physics simulation.</li>
<p>Then, you push the monkey in the right direction based on the stored direction value. It is important not to take complete control of the monkey. Otherwise, his &#8220;natural&#8221; behavior in response to events such as items dropping or collision detection won&#8217;t work properly.</p>
<p>All you really do is to give the monkey a nudge in the right direction. Since the physics engine updates happen 60 times per second, it&#8217;s important to keep the push quite light. It&#8217;s a monkey – not a bullet, even if he is in bullet mode!</p>
<p>You can move a box2D object by applying an impulse to the object. And you do this using the GB2Sprite&#8217;s applyLinearImpulse method, which takes two parameters: the impulse to apply and the point of application.</p>
<p>For the point of application, we&#8217;ll use the world center of the object. Applied at the world center, the object will be pushed without any torque that would result into a rotation. (Which, by the way, would not happen to the monkey anyway, since we already set him not to rotate.)</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/02/ApplyLinearImpulse-480x246.png" alt="" title="ApplyLinearImpulse" width="480" height="246" class="size-medium wp-image-9669" /></p>
<div style="clear:both"></div>
<p>When applying an impulse, I recommend using the mass of the object, which you can get with [self mass]. This is because the impulse is the product of mass and velocity. </p>
<p>Multiply the mass value by the stored direction value. This gives you small impulses when the device is tilted slightly, and bigger impulses when its tilted sharply. </p>
<p>Scaling the impulse with the object&#8217;s mass frees us from having to worry about the movement changing when we change the object&#8217;s shape in PhysicsEditor. If we didn&#8217;t scale the impulse and later we made the monkey a bit smaller (for example), the same impulse applied to a monkey of less mass will result in a faster-moving monkey.</p>
<p>We will also clamp the value to a maximum to avoid impulses that are too strong. The maximum impulse is defined with the MAX_WALK_IMPULSE variable.</p>
<p>Compile and run. Still nothing? Ah, I forgot to tell you one thing: the iPhone simulator does not simulate the accelerometer. So, from now on, we need to test on the device! Switch to the device and test.</p>
<p>The monkey now slides left and right – but the movement doesn&#8217;t look very natural.</p>
<p><em>Make the Monkey Move</em></p>
<p>We&#8217;re going to add some code to <em>Monkey.mm</em> to get the monkey animations working. Add the following to the end of the <em>updateCCFromPhysics</em> method:</p>

<div class="wp_codebox"><table><tr id="p788445"><td class="code" id="p7884code45"><pre class="objc" style="font-family:monospace;">    animDelay <span style="color: #002200;">-=</span> 1.0f<span style="color: #002200;">/</span>60.0f;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>animDelay &lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        animDelay <span style="color: #002200;">=</span> ANIM_SPEED;
        animPhase<span style="color: #002200;">++</span>;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>animPhase &gt; <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            animPhase <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The first line simply updates the time till the next animation by decreasing the time delay till the next animation phase. I use the value 1.0f/60.0f because I assume that the application runs at 60 fps and the updateCCFromPhysics method does not have a delta time parameter which would provide the timer interval between each update accurately. </p>
<p>If the animation time delay drops below zero, reset the animation delay value to the animation speed and increase the current phase by one. If the highest phase is reached, loop back to 1 so that the animation will continue to play in a loop.</p>
<p>Next, we need to determine the direction the monkey is facing. There are two ways to do this:</p>
<ol>
<li>Use the direction from the accelerometer</li>
<li>Use the monkey&#8217;s velocity vector</li>
</ol>
<p>I prefer to use the accelerometer, since it gives the player immediate feedback when he or she tries to change the direction by tilting the device. We&#8217;ll respond to velocity changes via accelerometer later.</p>
<p>Add this code to the end of <em>updateCCFromPhysics</em>:</p>

<div class="wp_codebox"><table><tr id="p788446"><td class="code" id="p7884code46"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// determine direction of the monkey</span>
    bool isLeft <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>direction &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// direction as string</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>dir <span style="color: #002200;">=</span> isLeft ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;left&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;right&quot;</span>;    
&nbsp;
    <span style="color: #11740a; font-style: italic;">// update animation phase</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>frameName;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> standingLimit <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.1</span>;
    <span style="color: #a61390;">float</span> vX <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self linearVelocity<span style="color: #002200;">&#93;</span>.x;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>vX &gt; <span style="color: #002200;">-</span>standingLimit<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &lt; standingLimit<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// standing</span>
        frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/idle/2.png&quot;</span><span style="color: #002200;">&#93;</span>;            
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// walking</span>
        <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>action <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;walk&quot;</span>;
        frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/%@/%@_%d.png&quot;</span>, action, dir, animPhase<span style="color: #002200;">&#93;</span>;        
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// set the display frame</span>
    <span style="color: #002200;">&#91;</span>self setDisplayFrameNamed<span style="color: #002200;">:</span>frameName<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Basically, all the code above does is, if the monkey&#8217;s speed is lower than standingLimit, it makes him look directly at the player with his idle animation frame. Otherwise, it uses a walk display frame matching the current direction and animation frame number.</p>
<p>Compile and run. The monkey now runs about – nice!</p>
<p><em>Slow Down Little Fella</em></p>
<p>I&#8217;m still not happy with one thing: I think the monkey moves too fast. We could reduce the impulse we&#8217;re applying to make him walk, but this will also make him slow and clumsy.</p>
<p>We need a strong enough impulse to make him react fast – but not too fast.</p>
<p>Replace the current code for <em>updateCCFromPhysics</em> in <em>Monkey.mm</em> with the following code:</p>

<div class="wp_codebox"><table><tr id="p788447"><td class="code" id="p7884code47"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// 1 - Call the super class</span>
    <span style="color: #002200;">&#91;</span>super updateCCFromPhysics<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// 2 - Update animation phase</span>
    animDelay <span style="color: #002200;">-=</span> 1.0f<span style="color: #002200;">/</span>60.0f;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>animDelay &lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        animDelay <span style="color: #002200;">=</span> ANIM_SPEED;
        animPhase<span style="color: #002200;">++</span>;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>animPhase &gt; <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            animPhase <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// 3 - Get the current velocity</span>
    b2Vec2 velocity <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self linearVelocity<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">float</span> vX <span style="color: #002200;">=</span> velocity.x;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// 4 - Determine direction of the monkey</span>
    bool isLeft <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>direction &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &gt; <span style="color: #002200;">-</span>MAX_VX<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &lt; MAX_VX<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// apply the directional impulse</span>
        <span style="color: #a61390;">float</span> impulse <span style="color: #002200;">=</span> clamp<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>direction<span style="color: #002200;">*</span>WALK_FACTOR, 
                              <span style="color: #002200;">-</span>MAX_WALK_IMPULSE, 
                              MAX_WALK_IMPULSE<span style="color: #002200;">&#41;</span>;            
        <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:-</span>b2Vec2<span style="color: #002200;">&#40;</span>impulse,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;        
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// 5 - Get direction as string</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>dir <span style="color: #002200;">=</span> isLeft ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;left&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;right&quot;</span>;        
&nbsp;
    <span style="color: #11740a; font-style: italic;">// 6 - Update animation phase</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>frameName;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> standingLimit <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.1</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>vX &gt; <span style="color: #002200;">-</span>standingLimit<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &lt; standingLimit<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// standing</span>
        frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/idle/2.png&quot;</span><span style="color: #002200;">&#93;</span>;            
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// walking</span>
        <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>action <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;walk&quot;</span>;
        frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/%@/%@_%d.png&quot;</span>, action, dir, animPhase<span style="color: #002200;">&#93;</span>;        
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// 7 - Set the display frame</span>
    <span style="color: #002200;">&#91;</span>self setDisplayFrameNamed<span style="color: #002200;">:</span>frameName<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>As you&#8217;ll notice, we&#8217;ve moved a few code blocks around but the major change is the addtion of a new line to section #3 for the <em>vX</em> variable and moving the impulse code to section #4 to be wrapped within an if condition that checks if the velocity is below a maximum value for the current direction. This allows the monkey to steer against being pushed away by an object, but keeps him from accelerating too fast on his own.</p>
<p>Compile and run. I thinks this looks much better now.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/mj2-02.png" alt="" title="mj2-02" width="700" height="372" class="aligncenter size-full wp-image-8190" style="background:none"/></p>
<p>The source code of the project in its current state is available in the folder <em>4-WalkingMonkey</em>.</p>
<h2>Jump, Jump!</h2>
<p>Now, let&#8217;s make the monkey jump. For this, we need to make the game detect touch events anywhere on the GameLayer, because we want the monkey to jump at every touch.</p>
<p>Open <em>GameLayer.mm</em> and enable touch detection by adding the following line to the <em>init</em> selector:</p>

<div class="wp_codebox"><table><tr id="p788448"><td class="code" id="p7884code48"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// enable touches</span>
    self.isTouchEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</pre></td></tr></table></div>

<p>Also add the following selector at the end of the file (but before the @end marker). It forwards the touches to the monkey object via the <em>jump</em> method.</p>

<div class="wp_codebox"><table><tr id="p788449"><td class="code" id="p7884code49"><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>ccTouchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/"><span style="color: #400080;">NSSet</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>monkey jump<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You might notice at this point that we don&#8217;t have a <em>jump</em> method in the Monkey class. That&#8217;s what we&#8217;ll add next. Switch to <em>Monkey.h</em> and add the following method definition just before @end:</p>

<div class="wp_codebox"><table><tr id="p788450"><td class="code" id="p7884code50"><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> jump;</pre></td></tr></table></div>

<p>Now open <em>Monkey.mm</em> and add the following code before the @end marker:</p>

<div class="wp_codebox"><table><tr id="p788451"><td class="code" id="p7884code51"><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> jump
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:</span>b2Vec2<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>JUMP_IMPULSE<span style="color: #002200;">&#41;</span> 
              point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;   
&nbsp;
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;jump.caf&quot;</span> 
              pitch<span style="color: #002200;">:</span>gFloatRand<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.8</span>,<span style="color: #2400d9;">1.2</span><span style="color: #002200;">&#41;</span>
              pan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>self.ccNode.position.x<span style="color: #002200;">-</span>240.0f<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> 240.0f 
              gain<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span> <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>All the above code does is to apply an impulse to make the monkey jump and play the jump sound. We&#8217;ll pan the sound as we did for the object sounds in <a href="http://www.raywenderlich.com/7261/monkey-jump">Part One</a> of this tutorial.</p>
<p>Compile and run. Tap the screen to make the monkey jump.</p>
<p><em>Outta Sight!</em></p>
<p>But this is far from perfect: if you tap the screen multiple times, the monkey goes through the roof! Worse still, the camera does not follow the monkey.</p>
<p>Let&#8217;s fix the camera first. First, add the following code to the top of the <em>update</em> selector, above the existing code, in <em>GameLayer.mm</em>.</p>

<div class="wp_codebox"><table><tr id="p788452"><td class="code" id="p7884code52"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// 0 - monkey's position</span>
    <span style="color: #a61390;">float</span> mY <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>monkey physicsPosition<span style="color: #002200;">&#93;</span>.y <span style="color: #002200;">*</span> PTM_RATIO;</pre></td></tr></table></div>

<p>The above code copies the monkey&#8217;s y-position into a new variable mY. Of course, we could also access the monkey&#8217;s ccNode and take the y-coordinate from there. The end result will be the same as multiplying his physics position by the PTM_RATIO.</p>
<p>Now, add these lines to the end of the <em>update</em> selector after the closing curly brace after section #7.</p>

<div class="wp_codebox"><table><tr id="p788453"><td class="code" id="p7884code53"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// 8 - Adjust camera</span>
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> monkeyHeight <span style="color: #002200;">=</span> 70.0f;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> screenHeight <span style="color: #002200;">=</span> 320.0f;
    <span style="color: #a61390;">float</span> cY <span style="color: #002200;">=</span> mY <span style="color: #002200;">-</span> monkeyHeight <span style="color: #002200;">-</span> screenHeight<span style="color: #002200;">/</span>2.0f; 
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>cY &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        cY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here, we calculate a good value for the camera&#8217;s y-coordinate, one that will more or less center the monkey on the middle of the screen. Clamp the value so that it does not go below 0 so that the camera does not move below ground level.</p>
<p>Now let&#8217;s implement parallax scrolling of the background so that the monkey&#8217;s movement appears more natural. The effect is quite easy to accomplish: just multiply the background layer by a factor below 1.0 and set the position for the layers. This will make the background layer scroll slower. The further away a layer is from the camera, the smaller the factor must be.</p>
<p>Add this code below the last few lines added to <em>update</em>:</p>

<div class="wp_codebox"><table><tr id="p788454"><td class="code" id="p7884code54"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// 9 - Do some parallax scrolling</span>
    <span style="color: #002200;">&#91;</span>objectLayer setPosition<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">-</span>cY<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>floorBackground setPosition<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">-</span>cY<span style="color: #002200;">*</span><span style="color: #2400d9;">0.8</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// move floor background slower</span>
    <span style="color: #002200;">&#91;</span>background setPosition<span style="color: #002200;">:</span>ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">-</span>cY<span style="color: #002200;">*</span><span style="color: #2400d9;">0.6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;      <span style="color: #11740a; font-style: italic;">// move main background even slower</span></pre></td></tr></table></div>

<p>Feel free to adjust the values to your liking.</p>
<p>That&#8217;s it – compile and run. Tap the screen multiple times to see the monkey rise. And beware of flying monkeys!!!!</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/mj2-03.png" alt="" title="mj2-03" width="700" height="372" class="aligncenter size-full wp-image-8192" style="background:none"/></p>
<div style="clear:both"></div>
<p><em>And It All Piles Up!</em></p>
<p>If you play the game for a while, you&#8217;ll notice that the items continue to drop from the same fixed height – sometimes way below the monkey&#8217;s current position. In fact, after some time, items don&#8217;t even drop because they&#8217;ve piled up beyond the spawning point.</p>
<p>Fix this by changing the following line in <em>GameLayer.mm</em>&#8216;s <em>update</em> method:</p>

<div class="wp_codebox"><table><tr id="p788455"><td class="code" id="p7884code55"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">float</span> yPos <span style="color: #002200;">=</span> <span style="color: #2400d9;">400</span>;</pre></td></tr></table></div>

<p>Change the above line to this:</p>

<div class="wp_codebox"><table><tr id="p788456"><td class="code" id="p7884code56"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">float</span> yPos <span style="color: #002200;">=</span> <span style="color: #2400d9;">400</span> <span style="color: #002200;">+</span> mY;</pre></td></tr></table></div>

<p>Items will now spawn 400pt above the monkey&#8217;s head, wherever it may be.</p>
<p>Back to the monkey. You&#8217;ll have noticed that the monkey can actually jump again and again while up in the air. This just won&#8217;t do. We need to fix it so the monkey only jumps when he has contact with the floor.</p>
<p>Let&#8217;s start by counting the number of contacts the monkey makes with the floor.</p>
<p>Add a new variable to <em>Monkey.h</em>:</p>

<div class="wp_codebox"><table><tr id="p788457"><td class="code" id="p7884code57"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">int</span> numFloorContacts;     <span style="color: #11740a; font-style: italic;">// number of floor contacts</span></pre></td></tr></table></div>

<p>Switch to <em>Monkey.mm</em> and add the following two new collision detection handlers to the end of the file (but above the @end marker):</p>

<div class="wp_codebox"><table><tr id="p788458"><td class="code" id="p7884code58"><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> beginContactWithFloor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>
    numFloorContacts<span style="color: #002200;">++</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> endContactWithFloor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>
    numFloorContacts<span style="color: #002200;">--</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As the names imply, the first one detects the beginning of a contact/collision with the floor and the second the end of a collision. In the beginContact selector, we increase the value of the floor contact variable, and then decrease it in the endContact selector.</p>
<p>These selectors are going to get called by GBox2D every time a contact starts or ends between the monkey and the floor. (Remember: we created a separate class for the floor so that GBox2D now can call the appropriate selector with the class&#8217;s name).</p>
<p>Now, if the monkey is standing on the floor, then the numFloorContacts value should be at least one. Use this to our advantages by wrapping the code in the <em>jump</em> method in <em>Monkey.mm</em> with an if condition to see if the monkey is actually standing on the floor before jumping:</p>

<div class="wp_codebox"><table><tr id="p788459"><td class="code" id="p7884code59"><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> jump
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>numFloorContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:</span>b2Vec2<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>JUMP_IMPULSE<span style="color: #002200;">&#41;</span> 
                               point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;   
&nbsp;
            ...
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Compile and run. Everything seems fine. Well&#8230; except that when the monkey lands on an object, he loses the ability to jump. To fix this, we&#8217;re going to consider any contact the monkey has with objects similar to a contact with the floor.</p>
<p>And it&#8217;s very simple to implement. Simply add a couple more collision-handling routines to the end of <em>Monkey.mm</em> and count object contacts the same way we  counted floor contacts:</p>

<div class="wp_codebox"><table><tr id="p788460"><td class="code" id="p7884code60"><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> beginContactWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// count object contacts as floor contacts </span>
        numFloorContacts<span style="color: #002200;">++</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> endContactWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// count object contacts as floor contacts </span>
        numFloorContacts<span style="color: #002200;">--</span>;        
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Compile and run. Isn&#8217;t it much better now? And our game is already playable!</p>
<h2>Push It!</h2>
<p>Let&#8217;s improve the gameplay and allow the monkey to push objects to the left and right &#8211; and give him the ability to put his hands above his head to shield him from dropping objects.</p>
<p>Do you remember how you added the sensors to the left and right sides of the monkey in <a href="http://www.raywenderlich.com/7261/monkey-jump">Part One</a>? They come into play now! The  key to the sensors was setting up the &#8220;Id&#8221; parameter in <a href="http://www.physicsedtior.de">PhysicsEditor</a>. You are going to retrieve this value now!</p>
<p>But before we do that, we need to add a few instance variables to keep count of the left and right sensors, as well as the number of contacts made with the monkey&#8217;s head. Add these variables to <em>Monkey.h</em>:</p>

<div class="wp_codebox"><table><tr id="p788461"><td class="code" id="p7884code61"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">int</span> numPushLeftContacts;
    <span style="color: #a61390;">int</span> numPushRightContacts;
    <span style="color: #a61390;">int</span> numHeadContacts;</pre></td></tr></table></div>

<p>Next, the beginContactWith* and endContactWith* selectors have a contact parameter we can use to determine which part of the monkey has contact with an object – the &#8220;Id&#8221; value we added in PhysicsEditor is stored as user data in each fixture. So replace the existing object contact handlers for <em>Monkey.mm</em> with the following:</p>

<div class="wp_codebox"><table><tr id="p788462"><td class="code" id="p7884code62"><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> beginContactWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
    <span style="color: #002200;">&#123;</span>
        <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>fixtureId <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact.ownFixture<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;">&#91;</span>fixtureId isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;push_left&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            numPushLeftContacts<span style="color: #002200;">++</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fixtureId isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;push_right&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            numPushRightContacts<span style="color: #002200;">++</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fixtureId isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;head&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            numHeadContacts<span style="color: #002200;">++</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// count others as floor contacts </span>
            numFloorContacts<span style="color: #002200;">++</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> endContactWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
    <span style="color: #002200;">&#123;</span>
        <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>fixtureId <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact.ownFixture<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;">&#91;</span>fixtureId isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;push_left&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            numPushLeftContacts<span style="color: #002200;">--</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fixtureId isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;push_right&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            numPushRightContacts<span style="color: #002200;">--</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fixtureId isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;head&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            numHeadContacts<span style="color: #002200;">--</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// count others as floor contacts </span>
            numFloorContacts<span style="color: #002200;">--</span>;        
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As you see from the new code, you retrieve the Id, which we&#8217;ll call fixtureId here, by accessing the contact parameter&#8217;s fixture and then accessing the fixture&#8217;s user data via the GetUserData method.</p>
<p>Now that we&#8217;re tracking the contacts, we can update the monkey&#8217;s animation frames to handle additional events.</p>
<p>Here&#8217;s the decision table for the various animations:</p>
<table>
<tr>
<th><center>Monkey</center></th>
<th><center>Objects</center></th>
<th><center>Animation</center></th>
</tr>
<tr>
<td>Standing</td>
<td>No object above head</td>
<td>Idle</td>
</tr>
<tr>
<td>Standing</td>
<td>Object above head</td>
<td>Arms up</td>
</tr>
<tr>
<td>Moving</td>
<td>No push sensor active in the direction</td>
<td>Walk</td>
</tr>
<tr>
<td>Moving</td>
<td>Push sensor active in the direction</td>
<td>Push</td>
</tr>
</table>
<p>Using the above table, we modify section #6 of <em>updateCCFromPhysics</em> in <em>Monkey.mm</em> as follows:</p>

<div class="wp_codebox"><table><tr id="p788463"><td class="code" id="p7884code63"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// 6 - Update animation phase</span>
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> standingLimit <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.1</span>;
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>frameName <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>vX &gt; <span style="color: #002200;">-</span>standingLimit<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &lt; standingLimit<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>numHeadContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// Standing, object above head</span>
            frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/arms_up.png&quot;</span><span style="color: #002200;">&#93;</span>;                        
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// Just standing</span>
            frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/idle/2.png&quot;</span><span style="color: #002200;">&#93;</span>;            
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>numFloorContacts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// Jumping, in air</span>
            frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/jump/%@.png&quot;</span>, dir<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// Determine if monkey is pushing an item</span>
            bool isPushing <span style="color: #002200;">=</span>  <span style="color: #002200;">&#40;</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>numPushLeftContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
            || <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>numPushRightContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">// On the floor</span>
            <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>action <span style="color: #002200;">=</span> isPushing ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;push&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;walk&quot;</span>;
&nbsp;
            frameName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;monkey/%@/%@_%d.png&quot;</span>, action, dir, animPhase<span style="color: #002200;">&#93;</span>;        
        <span style="color: #002200;">&#125;</span>        
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Compile and test. Perfect! The monkey now behaves just as we want.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/mj2-04.png" alt="" title="mj2-04" width="700" height="372" class="aligncenter size-full wp-image-8195" style="background:none"/></p>
<p><em>It Takes a Strong Monkey &#8230;</em></p>
<p>Playing a little bit more though, I think the monkey should be a bit stronger under certain conditions. Currently, he&#8217;s too weak to break free when an object is above his head. Let&#8217;s give him some extra strength when he&#8217;s trapped like that and wants to jump.</p>
<p>This is the current line from the jump selector in <em>Monkey.mm</em> that makes the monkey jump:</p>

<div class="wp_codebox"><table><tr id="p788464"><td class="code" id="p7884code64"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:</span>b2Vec2<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>JUMP_IMPULSE<span style="color: #002200;">&#41;</span> 
                       point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Replace it with:</p>

<div class="wp_codebox"><table><tr id="p788465"><td class="code" id="p7884code65"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">float</span> impulseFactor <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// if there is something above monkey's head make the push stronger</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>numHeadContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        impulseFactor <span style="color: #002200;">=</span> <span style="color: #2400d9;">2.5</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:</span>b2Vec2<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>JUMP_IMPULSE<span style="color: #002200;">*</span>impulseFactor<span style="color: #002200;">&#41;</span> 
                       point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>That&#8217;s our monkey on steroids! He now uses a 2.5-times stronger impulse when there&#8217;s an object resting above him – this should allow him to break free of most of the objects.</p>
<p>Let&#8217;s also change the walking impulse in case the monkey needs to push an object to the side to break free. Go to <em>updateCCFromPhysics</em> and cut the following lines from section #6:</p>

<div class="wp_codebox"><table><tr id="p788466"><td class="code" id="p7884code66"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// Determine if monkey is pushing an item</span>
    bool isPushing <span style="color: #002200;">=</span>  <span style="color: #002200;">&#40;</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>numPushLeftContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
                   || <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>numPushRightContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>Now paste that code into section #4 and modify it as follows:</p>

<div class="wp_codebox"><table><tr id="p788467"><td class="code" id="p7884code67"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// 4 - Determine direction of the monkey</span>
    bool isLeft <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>direction &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Determine if monkey is pushing an item</span>
    bool isPushing <span style="color: #002200;">=</span>  <span style="color: #002200;">&#40;</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>numPushLeftContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
                   || <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>numPushRightContacts &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &gt; <span style="color: #002200;">-</span>MAX_VX<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>isLeft <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>vX &lt; MAX_VX<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// apply the directional impulse</span>
        <span style="color: #a61390;">float</span> impulse <span style="color: #002200;">=</span> clamp<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #002200;">&#91;</span>self mass<span style="color: #002200;">&#93;</span><span style="color: #002200;">*</span>direction<span style="color: #002200;">*</span>WALK_FACTOR, 
                              <span style="color: #002200;">-</span>MAX_WALK_IMPULSE, 
                              MAX_WALK_IMPULSE<span style="color: #002200;">&#41;</span>;       
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>isPushing<span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            impulse <span style="color: #002200;">*=</span> <span style="color: #2400d9;">2.5</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#91;</span>self applyLinearImpulse<span style="color: #002200;">:-</span>b2Vec2<span style="color: #002200;">&#40;</span>impulse,<span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> point<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self worldCenter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;        
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Compile and test – that&#8217;s much better. But there&#8217;s still a problem: when the monkey slightly grazes an object with his head, the new jump power makes him go through the roof!</p>
<p>We need to clamp his maximum speed inside the updateCCFromPhysics method. Add this to the end of section #3 of the <em>updateCCFromPhysics</em> method:</p>

<div class="wp_codebox"><table><tr id="p788468"><td class="code" id="p7884code68"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">const</span> <span style="color: #a61390;">float</span> maxVelocity <span style="color: #002200;">=</span> <span style="color: #2400d9;">5.0</span>;
    <span style="color: #a61390;">float</span> v <span style="color: #002200;">=</span> velocity.Length<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>v &gt; maxVelocity<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self setLinearVelocity<span style="color: #002200;">:</span>maxVelocity<span style="color: #002200;">/</span>v<span style="color: #002200;">*</span>velocity<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Notice that in the code above we are directly modifying values controlled by the Box2d engine, thus affecting the overall behaviour of the physics engine. You should try to avoid doing this kind of manipulation.</p>
<p>Compile and test. I like the monkey&#8217;s behavior now. He reacts quickly, and is strong enough to push objects but does not become uncontrollable.</p>
<h2>Where To Go From Here?</h2>
<p>If you don&#8217;t have it already, here is all of the <a href="http://www.raywenderlich.com/downloads/monkeyjump.zip">source code</a> for this tutorial series.</p>
<p>You&#8217;ve now reached the end of Part Two of the MonkeyJump tutorial! The project in its current form is available in the source code zip in the folder called 5-MonkeyJumpAndRun.</p>
<p>Stay tuned for the final part of the series, where we&#8217;ll add some performance improvements, add a HUD layer to the game, and yes &#8211; kill the monkey!  :]</p>
<p>In the meantime, if you have any questions or comments, please join the forum discussion below!</p>
<p><img src="http://www.raywenderlich.com/downloads/AndreasLoew.jpg" class="photo alignleft"/><i>This is a post by special contributor <a href="http://twitter.com/CodeAndWeb">Andreas Loew</a>, the creator of TexturePacker and Physics Editor.</i></p>
<p><a href="http://www.raywenderlich.com/7884/how-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2">How To Build a Monkey Jump Game Using Cocos2D, PhysicsEditor &#038; TexturePacker Part 2</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton7884" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FzM6vy5&amp;via=rwenderlich&amp;text=How%20To%20Build%20a%20Monkey%20Jump%20Game%20Using%20Cocos2D%2C%20PhysicsEditor%20%26%23038%3B%20TexturePacker%20Part%202&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7884%2Fhow-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/rvH6a_U2y3I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/7884/how-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/7884/how-to-build-a-monkey-jump-game-using-cocos2d-physicseditor-texturepacker-part-2</feedburner:origLink></item>
		<item>
		<title>How To Build a Monkey Jump Game Using Cocos2D, PhysicsEditor &amp; TexturePacker Part 1</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/cJy-CHdkuPo/monkey-jump</link>
		<comments>http://www.raywenderlich.com/7261/monkey-jump#comments</comments>
		<pubDate>Wed, 01 Feb 2012 15:00:49 +0000</pubDate>
		<dc:creator>aloew</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[box2D]]></category>
		<category><![CDATA[cocos2D]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=7261</guid>
		<description><![CDATA[TweetThis is a post by special contributor Andreas Loew, the creator of TexturePacker and PhysicsEditor. In this tutorial, youll learn [...]<p><a href="http://www.raywenderlich.com/7261/monkey-jump">How To Build a Monkey Jump Game Using Cocos2D, PhysicsEditor &#038; TexturePacker Part 1</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton7261" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2Frp4rFn&amp;via=rwenderlich&amp;text=How%20To%20Build%20a%20Monkey%20Jump%20Game%20Using%20Cocos2D%2C%20PhysicsEditor%20%26%23038%3B%20TexturePacker%20Part%201&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7261%2Fmonkey-jump" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><i>This is a post by special contributor <a href="http://twitter.com/CodeAndWeb">Andreas Loew</a>, the creator of TexturePacker and PhysicsEditor.</i></p>
<p>In this tutorial, youll learn how to make a game about a monkey having a rough day.  He&#8217;s just minding his own business, but these crazy objects keep falling from the sky!</p>
<p>To see what I mean, check out this cool video:</p>
<p><iframe src="http://player.vimeo.com/video/34747053" width="700" height="394" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<p>By the end of this tutorial, you&#8217;ll have created a cool physics enabled game, and learned how using TexturePacker and PhysicsEditor can save you a ton of development time.</p>
<p>To go through this tutorial, you should have some basic familiarity with Cocos2D.  If you are new to Cocos2D, check out some of the <a href="http://www.raywenderlich.com/tutorials#cocos2d">other tutorials</a> on this site first.</p>
<p>You will also need a copy of TexturePacker and PhysicsEditor.  To to through this tutorial you can use an evaluation version from <a href="http://www.texturepacker.com">TexturePacker.com</a> and <a href="http://www.physicseditor.de">physicseditor.de</a>, but note that there are some features that are not present in the evaluation versions.  If you want to order the full versions, here&#8217;s a <a href="http://tinyurl.com/6wyz9ax">direct link to order the bundle</a>.</p>
<p>Ready to make that monkey jump?  Keep reading to get started!</p>
<p><span id="more-7261"></span></p>
<h2>Getting Started</h2>
<p>We will start with the design phase. So let me give you a brief overview of how the game will work.</p>
<p>As you&#8217;ve seen, our main character will be a monkey. His movement will be controlled with the accelerometer – that is, by tilting the iPhone or iPad left and right. The monkey will jump when the screen is tapped.</p>
<p>During the game, items will drop from the top of the screen, and the dropping frequency will increase the longer the game continues. We will have a drop indicator to show the player where the next item will fall. Items pile up, and the monkey must stay on top to stay alive.</p>
<p>There will be two sorts of items: those that hurt the monkey, and those that heal him – namely, bananas! The monkey&#8217;s health will be displayed in the top-left corner as a bar of bananas.</p>
<p>The score will be displayed in the top-right corner, and will be at the height of the highest item on the stack.</p>
<p><img class="size-full wp-image-7262" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/Design.png" alt="" width="700" height="526" /></p>
<p>To get started, <a href="http://www.raywenderlich.com/downloads/monkeyjump.zip">download the tutorial source code</a> and unzip it into your preferred location. Your starting point will be <em>0-BaseProject</em>, which contains the basic Cocos2d setup and some integrated assets.</p>
<p>I&#8217;ve added directories for several stages of the development process containing the results you should achieve. These directories will make your life easier in case you get lost, or if you simply want to skip one part of the project.</p>
<p>Each folder contains the complete project and is independent from the rest. Inside each folder are two more folders:</p>
<p>The first is called <em>Assets</em>, and contains such things as the TexturePacker and PhysicsEditor save files, a directory holding the Xcode project, and a <a href="http://www.vickiwenderlich.com/2011/06/game-art-pack-monkey-platformer/">free game art pack</a> created by Vicki Wenderlich.</p>
<p>The Assets folder is organized further into the subfolders <em>background</em>, containing the background graphics, and <em>jungle</em>, containing the foreground objects and animation.</p>
<p>The second folder is called <em>MonkeyJump</em>. It contains the Xcode project and the sources, which are contained in a second <em>MonkeyJump</em> folder which has the following subfolders:</p>
<ul>
<li>libs: Cocos2d, Box2d, Cocos Denshion&#8230; all the Cocos2d stuff</li>
<li>GBox2D: my ObjectiveC++ Box2d wrapper</li>
<li>Resources: sounds and music</li>
</ul>
<h2>Creating Sprite Sheets</h2>
<p>Time to start the real work, beginning with sprite sheets.</p>
<p>Open the <em>0-BaseProject</em> folder and have a look at the <em>Assets</em> folder. I&#8217;ve already arranged the images for us.  We&#8217;re going to add the sprites from the &#8220;background&#8221; folder into one sprite sheet, and the sprites from the &#8220;jungle&#8221; folder into as second.</p>
<p>The background sheet will use RGB565, a 16-bit image format that reduces the amount of memory used by 50%. Using RGB565 will also speed up rending. The foreground sheet, with most of the content, will use RGBA8888 for full quality.  For more information on pixel formats with TexturePacker, check out <a href="http://tinyurl.com/6wyz9ax">this tutorial</a>.</p>
<p><em>Creating the Background Sprite Sheet</em></p>
<p>Let&#8217;s start by creating the background sheet. Start TexturePacker, navigate to the O-BaseProject\Assets\background folder, and drag it into the right hand side of the TexturePacker window.  You&#8217;ll see the jungle background image show up in the center of the window.</p>
<p><img class="size-full wp-image-7277" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/1-01-background-settings-1.png" alt="" width="700" height="471" /></p>
<p>To set up the parameters for the texture, first choose the Data format, which is of course Cocos2d. Next choose the Texture format. We will use zlib compressed PVR format .pvr.ccz.</p>
<p>TexturePacker warns us that we should turn on pre-multiplied alpha. Let&#8217;s agree to that and click “Apply.”</p>
<p>Pre-multiply means that all color values get multiplied by their transparency value when saving the file. This speeds up graphics rendering in the game, since the multiplication step does not need to be performed at runtime.</p>
<p>The main advantage of pvr.ccz is that it can be loaded quite fast, and it usually consumes less memory than PNG. Note in the bottom-right corner of TexturePacker how the memory consumption is reduced from 8192kB to 4096kB.</p>
<p>The quality of the image will suffer a bit: we can see that the gradients are now not as smooth and have some artifacts called <em>banding</em>. We can compensate for this by setting Dithering to FloydSteinberg.</p>
<p>The following two images show a part of the jungle. The left one has no dithering, while on the right one, dithering has been enabled:</p>
<div id="attachment_7284" class="wp-caption alignleft" style="width: 229px"><a rel="attachment wp-att-7284" href="http://www.raywenderlich.com/?attachment_id=7284"><img class="size-full wp-image-7284" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/1-02-a-background-without-dithering.png" alt="" width="219" height="219" /></a><p class="wp-caption-text">RGB565 image with banding artifacts</p></div>
<div id="attachment_7285" class="wp-caption alignleft" style="width: 229px"><a rel="attachment wp-att-7285" href="http://www.raywenderlich.com/?attachment_id=7285"><img class="size-full wp-image-7285" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/1-02-b-background-with-dithering.png" alt="" width="219" height="219" /></a><p class="wp-caption-text">RGB565 image with dithering</p></div>
<div style="clear:both"></div>
<p>Next, set the Data file to background-hd.plist in the Resources directory of the Xcode project. This will automatically set the Texture file to background-hd.pvr.ccz.</p>
<p>Don&#8217;t worry that the paths are displayed as absolute paths. TexturePacker creates a relative path to the document file as soon as it is saved. This means we can move our project around as long as we don&#8217;t change the relative position of the assets and resources to the saved .tps file.</p>
<p>The -hd extension is important, because TexturePacker can save retina display and reduced resolution images for older devices. To enable this, simply check the AutoSD option. This will create background-hd.plist and background-hd.pvr.ccz files for retina display, and background.plist and background.pvr.ccz for reduced resolution.</p>
<p>Max. width and Max. height appear in red, which means TexturePacker can&#8217;t layout the sprite within the texture&#8217;s dimensions. This is because padding is set to 2 by default.</p>
<p>Scroll down on the left pane and set both border padding and shape padding to 0. Now all numbers should appear in black.</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/background-settings-2.png"><img class="alignnone size-large wp-image-8031" title="background-settings-2" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/background-settings-2-500x367.png" alt="" width="500" height="367" /></a></p>
<p>Finally, let&#8217;s save our sheet as background.tps in the Assets folder by first clicking <em>Save</em> and then <em>Publish</em>. This creates four files in the Resources folder: background-hd.plist, background-hd.pvr.ccz, background.plist and background.pvr.ccz. (Open the Resources folder to make sure the files are there.)</p>
<p><em>Creating the Jungle Sprite Sheet</em></p>
<p>Now create a new sheet for the foreground by pressing the <em>New</em> button on the toolbar. Drag the complete jungle folder from the Assets folder into the empty right pane.</p>
<p>See how TexturePacker builds the complete directory structure on the right pane? (You can expand the tree view by pressing the arrow button to the left of the word &#8220;jungle&#8221;.)</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/FolderHierarchy-500x245.png" alt="Folders in Texture Packer" title="Folders in Texture Packer" width="500" height="245" class="alignnone size-large wp-image-9652 bordered" /></p>
<p>We&#8217;re using a feature called Smart Folders, where TexturePacker rescans the directory every time we add sprites to the folder. If you want to add a new asset, or rename or change an existing asset, just drop it into the folder on your file system, and when you reenter TexturePacker it will automatically update the sheet.</p>
<div class="note">
<p><em>Note:</em> If you need to update many sprite sheets at once, you can switch to Terminal and update all .tps files from the command line by calling TexturePacker *.tps.</p>
<p>You can even integrate TexturePacker with your Xcode build process. This has several advantages. </p>
<p>First, you no longer need to worry about sprite sheets – just add the sprites to your Assets folder, and you&#8217;re done.  </p>
<p>Second, if you use version control, sprite sheets will hang around as binary blobs. This will grow your working copy size pretty fast, especially when using Git or Mercurial for version control.  The easy way around this is to simply not add the sprite sheets to version control, and instead create them on the fly.  </p>
<p>For more information on integrating TexturePacker with Xcode 4, check out <a href="http://www.texturepacker.com/cocos2d/xcode4-integration-tutorial-of-texturepacker-for-cocos2d-and-sparrow-framework/">this tutorial</a>.</div>
<p>For the options, we&#8217;ll use a nearly identical setup as for the background sheet (see screenshot below).  Just make sure to leave the image format at RGBA8888 for full quality.</p>
<p><img class="size-full wp-image-7306" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/1-04-foreground-settings.png" alt="" width="700" height="423" /></p>
<p>Also be sure to leave the paddings set to the default of 2. This will reduce artifacts on the sprite borders. Sometimes OpenGL drags in pixels from neighboring sprites. Padding avoids this problem by adding transparent borders around the sprites.</p>
<div class="note">
<p><em>Note:</em> If you want to use sprites as tiles with seamless connections, you must use either select extrude or reduce border artifacts, which replaces the transparent borders with colored ones. If you do not do this, you might get transparent lines between the tiles.</p>
</div>
<p>Set jungle-hd.plist in the Resources folder of the project as the Data file, and jungle-hd.pvr.ccz in the same folder as the Texture file. Check the box next to AutoSD.</p>
<p>Finally, save the sheet as “jungle.tps” in the Assets folder and click Publish. This creates four files in the Resources folder: jungle-hd.plist, jungle-hd.pvr.ccz, jungle.plist, and jungle.pvr.ccz. Check and see if they are there.</p>
<p><img class="aligncenter size-full wp-image-7672" style="border: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/1-a-SpriteSheets.png" alt="" width="275" height="378" /></p>
<h2>Creating Physics Shapes Manually</h2>
<p>Next we&#8217;ll set up the physics shapes with PhysicsEditor!</p>
<p>Launch PhysicsEditor and start with the basic setup. We need to set the Exporter to Box2d Generic (PLIST). For this I added a loader which you can use in your Cocos2d projects. If you are not satisfied with the formats supported by PhysicsEditor, you can simply create your own custom data exporter.</p>
<p>Set the PTM-Ratio to 170. This tells Box2d that 170 pixels are equal to 1 meter (39.37 inches), the internal measurement unit of Box2d. I chose 170 pixels because this is the monkey&#8217;s height, and he is going to be 1 meter tall in the simulation.</p>
<p>We&#8217;re going to create all the physics shapes for the high-res sprites only. This is okay, since we want the physics parameters to be identical on retina display and old displays.</p>
<p>Creating different shapes for different screen resolutions would create different masses for each object, resulting in different physics behavior – which we don&#8217;t want! The idea is to run the same simulation on all devices and only adjust the visuals.</p>
<p><img class="size-full wp-image-7321" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-01-physicseditor-setup.png" alt="" width="700" height="456" /></p>
<p><em>Setting Up the Fixture</em></p>
<p>We&#8217;re now going to create our first shape by hand. Drag the jungle/floor/grassfront.png shape onto the left pane of PhysicsEditor. When you do this, the shape will also appear in the center pane, which is the main editor area.</p>
<p>Note the small blue circle with the cross inside: that is the anchor point of the shape. We&#8217;re going to leave the anchor point for this shape where it is, on the bottom right corner. Later on, I&#8217;ll show you how to change the anchor point.</p>
<p>First use the polygon tool from the top tool bar. As soon as you click on it, a small red triangle appears.  You can drag this triangle with your mouse.</p>
<p>The handles allow you to change the triangle&#8217;s shape. Double-clicking somewhere near a line will add an additional vertex. Double-click the vertex to remove it.</p>
<p>You do not have to care about things like polygon orientation or convex or concave shapes. This is all handled by PhysicsEditor.</p>
<p>Now create a rectangular shape for the floor. It should cover the soil but not the grass, and look something like this:</p>
<p><img class="size-full wp-image-7326" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-02-editing-shapes.png" alt="" width="700" height="456" /></p>
<p><em>Setting Up the Parameters</em></p>
<p>Since the floor is going to be a static shape, its parameters are going to be very basic.</p>
<p>Density affects the weight of a body/fixture. Since the floor will not move, the value of the density is of no importance. Restitution is the degree a colliding shape will rebound from another object. For the floor, we want to keep this at 0.</p>
<p>Friction is the only important value here, as it determines how much the sprites will slide on the floor. Set this to 0.7.</p>
<p>The next items to set up are the collision bits. PhysicsEditor lets you handle these parameters quite easily – that is, without doing hex math. First, give the bits useful names by setting up the names in the text fields associated with the bits:</p>
<ul>
<li>floor – both the floor and the walls</li>
<li>monkey – what else?</li>
<li>good_objects – objects that don&#8217;t hurt the monkey</li>
<li>bad_objects – objectw that hurt the monkey</li>
</ul>
<p>Ignore the other bits – we don&#8217;t need them.</p>
<p><img class="aligncenter size-full wp-image-7675" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-03-setting-the-parameters1.png" alt="" width="700" height="441" /></p>
<p>Two objects can collide only when Object B&#8217;s bit category is of a type that has been selected as a mask for Object A, and vice-versa. We will tick the Category field for the floor bit since this shape is a floor, and then the Mask field for all the bits. This enables collision between all objects and the floor.</p>
<p>Save the project as “shapes.pes” inside the Assets folder.</p>
<h2>A Little Magic: Creating Shapes with the Tracer</h2>
<p>Creating shapes by hand isn&#8217;t always fun. So let&#8217;s use the magic wand tool and let PhysicsEditor do the work for us!</p>
<p>Drop the following objects onto the left pane: backpack, banana, bananabunch, canteen, hat, pineapple, and statue.</p>
<p><img class="aligncenter size-full wp-image-7678" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-04-autotrace1.png" alt="" width="699" height="457" /></p>
<p>Select the backpack, and click the magic wand icon on the tool bar. This will open a new window, the tracer. The tracer shows the current shape with an overlay of the traced shape.</p>
<p><img class="size-full wp-image-7329" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-05-tracer-in-action.png" alt="" width="700" height="463" /></p>
<p>The most important setting is the Tolerance. This value tells the tracer how exactly the polygon should match the shape. It directly influences the number of vertices the polygon will have. For example, setting the Tolerance to 20 will result in a 5-vertex polygon that no longer fits the shape; setting it to 1 creates a perfectly-matching polygon with 39 vertices.</p>
<div id="attachment_7332" class="wp-caption alignleft" style="width: 179px"><img class="size-full wp-image-7332" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-06-c-tracer-too-low.png" alt="" width="169" height="199" /><p class="wp-caption-text">Not enough vertexes</p></div>
<div id="attachment_7331" class="wp-caption alignleft" style="width: 179px"><img class="size-full wp-image-7331" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-06-b-tracer-too-many.png" alt="" width="169" height="199" /><p class="wp-caption-text">Too many vertexes</p></div>
<div id="attachment_7330" class="wp-caption alignleft" style="width: 178px"><img class="size-full wp-image-7330" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-06-a-tracer-ok.png" alt="" width="168" height="199" /><p class="wp-caption-text">Good trace</p></div>
<div style="clear:both"></div>
<p>A Tolerance value of 5 gives us an acceptable match, with about 15 vertices (the exact amount sometimes varies with the starting point of the tracer). That&#8217;s fine! Click OK to get back to the main screen. If you want you can fine-tune the shape, but don&#8217;t be too finicky!</p>
<p>Now set the backpack&#8217;s parameters. Set the Density to 5.00, the Restitution to 0.10 (so that the backpack bounces a little bit) and the Friction to 0.5. You might want to play around with these parameters later to get a better feel for how they work.</p>
<p>In the collisions section, tick the Cat. box next to bad_objects, and enable collision with all other objects by making sure all the Mask boxes are ticked. </p>
<p>Also drag the anchor point (the blue circle with the cross inside) to the center of the backpack shape. Alternatively, you can set the anchor point to the exact center of the shape by setting the Relative anchor point values in the right sidebar to 0.5 and 0.5.</p>
<p><img class="aligncenter size-full wp-image-7681" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/2-07-setting-up-the-backpack1.png" alt="" width="700" height="457" /></p>
<p>Now that you know how to create shapes, either by hand or using the tracer, go ahead and do the remaining ones yourself! Use the parameters indicted in the table below. And don&#8217;t forget to drag the anchor point for each shape to the position indicated in the images below.</p>
<table>
<tbody>
<tr>
<th>Object</th>
<th>Tolerance</th>
<th>Density</th>
<th>Restitution</th>
<th>Friction</th>
<th>Category (Cat.)</th>
<th>Mask</th>
</tr>
<tr>
<td>backpack</td>
<td>5</td>
<td>5.0</td>
<td>0.1</td>
<td>0.5</td>
<td>bad_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
<tr>
<td>canteen</td>
<td>5</td>
<td>3.0</td>
<td>0.1</td>
<td>0.2</td>
<td>bad_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
<tr>
<td>hat</td>
<td>5</td>
<td>1.0</td>
<td>0.15</td>
<td>0.4</td>
<td>bad_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
<tr>
<td>statue</td>
<td>5</td>
<td>10.0</td>
<td>0.05</td>
<td>0.7</td>
<td>bad_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
<tr>
<td>pineapple</td>
<td>5</td>
<td>1.0</td>
<td>0.4</td>
<td>0.7</td>
<td>bad_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
<tr>
<td>banana</td>
<td>5</td>
<td>1.0</td>
<td>0.5</td>
<td>0.5</td>
<td>good_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
<tr>
<td>bananabunch</td>
<td>5</td>
<td>1.0</td>
<td>0.3</td>
<td>0.7</td>
<td>good_objects</td>
<td>floor, monkey, bad_objects, good_objects</td>
</tr>
</tbody>
</table>
<p><img class="aligncenter size-full wp-image-7686" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/4-03-objects.png" alt="" width="700" height="477" /></p>
<h2>Last But Not Least: the Monkey</h2>
<p>There&#8217;s one shape we have left to create, and that&#8217;s because it&#8217;s the most complex. Our monkey consists of quite a few animation phases (fourteen total):</p>
<p><img class="size-full wp-image-7355" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/4-01-monkey-phases.png" alt="" width="700" height="79" /></p>
<p>The first idea that comes to mind is to add a collision polygon for each animation frame to make a perfect match for each phase of the animation. But this would be a bad idea.</p>
<p>First of all, the frequent exchange of fixtures would consume a good deal of CPU power. But the bigger problem is that with each new shape, the mass of the monkey would change!</p>
<p>This is because Box2d calculates mass based on a polygon&#8217;s area and density. If you change the polygon, the monkey will gain or lose weight! And this will lead to the physics simulation behaving erratically.</p>
<p>The solution is to make only one shape for the monkey, which we will try to make as good a fit as possible. To begin, drag one of the monkey shapes onto PhysicsEditor. You can rename the shape by double-clicking its name.</p>
<p><img class="size-full wp-image-7356" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/4-02-addmonkey.png" alt="" width="700" height="509" /></p>
<p>Now click the &#8220;+&#8221; button to add the other animation phases for the monkey. The files will show up in the combo box and you can switch between them.</p>
<p>Start the tracer by clicking on the magic wand icon. What you see now is that the shape does not fit the monkey anymore. This is exactly what we want.</p>
<p><img class="size-full wp-image-7357" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/4-03-tracer.png" alt="" width="700" height="424" /></p>
<p>The tracer is now in animation tracing mode. You can switch between two frame modes: <em>intersection</em> and <em>union</em>. Intersection creates the polygon only from the parts that are covered by all sprites, whereas union uses the parts that are covered by any sprite. Choose intersection.</p>
<p>The slider allows you to browse through the animation phases and see how the shape fits the different phases.</p>
<p>We will take the shape the tracer gave us just as an estimate. This is because we must split up monkey&#8217;s body into several parts. Here is what we have to build:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/4-04-monkey-shape-500x126.png" alt="" title="4-04-monkey-shape" width="500" height="126" class="alignnone size-large wp-image-8158" /></p>
<p>Sometimes polygon shapes don&#8217;t slide smoothly over other polygons and stick to each other. This is why we&#8217;re building the monkey from circle shapes.</p>
<p>The other reason is that using circles results in faster collision detection. The math to check if a point is inside a circle is much simpler than that for testing for a point inside a polygon. So we&#8217;ll save CPU power. We&#8217;ll delete the tracer shape after the other parts are set up.</p>
<p>First build the head of the monkey. Use a circle shape and make it as big as the traced version. The head must be treated differently from the other body parts, since we want the monkey to get hurt when objects land on his head from above.</p>
<p>To distinguish the head from the other parts, set the Id to “head.”  The head&#8217;s category is monkey, and it collides with good_objects, bad_objects and floor, so make sure these are selected as masks. Set the density to 2.0, the restitution to 0.1, and the friction to 0.5.</p>
<p>Next build the body of the monkey from two circles, one covering the torso and one the legs. Set the Id to “body.” The body&#8217;s category is monkey, and it collides with good_objects, bad_objects and floor. Set the body&#8217;s density to 2.0, its restitution to 0.1, and its friction to 0.5.</p>
<p>Cycle through the different animation phases by selecting the different images from the Filename combo box. Check if the circles fit all phases. It doesn&#8217;t have to be a perfect fit, and you can come back to PhysicsEditor any time you want to tweak the collision set.</p>
<p>Finally, delete the polygon created with the tracer.</p>
<p>During the game we need to check if there are objects to the left or right to trigger the push animation.  To do this, place two sensors on the left and right sides of the monkey.</p>
<p>Sensors are fixtures that don&#8217;t interact with other bodies and simply report collisions. To make a fixture a sensor, set isSensor.</p>
<p>Set the Id to “push_left” for the left sensor and “push_right” for the right one. The category is monkey, and they collide with bad_objects only – we don&#8217;t want the monkey push the floor and the bananas away!</p>
<div id="attachment_7430" class="wp-caption alignnone" style="width: 710px"><a rel="attachment wp-att-7430" href="http://www.raywenderlich.com/?attachment_id=7430"><img class="size-full wp-image-7430" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/4-04-monkey-shape.png" alt="" width="700" height="177" /></a><p class="wp-caption-text">Make the monkey shape fit all animation phases</p></div>
<p>One last thing to do: save and click Publish! When asked for a location, store the file in the Resources folder of the project as shapes.plist.</p>
<p>That&#8217;s it for now in PhysicsEditor. Next we move to Xcode and start coding our game!</p>
<h2>The Xcode Project: Overview</h2>
<p>I created the starter project using the standard Cocos2d+Box2d template that comes with Cocos2d and  removing all the demo stuff. I also added the sound resources and some classes which will make your life easier when working with Box2d inside Cocos2d.</p>
<p>One important thing to know about this project is that all files must have the .mm extension instead of .m. This is because Box2d is C++ based, so we must use Objective-C++ for development.</p>
<p>Now let&#8217;s discuss the starter project in more detail:</p>
<p><em>GBox2D</em></p>
<p>First let me give you some information about GBox2D. GBox2D is an Objective-C wrapper for Box2d that I developed for my upcoming game <a href="http://www.turtletrigger.com">TurtleTrigger</a>.</p>
<p>These are the main classes in Gbox2D:</p>
<ul>
<li><em>GB2Engine</em>: This class wraps the Box2d world simulation. It runs the simulation, updates all sprite positions and rotations and can iterate through all objects in the world. It is implemented as a singleton class to make access as easy as possible.</li>
<li><em>GB2Node</em>: This class combines a CCNode and a B2Body. It&#8217;s the glue between the physics simulation and the graphics representation inside Cococs2d. It also contains selectors for simple management of the physics object and implements a proxy to access the inner node&#8217;s data.</li>
<li><em>GB2Sprite</em>: This class is derived from GB2Node and specializes in using CCSprite as the inner object.</li>
<li><em>GB2DebugDrawLayer</em>: This is a Cocos2d layer that wraps the debug drawing. It can be added to your project like a normal layer. When added it will draw the physics shape outlines. The nice thing about it is that it detects when running on a retina display target and scales the content accordingly.</li>
<li><em>GB2Contact</em>: This structure will be passed as parameter to an object when a collision is detected. Both objects involved in the collision will be called for each single point of contact.</li>
<li><em>GB2WorldContactListener</em>: This is a C++ class that reacts to collisions in the physics simulation.</li>
</ul>
<p>If you&#8217;re curious, go ahead and scan through these classes to get an idea of what they do.  Don&#8217;t worry if you don&#8217;t understand what they&#8217;re doing &#8211; you&#8217;ll learn how to use it in the next few sections.</p>
<p><em>Collision Detection with Gbox2D</em></p>
<p>GBox2D makes collision detection a piece of cake! This is because you do not need to create one huge switch-case statement or a series of if-else cascades to detect the various possible collision combinations.</p>
<p>Instead, GBox2D simply uses the names of the colliding classes and calls selectors with names derived from the class names! If this sounds too abstract, take a look at the following example:</p>
<p>Let&#8217;s assume you have a monkey that is an object of class <em>Monkey</em>, and a banana that is an object of class <em>Banana</em>. If both objects begin to collide, the following selectors will be called by GBox2D:</p>

<div class="wp_codebox"><table><tr id="p7261104"><td class="code" id="p7261code104"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">&#91;</span>banana beginCollisionWithMonkey<span style="color: #002200;">:</span>collisionA<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>monkey beginCollisionWithBanana<span style="color: #002200;">:</span>collisionB<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>If the collision is released, because the objects do not touch anymore:</p>

<div class="wp_codebox"><table><tr id="p7261105"><td class="code" id="p7261code105"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">&#91;</span>banana endCollisionWithMonkey<span style="color: #002200;">:</span>collisionA<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>monkey endCollisionWithBanana<span style="color: #002200;">:</span>collisionB<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>The collisionA and collisionB parameters contain collision information, e.g., which objects and which fixtures took part in the collision. We will use this information to see if the monkey was hit on his head or body.</p>
<p><em>AppDelegate</em></p>
<p>Things I changed from the AppDelegate contained in the original Box2d project are as follows:</p>
<p>First I set the default pixel format of the frame buffer to RGBA8. That means that the game gets the full 24-bit color depth. I disabled the depth buffer, since we don&#8217;t need it.</p>

<div class="wp_codebox"><table><tr id="p7261106"><td class="code" id="p7261code106"><pre class="objc" style="font-family:monospace;">EAGLView <span style="color: #002200;">*</span>glView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>EAGLView viewWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>window bounds<span style="color: #002200;">&#93;</span>
	pixelFormat<span style="color: #002200;">:</span>kEAGLColorFormatRGBA8
	depthFormat<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span>
	<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>The next important thing to set is pre-multiplied alpha. This is because we use pre-multiplied PVR images created with TexturePacker. If we don&#8217;t set pre-multiplied alpha, our images will have dark borders.</p>

<div class="wp_codebox"><table><tr id="p7261107"><td class="code" id="p7261code107"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">&#91;</span>CCTexture2D PVRImagesHavePremultipliedAlpha<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Since we&#8217;re going to use the random generator to select objects to drop, we have to seed it. The best way to do this is using time(). If you forget to seed the random generator, you will still get random numbers – but they will be the same with every start of the game.</p>

<div class="wp_codebox"><table><tr id="p7261108"><td class="code" id="p7261code108"><pre class="objc" style="font-family:monospace;">    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/srand.html"><span style="color: #a61390;">srand</span></a> <span style="color: #002200;">&#40;</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span style="color: #a61390;">time</span></a> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>When the initialization is done, the GameLayer scene is started:</p>

<div class="wp_codebox"><table><tr id="p7261109"><td class="code" id="p7261code109"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span> runWithScene<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>GameLayer scene<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p><em><em>GameLayer</em></em></p>
<p>The GameLayer is a simple class derived from CCLayer. In this state it consists of an empty init function&#8230;</p>

<div class="wp_codebox"><table><tr id="p7261110"><td class="code" id="p7261code110"><pre class="objc" style="font-family:monospace;">    <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>
    	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#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></pre></td></tr></table></div>

<p>&#8230;and a static selector that wraps the CCLayer into a CCScene to hand over to the Director:</p>

<div class="wp_codebox"><table><tr id="p7261111"><td class="code" id="p7261code111"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>CCScene <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> scene
    <span style="color: #002200;">&#123;</span>
    	<span style="color: #11740a; font-style: italic;">// 'scene' is an autorelease object.</span>
    	CCScene <span style="color: #002200;">*</span>scene <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCScene node<span style="color: #002200;">&#93;</span>;
&nbsp;
    	<span style="color: #11740a; font-style: italic;">// 'layer' is an autorelease object.</span>
    	GameLayer <span style="color: #002200;">*</span>layer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>GameLayer node<span style="color: #002200;">&#93;</span>;
&nbsp;
    	<span style="color: #11740a; font-style: italic;">// add layer as a child to scene</span>
    	<span style="color: #002200;">&#91;</span>scene addChild<span style="color: #002200;">:</span> layer<span style="color: #002200;">&#93;</span>;
&nbsp;
    	<span style="color: #11740a; font-style: italic;">// return the scene</span>
    	<span style="color: #a61390;">return</span> scene;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p><em><em>Audio Resources</em></em></p>
<p>I got the theme music, tafi-maradi-loop.caf, from <a href="“http://incompetech.com/”">http://incompetech.com/</a></p>
<p>Since I knew I wanted to loop the music, I made things easier on myself and chose a theme without a vocal track.</p>
<p>The sounds effects for the objects were obtained from <a href="“http://soundbible.com”"> http://soundbible.com</a> (thanks to Mike Koenig). Some of them were created using <a href="“http://thirdcog.eu/apps/cfxr”">cfxr</a>.</p>
<p>All sounds and the music files were converted to caf format. See Ray&#8217;s <a href="http://www.raywenderlich.com/233/audio-101-for-iphone-developers-converting-and-recording">Audio 101 tutorial</a> for more information.</p>
<p>If you compile and run the project, you will simply see a black screen. So let&#8217;s add some content to that screen now!</p>
<h2>Basic Setup in Xcode</h2>
<p>The goals for this section of the tutorial are to set up the basic game layers and backgrounds, and to set up the physics engine.</p>
<p>Before we dive into the code, there&#8217;s one more thing to do: add the resources we created in PhysicsEditor. Remember these?</p>
<ul>
<li>background.plist</li>
<li>background.pvr.ccz</li>
<li>background-hd.plist</li>
<li>background-hd.pvr.ccz</li>
<li>jungle.plist</li>
<li>jungle.pvr.ccz</li>
<li>jungle-hd.plist</li>
<li>jungle-hd.pvr.ccz</li>
<li>shapes.plist</li>
</ul>
<p>Add the above files to the Resources folder in your Xcode project by Control-clicking on the Resources folder inside Xcode, selecting Add Files To “MonekyJump” and then selecting the files listed above from the project Resourcess folder.</p>
<p>Now for the code. First, we need to create a Floor class to represent the floor for the game. Add Floor.h and Floor.mm files to your project by creating a new file with the iOS\Cocoa Touch\Objective-C class template. Name the class Floor, and make it a subclass of GB2Sprite. And don&#8217;t forget to change the extension for Floor.m to .mm once it has been created.</p>
<p><em>Floor.h</em> simply contains this:</p>

<div class="wp_codebox"><table><tr id="p7261112"><td class="code" id="p7261code112"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#pragma once</span>
&nbsp;
    <span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GB2Sprite.h&quot;</span>
&nbsp;
    <span style="color: #a61390;">@interface</span> Floor <span style="color: #002200;">:</span> GB2Sprite
    <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>Floor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> floorSprite;
&nbsp;
    <span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And <em>Floor.mm</em> this:</p>

<div class="wp_codebox"><table><tr id="p7261113"><td class="code" id="p7261code113"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;Floor.h&quot;</span>
&nbsp;
    <span style="color: #a61390;">@implementation</span> Floor
&nbsp;
    <span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>Floor<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> floorSprite
    <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> initWithStaticBody<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;grassfront&quot;</span> spriteFrameName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;floor/grassfront.png&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>The only remarkable line of code here is the call to the initWithStaticBody selector. This makes our object a static object – one that isn&#8217;t moved by the physics engine. It initializes the body&#8217;s shape using a shape from the shapes.plist file with the name “grassfront.”</p>
<p>It also uses a sprite image with the name floor/grassfront.png that is taken from jungle.plist.</p>
<p>Why did we derive this class from GB2Sprite instead of simply using a GB2Sprite directly? The answer is GBox2D&#8217;s collision handling, which uses the name of the class to call appropriate selectors on the colliding objects. Since we want to know when something collides with the floor, the class name for the floor object must be distinguishable from other GB2Sprite objects.</p>
<p>The next thing to do is update the GameLayer. Add some instance variables to hold the required objects in <em>GameLayer.h</em>:</p>

<div class="wp_codebox"><table><tr id="p7261114"><td class="code" id="p7261code114"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">@interface</span> GameLayer <span style="color: #002200;">:</span> CCLayer
    <span style="color: #002200;">&#123;</span>
        CCSprite <span style="color: #002200;">*</span>background;                   <span style="color: #11740a; font-style: italic;">// weak reference</span>
        CCSprite <span style="color: #002200;">*</span>floorBackground;              <span style="color: #11740a; font-style: italic;">// weak reference</span>
        CCSpriteBatchNode<span style="color: #002200;">*</span> objectLayer;         <span style="color: #11740a; font-style: italic;">// weak reference</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We will store the objects as weak references – that is, without increasing the retain count for each object.</p>
<p>You do not need to worry about the CCSprites being deleted. They will be added as children of the CCLayer and thus have a retain count of at least 1. This is done because otherwise we will be caught in a retain cycle and not able to free the memory allocated by these objects.</p>
<p>Now fill <em>GameLayer.mm</em>&#8216;s <em>init</em> selector with content. First, load the sprite sheets:</p>

<div class="wp_codebox"><table><tr id="p7261115"><td class="code" id="p7261code115"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCSpriteFrameCache sharedSpriteFrameCache<span style="color: #002200;">&#93;</span> addSpriteFramesWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;jungle.plist&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCSpriteFrameCache sharedSpriteFrameCache<span style="color: #002200;">&#93;</span> addSpriteFramesWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;background.plist&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Next, load the physics shapes into the GB2ShapeCache:</p>

<div class="wp_codebox"><table><tr id="p7261116"><td class="code" id="p7261code116"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GB2ShapeCache sharedShapeCache<span style="color: #002200;">&#93;</span> addShapesWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;shapes.plist&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Then, set up the layers. We&#8217;ll divide the game into the following layers:</p>
<ul>
<li><em>Background layer:</em> The jungle image</li>
<li><em>Floor background:</em>  A single sprite with the tall grass</li>
<li><em>Object layer:</em>  Contains all the items and the monkey</li>
<li><em>Debug draw layer</em> Activated as needed</li>
<li><em>Hud layer</em>  Has the score and live energy indicators (to be added later)</li>
</ul>
<p><img class="size-full wp-image-7463" style="background: transparent;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/layers.png" alt="" width="642" height="603" /></p>
<p>Add the code to create the basic background and floor background layers to <em>init</em>:</p>

<div class="wp_codebox"><table><tr id="p7261117"><td class="code" id="p7261code117"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// Setup background layer</span>
    background <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSprite spriteWithSpriteFrameName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;jungle.png&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>background z<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    background.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>;
    background.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>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Setup floor background</span>
    floorBackground <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSprite spriteWithSpriteFrameName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;floor/grassbehind.png&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>floorBackground z<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
    floorBackground.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>;
    floorBackground.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>;</pre></td></tr></table></div>

<p>Then, add the object layer. This will be a sprite batch node to speed up rendering of the objects:</p>

<div class="wp_codebox"><table><tr id="p7261118"><td class="code" id="p7261code118"><pre class="objc" style="font-family:monospace;">	objectLayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSpriteBatchNode batchNodeWithFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;jungle.pvr.ccz&quot;</span> capacity<span style="color: #002200;">:</span><span style="color: #2400d9;">150</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>objectLayer z<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>And finally, the debug draw layer:</p>

<div class="wp_codebox"><table><tr id="p7261119"><td class="code" id="p7261code119"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">// add the debug draw layer, uncomment this if something strange happens ;)</span>
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GB2DebugDrawLayer alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> z<span style="color: #002200;">:</span><span style="color: #2400d9;">30</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>If you want to disable the debug drawing, simply comment out the second line. If enabled, the physics shapes will be drawn over the sprites, allowing you to see where collisions happen and if all shapes are properly aligned.</p>
<p>Next, add the floor object as the child of the object layer. Include Floor.h at the top of <em>GameLayer.mm</em>:</p>

<div class="wp_codebox"><table><tr id="p7261120"><td class="code" id="p7261code120"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;Floor.h&quot;</span></pre></td></tr></table></div>

<p>Then add the floor object at the end of <em>init</em>:</p>

<div class="wp_codebox"><table><tr id="p7261121"><td class="code" id="p7261code121"><pre class="objc" style="font-family:monospace;">    <span style="color: #002200;">&#91;</span>objectLayer addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Floor floorSprite<span style="color: #002200;">&#93;</span> ccNode<span style="color: #002200;">&#93;</span> z<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>There isn&#8217;t anything more we have to do to add the objects to the physics world – everything else is covered inside Gbox2D!</p>
<p>Compile and run the project in the iPhone simulator. (By the way, the project in its current state is available in the folder called <em>2-BasicLayerSetup</em>.) You should see something similar to this:</p>
<p><img class="aligncenter size-full wp-image-7725" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/5-02-s01.png" alt="" width="700" height="372" /></p>
<p>Nice – now let&#8217;s add some action to the game!</p>
<h2>Dropping Objects</h2>
<p>We have two related goals for this section of the tutorial: make our objects drop from the sky and add our sound effects.</p>
<p>Our base class for all the dropping objects will be called Object. It will handle the sound and some basic collision detection. We will derive other sub-classes later on in the tutorial from the Object class.</p>
<p>First, create a new file with the iOS\Cocoa Touch\Objective-C class template. Name the class Object, and make it a subclass of GB2Sprite. (And remember to change the extension for Object.m to .mm)</p>
<p>Object is a simple class, derived from GB2Sprite. This means that it comes with physics and graphical capabilities built in.</p>
<p>To make our lives easier, I&#8217;ve named the sound files in the same way as the physics sprites and the images from the sprite sheet. This allows us to simply use the object&#8217;s name to create the right shape and sound when needed. You&#8217;re welcome! </p>
<p>In order for this to work, we need a property named objName &#8211; objName is passed into the initWithObject selector and stored as part of the class.</p>
<p>RandomObject is a factory method that creates a random object and hands over the right object name upon creation.</p>
<p>Paste this code into <em>Object.h</em>:</p>

<div class="wp_codebox"><table><tr id="p7261122"><td class="code" id="p7261code122"><pre class="objc" style="font-family:monospace;">   <span style="color: #6e371a;">#pragma once</span>
&nbsp;
    <span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GB2Sprite.h&quot;</span>
&nbsp;
    <span style="color: #a61390;">@interface</span> Object <span style="color: #002200;">:</span> GB2Sprite
    <span style="color: #002200;">&#123;</span>
        <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>objName; <span style="color: #11740a; font-style: italic;">// type of the object</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>retain, nonatomic<span style="color: #002200;">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>objName;
&nbsp;
    <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>objName;
    <span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>Object<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> randomObject;
&nbsp;
    <span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Let&#8217;s now go to <em>Object.mm</em>. Start with some needed imports and with synthesizing the objName property.</p>

<div class="wp_codebox"><table><tr id="p7261123"><td class="code" id="p7261code123"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;Object.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GB2Contact.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;SimpleAudioEngine.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GMath.h&quot;</span>
&nbsp;
    <span style="color: #a61390;">@implementation</span> Object
&nbsp;
    <span style="color: #a61390;">@synthesize</span> objName;</pre></td></tr></table></div>

<p>GMath.h contains some helper functions – for example, gFloatRand, a ranged floating point random number generation.</p>
<p>Next, add the init selector and instantiate the physics object. You can use the object&#8217;s name as it is to instantiate the physics shape. For the sprite frame name, you&#8217;ll need to add the folder&#8217;s name (which is object) and the .png extension. Store the objName in the property – we&#8217;ll need it during collision detection to play the sound effect.</p>

<div class="wp_codebox"><table><tr id="p7261124"><td class="code" id="p7261code124"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theObjName
<span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithDynamicBody<span style="color: #002200;">:</span>theObjName
              spriteFrameName<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;objects/%@.png&quot;</span>, theObjName<span style="color: #002200;">&#93;</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>
        self.objName <span style="color: #002200;">=</span> theObjName;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the dealloc selector, simply release the objName property and call super dealloc:</p>

<div class="wp_codebox"><table><tr id="p7261125"><td class="code" id="p7261code125"><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> dealloc
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>objName 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></pre></td></tr></table></div>

<p>The next thing to add is our static factory method, which will simply create a random object. I decided to use a simple switch-case statement for this. The reason is that we&#8217;ll need to create special classes for banana and banana bunch later on. These two objects get only one case entry, while the other objects get three each, so that there&#8217;s a higher probability they appear more often.</p>
<p>Switch case constructs are quite efficient (usually implemented by the compiler using a jump table). You might save some CPU cycles by using an array with the names instead, but since the routine will be called once in a second, our way is fine.</p>

<div class="wp_codebox"><table><tr id="p7261126"><td class="code" id="p7261code126"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>Object<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> randomObject
<span style="color: #002200;">&#123;</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>objName;
    <span style="color: #a61390;">switch</span><span style="color: #002200;">&#40;</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/rand.html"><span style="color: #a61390;">rand</span></a><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">%</span> <span style="color: #2400d9;">18</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">case</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;banana&quot;</span>;
            <span style="color: #a61390;">break</span>;
&nbsp;
        <span style="color: #a61390;">case</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bananabunch&quot;</span>;
            <span style="color: #a61390;">break</span>;
&nbsp;
        <span style="color: #a61390;">case</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">5</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;backpack&quot;</span>;
            <span style="color: #a61390;">break</span>;
&nbsp;
        <span style="color: #a61390;">case</span> <span style="color: #2400d9;">6</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">7</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">8</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;canteen&quot;</span>;
            <span style="color: #a61390;">break</span>;
&nbsp;
        <span style="color: #a61390;">case</span> <span style="color: #2400d9;">9</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">10</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">11</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hat&quot;</span>;
            <span style="color: #a61390;">break</span>;
&nbsp;
        <span style="color: #a61390;">case</span> <span style="color: #2400d9;">12</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">13</span><span style="color: #002200;">:</span> <span style="color: #a61390;">case</span> <span style="color: #2400d9;">14</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;statue&quot;</span>;
            <span style="color: #a61390;">break</span>;
&nbsp;
        <span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
            objName <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pineapple&quot;</span>;
            <span style="color: #a61390;">break</span>;
    <span style="color: #002200;">&#125;</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> initWithObject<span style="color: #002200;">:</span>objName<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Finally, add the closing end to the file:</p>

<div class="wp_codebox"><table><tr id="p7261127"><td class="code" id="p7261code127"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Now switch to <em>GameLayer.h</em> and add a forward declaration for the object class, directly after the #import statement:</p>

<div class="wp_codebox"><table><tr id="p7261128"><td class="code" id="p7261code128"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span>
    <span style="color: #a61390;">@class</span> Object;</pre></td></tr></table></div>

<p>Add these new members to the GameLayer class:</p>

<div class="wp_codebox"><table><tr id="p7261129"><td class="code" id="p7261code129"><pre class="objc" style="font-family:monospace;">    ccTime nextDrop;    <span style="color: #11740a; font-style: italic;">// Will keep the time until the next drop.</span>
    ccTime dropDelay;     <span style="color: #11740a; font-style: italic;">// The delay between two drops.</span>
    Object <span style="color: #002200;">*</span>nextObject;   <span style="color: #11740a; font-style: italic;">// Contains a reference to the next item to drop.</span></pre></td></tr></table></div>

<p>Switch to <em>GameLayer.mm</em> and add an import of Object.h to the imports at the start of the file. Also import GMath.h:</p>

<div class="wp_codebox"><table><tr id="p7261130"><td class="code" id="p7261code130"><pre class="objc" style="font-family:monospace;">    <span style="color: #6e371a;">#import &quot;Object.h&quot;</span>
    <span style="color: #6e371a;">#import &quot;GMath.h&quot;</span></pre></td></tr></table></div>

<p>Initialize the new variables at the end of the init selector, and schedule an update selector with every frame update:</p>

<div class="wp_codebox"><table><tr id="p7261131"><td class="code" id="p7261code131"><pre class="objc" style="font-family:monospace;">    nextDrop <span style="color: #002200;">=</span> 3.0f;  <span style="color: #11740a; font-style: italic;">// drop first object after 3s</span>
    dropDelay <span style="color: #002200;">=</span> 2.0f; <span style="color: #11740a; font-style: italic;">// drop next object after 1s                </span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self scheduleUpdate<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>The last line will call a selector called &#8220;update&#8221; for every frame. The parameter to this selector is the time elapsed since the selector was last called. Add the update method right after <em>init</em>:</p>

<div class="wp_codebox"><table><tr id="p7261132"><td class="code" id="p7261code132"><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> update<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: #11740a; font-style: italic;">// 1 - drop next item</span>
        nextDrop <span style="color: #002200;">-=</span> dt;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>nextDrop &lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// 2 - do we have the next object?</span>
            <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>nextObject<span style="color: #002200;">&#41;</span>
            <span style="color: #002200;">&#123;</span>
                <span style="color: #11740a; font-style: italic;">// 3 - set the object as active, making it drop</span>
                <span style="color: #002200;">&#91;</span>nextObject setActive<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
                <span style="color: #11740a; font-style: italic;">// 4 - set next drop time</span>
                nextDrop <span style="color: #002200;">=</span> dropDelay;
                <span style="color: #11740a; font-style: italic;">// reduce delay to the drop after this</span>
                <span style="color: #11740a; font-style: italic;">// this will increase game difficulty</span>
                dropDelay <span style="color: #002200;">*=</span> 0.98f;            
&nbsp;
            <span style="color: #002200;">&#125;</span>
&nbsp;
            <span style="color: #11740a; font-style: italic;">// 5 - create new random object</span>
            nextObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Object randomObject<span style="color: #002200;">&#93;</span>;
            <span style="color: #11740a; font-style: italic;">// but keep it disabled</span>
            <span style="color: #002200;">&#91;</span>nextObject setActive<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">// 6 - set random position</span>
            <span style="color: #a61390;">float</span> xPos <span style="color: #002200;">=</span> gFloatRand<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">40</span>,<span style="color: #2400d9;">440</span><span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">float</span> yPos <span style="color: #002200;">=</span> <span style="color: #2400d9;">400</span>;
            <span style="color: #002200;">&#91;</span>nextObject setPhysicsPosition<span style="color: #002200;">:</span>b2Vec2FromCC<span style="color: #002200;">&#40;</span>xPos, yPos<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">// 7 - add it to our object layer</span>
            <span style="color: #002200;">&#91;</span>objectLayer addChild<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>nextObject ccNode<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s go through the above code section by section.</p>
<ol>
<li>This section simply reduces the time interval since update was last called from nextDrop. If nextDrop falls below 0, it&#8217;s time to create a new item to drop. </li>
<li>If the nextDrop timer runs out, this section checks if there is already an object stored in nextObject. </li>
<li>If so, it&#8217;s set to to active in here. Setting the object to active gives the physics engine control over the object.</li>
<li>This section sets the time until the next drop to the current drop delay, and reduces the drop delay by 2%, making the game a bit more difficult with each dropped item.</li>
<li>This section creates a new object to drop using our factory method in Object &#8211; randomObject &#8211; and sets the object to inactive, which keeps the object from dropping and participating in the physics simulation.</li>
<li>This section gives the object a random position. The screen is 480pt wide (Cocos2d uses points as the base unit, with 1 pt equivalent to 1 pixel on &#8220;old&#8221; devices and 2 pixels on retina display devices). The code ensures that the object&#8217;s position is somewhere between 40 and 440 points.  The section also sets the y-coordinate to 400 for the starting position so that the object will start offscreen from the top of the screen. The b2Vec2FromCC method is used to create a box2db2Vec2 from the point coordinates. B2Vec2FromCC transforms Cocos2d&#8217;s points to Box2d&#8217;s meters-based values.
<li>Finally, this section adds the object to the object layer.</li>
</ol>
<p>Compile and run! You should see something similar to the following but of course, with different items. The items look a bit blurry since debug drawing is still enabled:</p>
<p><img class="aligncenter size-full wp-image-7739" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/5-03-s02.png" alt="" width="698" height="372" /></p>
<p>Disable the debug draw layer as follows by commenting out the relevant line in <em>GameLayer.mm</em>:</p>

<div class="wp_codebox"><table><tr id="p7261133"><td class="code" id="p7261code133"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">//        [self addChild:[[GB2DebugDrawLayer alloc] init] z:30];</span></pre></td></tr></table></div>

<p>Now your game should look much nicer:<br />
<img class="aligncenter size-full wp-image-7740" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/5-04-s02.png" alt="" width="700" height="372" /></p>
<p>Notice how the items can tumble out-of-screen to the left and right? The goal of the game is to let the items pile up, so we need to add a wall on each side of the screen.</p>
<p>To do this, simply create two new GB2Node objects. They will be out of the screen to the left and right.</p>
<p>Since GB2Nodes add themselves to the current physics simulation, you don&#8217;t need to add them manually. They are not represented graphically, so creating them will suffice.</p>
<p>Add these lines to the <em>init</em> in <em>GameLayer.mm</em>, right after the floor layer:</p>

<div class="wp_codebox"><table><tr id="p7261134"><td class="code" id="p7261code134"><pre class="objc" style="font-family:monospace;">    GB2Node <span style="color: #002200;">*</span>leftWall <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GB2Node alloc<span style="color: #002200;">&#93;</span> initWithStaticBody<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> node<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>leftWall addEdgeFrom<span style="color: #002200;">:</span>b2Vec2FromCC<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> to<span style="color: #002200;">:</span>b2Vec2FromCC<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">10000</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    GB2Node <span style="color: #002200;">*</span>rightWall <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GB2Node alloc<span style="color: #002200;">&#93;</span> initWithStaticBody<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> node<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>rightWall addEdgeFrom<span style="color: #002200;">:</span>b2Vec2FromCC<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> to<span style="color: #002200;">:</span>b2Vec2FromCC<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">10000</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Compile and run. See how the objects are now kept inside the screen by our walls?</p>
<p><img class="aligncenter size-full wp-image-7744" style="background: none;" src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/5-05-s021.png" alt="" width="700" height="372" /></p>
<p>This looks nice, but there is still something missing. I think the objects should make some noise when colliding with each other. Don&#8217;t you agree?</p>
<p>I don&#8217;t want the objects to make sounds all the time, just when they hit each other at a decent speed. So we&#8217;ll check the object&#8217;s velocity, and play a sound only when it collides at a fast enough speed.</p>
<p>Add this code to <em>Object.mm</em>:</p>

<div class="wp_codebox"><table><tr id="p7261135"><td class="code" id="p7261code135"><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> beginContactWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
    <span style="color: #002200;">&#123;</span>
        b2Vec2 velocity <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self linearVelocity<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// play the sound only when the impact is high</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>velocity.LengthSquared<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> &gt; <span style="color: #2400d9;">3.0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// play the item hit sound</span>
            <span style="color: #11740a; font-style: italic;">// pan it depending on the position of the collision</span>
            <span style="color: #11740a; font-style: italic;">// add some randomness to the pitch</span>
            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@.caf&quot;</span>, objName<span style="color: #002200;">&#93;</span>
                        pitch<span style="color: #002200;">:</span>gFloatRand<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.8</span>,<span style="color: #2400d9;">1.2</span><span style="color: #002200;">&#41;</span>
                        pan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>self.ccNode.position.x<span style="color: #002200;">-</span>240.0f<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> 240.0f
                        gain<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span> <span style="color: #002200;">&#93;</span>;    
&nbsp;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The above method must be named beginContactWithObject so that it will be automatically called by GBox2D each time two objects collide.</p>
<p>The linearVelocity method gives us the velocity of the object. Calling Length or LengthSquared on the object delivers the velocity&#8217;s value. I prefer using LengthSquared when comparing with a constant value, since it doesn&#8217;t require calculating the square root of the value.</p>
<p>We&#8217;ll play the sound with a call to SimpleAudioEngine&#8217;s playEffect method. The first parameter is the name of the audio file.</p>
<p>Remember that to make our lives easier I gave the sound effects the same name as the objects and sprites. So you can use the objName we stored earlier to get the right sound file. Use NSString to append .caf to the name.</p>
<p>Add some variation to the pitch by using gFloatRand with 0.8 and 1.2. This will play the sound with some pitch variation. It would be boring if every object made the same sound all the time.</p>
<p>The last trick to apply is to pan the sound&#8217;s source to the position of the object. Pan allows values between -1.0 and 1.0. The object&#8217;s x position (in points) will be somewhere between 0 and 480, so subtracting 240 and dividing by 240 will deliver that range.</p>
<p>If you want objects to make a sound when they hit the floor without rewriting a lot of the code, add the following method which forwards the object-floor collision to the object-object collision to <em>Object.mm</em>:</p>

<div class="wp_codebox"><table><tr id="p7261136"><td class="code" id="p7261code136"><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> beginContactWithFloor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GB2Contact<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self beginContactWithObject<span style="color: #002200;">:</span>contact<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Compile, run and see how the objects drop and make a sound upon collision.</p>
<p>Ah, but there is one more thing I don&#8217;t like about our game right now. The first item drops and then pauses in mid-air while the sound engine is initialized.</p>
<p>This won&#8217;t be a problem once we add the theme music, since the music will initialize the sound engine right away. But if you want to fix this now, first add an import statement to the top of <em>GameLayer.mm</em>:</p>

<div class="wp_codebox"><table><tr id="p7261137"><td class="code" id="p7261code137"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;SimpleAudioEngine.h&quot;</span></pre></td></tr></table></div>

<p>Then, add a call to SimpleAudioEngine&#8217;s shared object inside GameLayer&#8217;s init selector:</p>

<div class="wp_codebox"><table><tr id="p7261138"><td class="code" id="p7261code138"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>The above implementation plays the same basic sound (with pitch variations) for all falling object collisions. If you&#8217;re ambitious, you could play different sounds depending upon the types of objects colliding. That is, play one sound when a canteen hits a canteen, and another when a banana hits a canteen&#8230;</p>
<p>Another way to improve this code would be to vary the gain of the effect with the speed of the collision.</p>
<h2>Where to Go From Here?</h2>
<p>If you don&#8217;t have it already, here is all of the <a href="http://www.raywenderlich.com/downloads/monkeyjump.zip">source code</a> for this tutorial series.</p>
<p>You&#8217;ve now reached the end of Part One of the MonkeyJump tutorial! The project in its current form is available in the source code zip in the folder called 3-DroppingObjects.</p>
<p>Stay tuned for Part Two, where we&#8217;ll add our hero (the monkey), make him move and interact with objects – even make him teleport! Plus injury and death, life-giving bananas, a drop warning light&#8230; there&#8217;s lots of exciting stuff ahead.</p>
<p>Before we get there, let me know if you have any questions or comments about what we&#8217;ve done so far. I&#8217;ll be following the discussions in the forum below.</p>
<p><img src="http://www.raywenderlich.com/downloads/AndreasLoew.jpg" class="photo alignleft"/><i>This is a post by special contributor <a href="http://twitter.com/CodeAndWeb">Andreas Loew</a>, the creator of TexturePacker and Physics Editor.</i></p>
<p><a href="http://www.raywenderlich.com/7261/monkey-jump">How To Build a Monkey Jump Game Using Cocos2D, PhysicsEditor &#038; TexturePacker Part 1</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton7261" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2Frp4rFn&amp;via=rwenderlich&amp;text=How%20To%20Build%20a%20Monkey%20Jump%20Game%20Using%20Cocos2D%2C%20PhysicsEditor%20%26%23038%3B%20TexturePacker%20Part%201&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7261%2Fmonkey-jump" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/cJy-CHdkuPo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/7261/monkey-jump/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/7261/monkey-jump</feedburner:origLink></item>
		<item>
		<title>How to Submit Your App to Apple: From No Account to App Store, Part 2</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/fg_6uqrZR_w/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2</link>
		<comments>http://www.raywenderlich.com/8045/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2#comments</comments>
		<pubDate>Mon, 30 Jan 2012 15:00:59 +0000</pubDate>
		<dc:creator>gambrozio</dc:creator>
				<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=8045</guid>
		<description><![CDATA[TweetThis is a blog post by iOS Tutorial Team member Gustavo Ambrozio, a software engineer with over 20 years experience, [...]<p><a href="http://www.raywenderlich.com/8045/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2">How to Submit Your App to Apple: From No Account to App Store, Part 2</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton8045" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2Fx8Uai5&amp;via=rwenderlich&amp;text=How%20to%20Submit%20Your%20App%20to%20Apple%3A%20From%20No%20Account%20to%20App%20Store%2C%20Part%202&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F8045%2Fhow-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><i>This is a blog post by iOS Tutorial Team member <a href="http://www.raywenderlich.com/about#gustavoambrozio">Gustavo Ambrozio</a>, a software engineer with over 20 years experience, including over three years of iOS experience. He is the  founder of <a href="http://www.codecrop.com/">CodeCrop Software.</a></i></p>
<div id="attachment_8681" class="wp-caption alignright" style="width: 260px"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/OnStore-250x218.png" alt="Learn how to get your app all the way to the App Store!" title="Learn how to get your app all the way to the App Store!" width="250" height="218" class="size-thumbnail wp-image-8681" /><p class="wp-caption-text">Learn how to get your app all the way to the App Store!</p></div>
<p>So you&#8217;re on your way to being a big iOS developer, eh? You&#8217;ve jumped through Apple&#8217;s hoops, paid your $99 fee, and had your brilliant app idea. Maybe you even put it all together and got it running in Xcode. </p>
<p>What, you haven&#8217;t finished coding your app since reading <a href=“http://www.raywenderlich.com/?p=8003”>How to Submit Your App to Apple, Part One</a>? No matter. There are other tutorials that cover that.</p>
<p>This is the tutorial series explaining the basics of how to register as an iOS Developer, and how to interface with Apple&#8217;s portals to test and then submit your apps for approval. </p>
<p>In <a href=“http://www.raywenderlich.com/?p=8003”>Part One</a>, we went through the developer registration process, and we used the iOS Provisional Portal to register our devices and to create the certificates and profiles Apple requires to test and submit our apps.</p>
<p>Now in Part Two, we&#8217;ll configure an app to run on the devices we registered, we&#8217;ll use iTunes Connect to send Apple all the information it needs to pay you for your apps, and finally, we&#8217;ll submit an app to Apple for approval.</p>
<p>Ready to jump from beginning developer to approved seller? (At least virtually.) Then read on!</p>
<p><span id="more-8045"></span></p>
<h2>Getting Started</h2>
<p>OK, that section header is a fake-out. Before we actually get started, I wanted to show you an easier way to get your apps running on your devices than setting up the provisioning profiles manually like we did in part 1.  </p>
<p>Don&#8217;t be mad that I showed you the hard way first&#8230; believe me, you&#8217;re going to need to do it that way someday. You should be relieved you already know how! </p>
<p>The truth is, in some situations, such as when working with more than one developer account, the automatic provisioning I&#8217;m about to show you just does not work. But here&#8217;s how to do it to save yourself time, when you can:</p>
<p>Open Xcode if you haven&#8217;t already. Note that for this tutorial, I&#8217;m using the latest version of Xcode as of the time of writing, version 4.2.</p>
<p>Once in Xcode, open the Organizer (CMD-SHIFT-2). Plug in your iOS device and go to the Devices tab. Your screen should look something like below. Click &#8220;Use for Development”:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/auto011-500x396.png" width="500" height="396" class="alignnone size-large bordered" /></p>
<p>After a few seconds, Xcode will ask you for your credentials. Enter your iOS Developer&#8217;s credentials and click &#8220;Log in&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/auto02-500x399.png" width="500" height="399" class="aligncenter size-large bordered" /></p>
<p>Initially you should not see much:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/auto031-500x396.png" width="500" height="396" class="alignnone size-large bordered" /></p>
<p>Why did Xcode ask for your credentials? Because it&#8217;s going to connect automatically to the Provisioning Portal for you and add the device you&#8217;ve connected to your device list (if it isn&#8217;t there already). Then it will create a provisioning profile called &#8220;iOS Team Provisioning Profile,&#8221; which you should soon see displayed in your Xcode window:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/auto041-500x396.png" width="500" height="396" class="alignnone size-large bordered" /></p>
<p>From now on, whenever you plug in a new device and click &#8220;Use for Development&#8221; in Xcode&#8217;s Organizer, it will add the device to your account and update your Team Provisioning Profile so you don&#8217;t have to go to the provisioning portal all the time. You can then use this provisioning profile when testing your apps in your device.</p>
<p>Pretty easy, eh?  :]</p>
<p>You can turn this behavior on and off by checking or unchecking this box in the organizer:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/auto051-500x398.png" width="500" height="398" class="alignnone size-large bordered" /></p>
<p>BUT: as I said, there will be times you&#8217;ll still need to know how to do these things using the Provisioning Portal, so it&#8217;s a good thing I showed you the hard way first. Some situations in which you&#8217;ll need to know the old-school way:</p>
<ul>
<li>You&#8217;re working for a person or company that has their own account, and you need to use their account instead of your own.</li>
<li>You&#8217;re building a beta version of an app for someone&#8217;s device and you don&#8217;t have access to the device itself, just the UDID.</li>
<li>You need to test push services or in-app purchases. For this you need a provisioning profile with a bundle ID that is not the wildcard *. The Team Provisioning Portal bundle ID is *. As you might remember from the first part of this tutorial, * works with every app, but does not allow you to test push services or in-app purchases, so in this case you need a provisioning profile with a unique bundle ID.</li>
<li>You want to be the cool kid that does not use these &#8220;generic&#8221; profiles and has a profile for every app. OK, maybe I&#8217;m trying to boost my ego with this one&#8230;</li>
</ul>
<p>Well, now I&#8217;ve got this weight off my shoulders let&#8217;s get started with our first real task for this tutorial: preparing the app to run on your device.</p>
<h2>Running Your App on Your Device</h2>
<p>For this section of the tutorial, we need an app to test in your device. I&#8217;ll use <a href="http://itunes.apple.com/app/cute-a-pult/id493436659?ls=1&#038;mt=8">cute-a-pult</a>, the app from my last tutorial, &#8220;<a href="http://www.raywenderlich.com/4756/how-to-make-a-catapult-shooting-game-with-cocos2d-and-box2d-part-1">How To Make a Catapult Shooting Game with Cocos2D and Box2D</a>.&#8221; You can download the final project <a href="https://github.com/gpambrozio/cute-a-pult/zipball/FinalTouches">here</a>.</p>
<p>The version of the app I submitted to the App Store is an improvement on the original project, done with LevelHelper and SpriteHelper. Some day I might even write a tutorial on how I did this.</p>
<p>If you already have an app of your own that&#8217;s ready to test, feel free to use it instead, or use any of the other great apps from <a href="http://www.raywenderlich.com/tutorials">Ray&#8217;s site</a>.</p>
<p>Open the project you chose (mine, your own, or one from Ray&#8217;s site) in Xcode. Open the project navigator and click on the project node of the tree (1), then click on the target (2) and fill out the app version and deployment target (3):</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog109-500x108.png" width="500" height="108" class="aligncenter size-large bordered" /></p>
<p>Note that deployment target is a fancy way of saying &#8220;the minimum version of iOS that your code supports.&#8221;  Be careful though, because if you set this to iOS 3.0 and use an API that is only on iOS 5 without checking first, your app will crash!  The safest thing to do is to test your code on a device running the oldest version of iOS you want to support (I know, easier said than done!)</p>
<p>For this tutorial, just set it to 4.0.</p>
<p>Next, click the &#8220;Info&#8221; tab and change the bundle identifier. This should be the same bundle identifier you used for the App ID you registered in the Provisioning Portal:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog111-500x252.png" width="500" height="252" class="aligncenter size-large bordered" /></p>
<p>Next we need to point Xcode to the provisioning profiles. Click on the &#8220;Build Settings&#8221; tab and search for the word “signing.” In the search results list, click on the cells indicated and choose the correct provisioning profiles from among those presented by Xcode. </p>
<p>You should choose the developer profile for the Debug build, and the distribution profile for the Release build.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog113-500x141.png" width="500" height="141" class="aligncenter size-large bordered" /></a></p>
<p>We&#8217;re almost ready to build. Before we do, choose to build for the device using the scheme chooser:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog115.png" width="449" height="284" class="aligncenter size-large bordered" /></p>
<p>CMD-B to build the project. If there are any problems with your profiles, you&#8217;ll see something like this:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog117.png" width="360" height="72" class="aligncenter size-large bordered" /></p>
<p>Click the log navigator on the left to check the problem. In my case, I created a provisioning profile with the bundle identifier as &#8220;com.codecrop.cute-a-pult,&#8221; but my app has the bundle identifier set to &#8220;com.codecrop.cuteapult&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog119-500x105.png" width="500" height="105" class="aligncenter size-large bordered" /></p>
<p>Let&#8217;s fix this. Click the project navigator (1), click the project tree node (2) and change the bundle identifier (3). Hit CMD-B to build again.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog121-500x121.png" width="500" height="121" class="aligncenter size-large bordered" /></p>
<p>Now everything should be OK:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog123.png" width="356" height="73" class="aligncenter size-large bordered" /></p>
<p>Connect your iOS device to your computer and hit CMD-R to run the app. In a few moments you should see your app running on your device! Finally!</p>
<p>Next I&#8217;ll show you how to submit your project to Apple for approval. For this, we&#8217;ll have to log into iTunes Connect.</p>
<h2>Last Stop: iTunes Connect</h2>
<p>Now it&#8217;s time to get to know iTunes Connect. Go to <a href="https://itunesconnect.apple.com">https://itunesconnect.apple.com</a> and log in with your new iOS Developer credentials. </p>
<p>The first time you connect, you&#8217;ll have to accept the Terms of Service. By now you know the drill: lawyer, checkbox, &#8220;Accept Terms.&#8221; It might be a good idea to keep that lawyer on a retainer&#8230;</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog125-500x463.jpg" width="500" height="463" class="aligncenter size-large bordered" /></p>
<p>There are a lot of things to do here in iTunes Connect. I&#8217;ll show you the basic steps to get your app submitted, but if you want the gritty details you can check the complete (currently almost 200 pages long) <a href="https://itunesconnect.apple.com/docs/iTunesConnect_DeveloperGuide.pdf">iTunes Connect Developer Guide</a>.</p>
<p>First things first: If you want to get paid for your apps, there is a lot of &#8220;paperwork&#8221; you must fill out. It&#8217;s better to get this stuff out of the way right now. It&#8217;s pretty boring and it will vary a lot from country to country but, seriously, if you want to get money from your apps you&#8217;ll have to endure it.</p>
<p>If all of your apps (or at least the first) will be free, you can skip this section and go right to &#8220;Submitting Your App,” below.</p>
<p>Hey, you&#8217;re still here? OK then, click &#8220;Contracts, Tax and Banking.&#8221;</p>
<p>You&#8217;ll have to electronically sign two contracts, one for apps and another for iAds. This is fairly quick, so even if you don&#8217;t plan to use iAds now, you should do it.</p>
<p>Click &#8220;Request&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog127-500x299.jpg" width="500" height="299" class="aligncenter size-large bordered" /></p>
<p>Phone lawyer (told you you should get a retainer), yada, yada, yada, checkbox, &#8220;Submit&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog129-500x290.jpg" width="500" height="290" class="aligncenter size-large bordered" /></p>
<p>You&#8217;re welcome!</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog131-500x149.png" width="500" height="149" class="aligncenter size-large bordered" /></p>
<p>Repeat for the iAd contract:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog133-500x241.png" width="500" height="241" class="aligncenter size-large bordered" /></p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog135-500x291.png" width="500" height="291" class="aligncenter size-large bordered" /></p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog137-500x146.png" width="500" height="146" class="aligncenter size-large bordered" /></p>
<p>Are you bored yet? Hold on, it&#8217;s about to get worse&#8230;</p>
<p>It&#8217;s time to set up your contact information. Click the first &#8220;Set Up&#8221; button, under &#8220;Contact Info&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog139-500x299.png" width="500" height="299" class="aligncenter size-large bordered" /></p>
<p>Click &#8220;Add New Contact&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog141-500x303.png" width="500" height="303" class="aligncenter size-large bordered" /></p>
<p>Add yourself as a new contact. Since no one is looking, give yourself a nice title, like CEO or President. Click &#8220;Save&#8221; when done:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog143-500x223.png" width="500" height="223" class="aligncenter size-large bordered" /></p>
<p>Assuming, as in <a href=“http://www.raywenderlich.com/?p=8003”>Part One</a>, that you are an individual developer and have no employees, you can set all contacts as yourself. Change every drop-down menu and click &#8220;Done&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog145-500x304.png" width="500" height="304" class="aligncenter size-large bordered" /></p>
<p>Now click the second &#8220;Set Up&#8221; button, under &#8220;Bank Info.&#8221; Again, since this is your first time here, you&#8217;ll have to first click &#8220;Add Bank Account&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog147-500x107.png" width="500" height="107" class="aligncenter size-large bordered" /></p>
<p>Choose your bank&#8217;s country. If you choose anything other than United States, be aware that the steps from now on may be different. Click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog149-500x72.png" width="500" height="72" class="aligncenter size-large bordered" /></p>
<p>Your bank&#8217;s ABA Routing number is located on your checks or statements, as indicated below in the orange box. Click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog151-500x174.png" width="500" height="174" class="aligncenter size-large bordered" /></p>
<p>Now you have to look for your bank&#8217;s branch. Look for one in your city but don&#8217;t expect to find an exact match for your branch. Don&#8217;t worry, it does not have to be exact. Click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog153-500x288.png" width="500" height="288" class="aligncenter size-large bordered" /></p>
<p>Check the information (again, don&#8217;t worry if the address is not exact) and click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog155-500x122.png" width="500" height="122" class="aligncenter size-large bordered" /></p>
<p>Now comes the important piece: your account number. This is also found on your checks or statements, as indicated in the blue box below. Fill out all the details for your account and click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog157-500x225.png" width="500" height="225" class="aligncenter size-large bordered" /></p>
<p>Confirm all the information, check the box indicating that it is correct, and click &#8220;Save&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog159-500x235.png" width="500" height="235" class="aligncenter size-large bordered" /></p>
<p>Now you can select this new back account and click &#8220;Save&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog161-500x106.png" width="500" height="106" class="aligncenter size-large bordered" /></p>
<p>We&#8217;re almost done. Click &#8220;Set Up&#8221; under &#8220;Tax Info.” You&#8217;ll at least have to fill out the US tax forms, as they are required for everyone. So go ahead and click to do so:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog163-500x310.png" width="500" height="310" class="aligncenter size-large bordered" /></p>
<p>Fill out all the required information. Though the process should be straightforward for US citizens, you may have questions. Before coughing up money to speak to an accountant, make sure that your question can&#8217;t be answered in the W-9 instructions available for download from the upper-left corner of the page.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog165-500x240.png" width="500" height="240" class="aligncenter size-large bordered" /></p>
<p>Scroll down, fill everything out, and refer back to the W-9 instructions if you need to. Make sure everything is correct and click &#8220;Confirm&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog167-500x281.png" width="500" height="281" class="aligncenter size-large bordered" /></p>
<p>Phew, that&#8217;s done! Now I promise you, the boring part is over. Better yet, if all was done correctly, you can get paid.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog169-500x209.png" width="500" height="209" class="aligncenter size-large bordered" /></p>
<h2>Submitting Your App</h2>
<p>Before we go any further, if you really want to submit an app of your own instead of just following along, there are certain items you&#8217;ll need to get in order. Make sure you have the following ready before you even get started:</p>
<ul>
<li>Your app&#8217;s name</li>
<li>Your app&#8217;s description</li>
<li>Your app&#8217;s icon sized 512px by 512px</li>
<li>At least one app screenshot. These should be sized 320&#215;460 (no status bar); 320&#215;480; 640&#215;920 (retina, no status); 640&#215;960 (retina); or, for landscape, 480&#215;300 (no status bar); 480&#215;320; 960&#215;600 (retina, no status bar); or 960&#215;640 (retina).</li>
</ul>
<p>Once you&#8217;ve got all this assembled, click &#8220;Manage Your Applications&#8221; in iTunes Connect:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog171-500x463.jpg" width="500" height="463" class="aligncenter size-large bordered" /></p>
<p>Click &#8220;Add New App&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog173-500x167.jpg" width="500" height="167" class="aligncenter size-large bordered" /></p>
<p>Choose the primary language of your app and your company name. </p>
<p>Be careful here. This is the only  opportunity you&#8217;ll have to enter your company name. Once you click &#8220;Continue,” it will be very hard to change, so think about how you want your company name to appear in the App Store before proceeding. When you&#8217;re ready, click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog175-500x267.jpg" width="500" height="267" class="aligncenter size-large bordered" /></p>
<p>Fill in the name of your app (it should at least be similar to the app name that appears under your app&#8217;s icon on the device), an SKU number (any number with two or more digits will do) and your bundle ID. The possible bundle IDs are pulled from the app IDs you registered in the Provisioning Portal, so choose the right one for your app. </p>
<p>Click &#8220;Continue&#8221; when ready:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog177-500x238.jpg" width="500" height="238" class="aligncenter size-large bordered" /></p>
<p>Now you have to choose the availability date for your app. You have three choices:</p>
<ul>
<li>You want your app to be available as soon as it&#8217;s approved. If this is the case, just leave the date as  the submission date.</li>
<li>You want your app to be available at a certain date in the future. In this case, enter the date you want your app to be available. Please note that if your app is not approved by this date, then it will be released as soon as it&#8217;s approved. In other words, the date you enter is not a guaranteed release date.</li>
<li>You want to choose the release date once the app has been approved. In this case, change the year to next year, or a date far enough in the future that when your app is approved, you can come back and reenter the date you want.</li>
</ul>
<p>Now choose your price tier or indicate that your app will be free. View the Pricing Matrix for more information about the price tiers.</p>
<p>Check the box if you want your app to be offered at a discount to educational institutions when they purchase multiple copies at once.</p>
<p>Once you&#8217;re done, click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog179-500x187.jpg" width="500" height="187" class="aligncenter size-large bordered" /></p>
<p>Set the version number (should be the same as in your app&#8217;s Xcode project), fill out the description (this is what the users will see in the app store), and select the categories appropriate for your app.</p>
<p>Then consider the keywords.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog181-500x389.jpg" width="500" height="389" class="aligncenter size-large bordered" /></p>
<p>Keywords are very important. Only your app&#8217;s name and keywords are indexed in iTunes&#8217; search engine, so brainstorm words that potential users might think of using to find your app or your competitor&#8217;s apps. You can only change keywords when submitting updates, so choose wisely.</p>
<p>Fill out a copyright notice (generally just the release year and your name or your company&#8217;s name), a contact email and affiliated websites.</p>
<p>The Review Notes can be used for details about your app that you want the reviewer to know. For example, if users need to sign up at a website or even within the app in order to use your app, provide a couple of credentials here to make the reviewer&#8217;s life easier. </p>
<p>Here&#8217;s another example: if your app requires special hardware, explain that in the Review Notes section, and try to have a way for the reviewer to use the app without the hardware.</p>
<p>Scroll down and fill in the required information to determine your app&#8217;s rating. Be honest, as the reviewer can make changes to this section if they disagree.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog183-500x326.jpg" width="500" height="326" class="aligncenter size-large bordered" /></p>
<p>Scroll down some more to submit your graphic assets. You have to upload your app&#8217;s icon and at least one screenshot. If your app is universal, you&#8217;ll need to submit one screenshot for the iPhone and one for the iPad.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog185-500x177.png" width="500" height="177" class="aligncenter size-large bordered" /></p>
<p>After uploading, check order of the screenshots and click &#8220;Save&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog187-500x334.png" width="500" height="334" class="aligncenter size-large bordered" /></p>
<p>The app is almost ready to be submitted. We just need to do one more thing. Click &#8220;View Details&#8221; below the app&#8217;s icon:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog189-500x350.png" width="500" height="350" class="aligncenter size-large bordered" /></p>
<p>On the next screen, click &#8220;Ready to Upload Binary&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog191-500x170.jpg" width="500" height="170" class="aligncenter size-large bordered" /></p>
<p>Answer the question honestly and click &#8220;Save.&#8221; If you&#8217;re using only the default cryptography libraries supplied by Apple&#8217;s API, it should be fine for you to answer &#8220;No&#8221; here.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog193-500x181.jpg" width="500" height="181" class="aligncenter size-large bordered" /></p>
<p>You&#8217;ll be provided with some instructions telling you how to submit your app. Ignore these, as there is an easier way to submit using the latest version of Xcode. Just click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog195-500x126.jpg" width="500" height="126" class="aligncenter size-large bordered" /></p>
<p>Now your app is ready for upload. Let&#8217;s go back to Xcode and finish this journey.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog197-500x138.jpg" width="500" height="138" class="aligncenter size-large bordered" /></p>
<h2>Submitting with Xcode</h2>
<p>Your application should now be tested and ready to roll. All we need to do is submit to Apple for approval. This is surprisingly easy considering what we&#8217;ve been through already.</p>
<p>Go to Xcode and choose iOS Device in the scheme chooser:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog199-500x149.png" width="500" height="149" class="aligncenter size-large bordered" /></p>
<p>Then choose &#8220;Archive&#8221; from under the &#8220;Product&#8221; menu:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog201-500x439.png" width="500" height="439" class="aligncenter size-large bordered" /></p>
<p>If everything is okay with the build, Xcode will open the Organizer window with your app in the Archives tab. You don&#8217;t really need to click &#8220;Validate&#8230;&#8221; here because this will be done when you submit anyway, and Xcode should have already validated against most problems. So save yourself some time and click &#8220;Submit&#8230;&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog203-500x176.png" width="500" height="176" class="aligncenter size-large bordered" /></p>
<p>Enter your iOS Developer credentials and click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog205-500x335.png" width="500" height="335" class="aligncenter size-large bordered" /></p>
<p>Choose the app you want to submit and your submission identity. The identity is the provisioning profile you used to sign the app and should be pre-selected. Make sure everything is correct and click &#8220;Next&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog207-500x335.png" width="500" height="335" class="aligncenter size-large bordered" /></p>
<p>Your app will start uploading to iTunes Connect:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog209-500x336.png" width="500" height="336" class="aligncenter size-large bordered" /></p>
<p>When the upload finishes, you should see this message. Just smile and click &#8220;Finish&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog211-500x338.png" width="500" height="338" class="aligncenter size-large bordered" /></p>
<p>You&#8217;re done now. You should receive a couple of emails from iTunes Connect telling you your app has been uploaded and is waiting for review. If you go back to iTunes Connect you should see your app changing status:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog213-500x342.png" width="500" height="342" class="aligncenter size-large bordered" /></p>
<p>After a few minutes the status should change to &#8220;Waiting For Review&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog215-500x334.png" width="500" height="334" class="aligncenter size-large bordered" /></p>
<p>All you have to do now is wait for your app to be approved! You will receive emails about every change in status your app moves through. Usually after about a week the status should change to &#8220;In Review,&#8221; then to &#8220;Approved.&#8221; Unless you chose a future release date, a few minutes after approval your app&#8217;s status will shift to &#8220;Processing for App Store,&#8221; then a few minutes later to &#8220;Ready for Sale.&#8221;</p>
<p>If your app is not approved, Apple will email you with more information.  They have gotten pretty good at being specific at what the exact problem is and how you can fix it, for the most part.  If this happens, it&#8217;s no big deal &#8211; just fix the problem and re-submit.</p>
<p>Here are the exact dates and times that <a href="http://itunes.apple.com/app/cute-a-pult/id493436659?ls=1&#038;mt=8">cute-a-pult</a> achieved every step of the approval process (all times are UTC):</p>
<ul>
<li>Waiting for Review: Fri, Jan 6, 2012 at 1:51 AM</li>
<li>In Review: Wed, Jan 11, 2012 at 3:43 PM</li>
<li>Processing for App Store: Wed, Jan 11, 2012 at 6:40 PM</li>
<li>Ready for Sale: Wed, Jan 11, 2012 at 6:41 PM</li>
</ul>
<p>I also submitted an update of the app a few days later, with a last scene with the purpose of the app and a few links to my first tutorial post.  Here is the timing for that:</p>
<ul>
<li>Waiting for review: Fri, Jan 13, 2012 at 3:21 PM</li>
<li>In Review: Fri, Jan 20, 2012 at 3:31 PM</li>
<li>Preparing for the app store: Fri, Jan 20, 2012 at 3:53 PM</li>
<li>Ready for sale: Fri, Jan 20, 2012 at 3:55 PM</li>
</ul>
<p>The time for reviews for your apps will vary based on traffic, but in general it takes around 5-10 days for most people.</p>
<h2>Where to Go From Here?</h2>
<p>If you&#8217;ve followed with me all the way, that was a long journey: from no account to App Store! You may not have an app ready to submit yet, but you should feel confident about the submission and approval process once you do.</p>
<p>If you&#8217;ve submitted your first app along with me – congratulations are again in order! Now go get ready for your next update or your next big app!</p>
<p>I look forward to reading and responding to your feedback in the forums.</p>
<p><img src="http://www.raywenderlich.com/downloads/GustavoAmbrozio.jpg" class="photo alignleft" /><i>This is a blog post by iOS Tutorial Team member <a href="http://www.raywenderlich.com/about#gustavoambrozio">Gustavo Ambrozio</a>, a software engineer with over 20 years experience, including over three years of iOS experience. He is the  founder of <a href="http://www.codecrop.com/">CodeCrop Software.</a></i></p>
<p><a href="http://www.raywenderlich.com/8045/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2">How to Submit Your App to Apple: From No Account to App Store, Part 2</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton8045" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2Fx8Uai5&amp;via=rwenderlich&amp;text=How%20to%20Submit%20Your%20App%20to%20Apple%3A%20From%20No%20Account%20to%20App%20Store%2C%20Part%202&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F8045%2Fhow-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/fg_6uqrZR_w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/8045/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/8045/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-2</feedburner:origLink></item>
		<item>
		<title>How to Submit Your App to Apple: From No Account to App Store, Part 1</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/F9UZ7-OvWVU/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1</link>
		<comments>http://www.raywenderlich.com/8003/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1#comments</comments>
		<pubDate>Thu, 26 Jan 2012 15:00:33 +0000</pubDate>
		<dc:creator>gambrozio</dc:creator>
				<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=8003</guid>
		<description><![CDATA[TweetThis is a blog post by iOS Tutorial Team member Gustavo Ambrozio, a software engineer with over 20 years experience, [...]<p><a href="http://www.raywenderlich.com/8003/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1">How to Submit Your App to Apple: From No Account to App Store, Part 1</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton8003" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FAksOgE&amp;via=rwenderlich&amp;text=How%20to%20Submit%20Your%20App%20to%20Apple%3A%20From%20No%20Account%20to%20App%20Store%2C%20Part%201&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F8003%2Fhow-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><i>This is a blog post by iOS Tutorial Team member <a href="http://www.raywenderlich.com/about#gustavoambrozio">Gustavo Ambrozio</a>, a software engineer with over 20 years experience, including over three years of iOS experience. He is the  founder of <a href="http://www.codecrop.com/">CodeCrop Software.</a></i></p>
<div id="attachment_8675" class="wp-caption alignright" style="width: 260px"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/Welcome1-250x230.png" alt="Learn how to join the Apple Developer program and get your app on the App Store!" title="Learn how to join the Apple Developer program and get your app on the App Store!" width="250" height="230" class="size-thumbnail wp-image-8675" /><p class="wp-caption-text">Learn how to join the Apple Developer program and get your app on the App Store!</p></div>
<p>This two-part tutorial series will document every step of becoming an Apple iOS developer &#8211; from literally no account, to published on the App Store!</p>
<p>I&#8217;ll show you how to sign up for Apple&#8217;s iOS Developer Program, how to generate the various certificates needed, how to configure your app, and how to submit to the App Store for approval.</p>
<p>To create this tutorial, I created a completely new App Store account and submitted a new app to the App Store, keeping careful note of each step along the way.  </p>
<p>The app I submitted to the App Store is an improved version of the app I built during my last tutorial series, <a href="http://www.raywenderlich.com/4756/how-to-make-a-catapult-shooting-game-with-cocos2d-and-box2d-part-1">How To Make a Catapult Shooting Game with Cocos2D and Box2D</a>. The app, <a href="http://itunes.apple.com/app/cute-a-pult/id493436659?ls=1&#038;mt=8">cute-a-pult</a>, has already been approved and can be <a href="http://itunes.apple.com/app/cute-a-pult/id493436659?ls=1&#038;mt=8">downloaded for free in the App Store</a>.</p>
<p>For this tutorial, you will need US$99, a valid credit card, and a browser. And it may go without saying, but to develop apps, you&#8217;ll need a Mac computer, preferably with OS X 10.7 Lion installed. </p>
<p>Finally, you will need <a href=“http://developer.apple.com/xcode/”>Xcode</a>, Apple&#8217;s development software. You can download Xcode once you are registered as an Apple iOS developer. Which you will be, after the first few sections of this tutorial!</p>
<p>It will help to approach this tutorial with some patience and perspective. Becoming a registered iOS developer is a lengthy process and is sometimes repetitive. Just remember: in the end you will be able to submit your apps to the App Store for potential fortune and glory!</p>
<p><span id="more-8003"></span></p>
<h2>Getting Started</h2>
<p>The first step on the path to the App Store is to register as an Apple developer. Note that becoming an Apple developer is free and is not the same as becoming an iOS developer, for which you must pay the aforementioned US$99 fee. </p>
<p>You may already have a developer account with Apple. If so, feel free to skip this section and move on to the next.</p>
<p>If you don&#8217;t yet have an Apple developer account, go to the <a href="http://developer.apple.com/devcenter/ios/index.action">iOS Dev Center</a> and click the &#8220;register&#8221; link:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog001-500x227.jpg" width="500" height="227" class="aligncenter size-large bordered" /></p>
<p>Click &#8220;Get Started&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog003-500x233.jpg" width="500" height="233" class="aligncenter size-large bordered" /></p>
<p>On this next page, you can choose to create a completely new Apple ID or use an existing one. If you want, you can save time and use the Apple ID you already use for your iTunes purchases, for example. But I&#8217;d suggest creating a new, clean ID. It helps ensure all your data is correct, and it keeps your personal and professional lives separate. </p>
<p>So, select &#8220;Create an Apple ID&#8221; and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog005-500x317.jpg" width="500" height="317" class="aligncenter size-large bordered" /></p>
<p>Fill in your email, password and security information. Use an email address that you check often, because Apple sends frequent updates on the program and on the status of apps you&#8217;ve submitted for approval.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog007-500x377.jpg" width="500" height="377" class="aligncenter size-large bordered" /></p>
<p>Scroll down, fill out your personal information and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog009-500x300.png" width="500" height="300" class="aligncenter size-large bordered" /></p>
<p>The next page contains questions about your professional profile. There are no right or wrong answers to these questions. You don&#8217;t even necessarily have to select iOS as a platform, though of course it doesn&#8217;t hurt to be accurate:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog011-500x353.png" width="500" height="353" class="aligncenter size-large bordered" /></p>
<p>Scroll down, fill out the rest and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog013-500x305.jpg" width="500" height="305" class="aligncenter size-large bordered" /></p>
<p>The next page is the &#8220;oh, so fun&#8221; legal agreement. Call your lawyer and read the whole thing to him/her on the phone. As soon as you get your lawyer&#8217;s approval, click the checkbox and click &#8220;I Agree.&#8221;</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog015-500x333.jpg" width="500" height="333" class="aligncenter size-large bordered" /></p>
<p>Almost done.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog017-500x264.jpg" width="500" height="264" class="aligncenter size-large bordered" /></p>
<p>Check the email account you entered when registering. You should have received an email just like this. Click the link in the email or just enter the number in the final page of the registration process.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog019-500x331.jpg" width="500" height="331" class="aligncenter size-large bordered" /></p>
<p>Cool, you&#8217;re an Apple developer! That&#8217;s great, but can you start developing apps and submitting them to the App Store? Well, not so fast&#8230; Click &#8220;Continue&#8221; and let&#8217;s give Apple some (more) money.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog021-500x233.jpg" width="500" height="233" class="aligncenter size-large bordered" /></p>
<h2>Joining the iOS Developer Program</h2>
<p>Being a registered Apple developer gives you access to a lot of information, but to be able to send apps to the App Store (and to have access to certain associated portals) you need to enroll in Apple&#8217;s iOS developer program. This is the part that will cost you US$99 per year.</p>
<p>If you followed along with the previous section and clicked “Continue,” you should be in the right place. If you skipped the previous section because you already have an Apple developer account, then go to the <a href="https://developer.apple.com/membercenter/index.action">Developer Member Center</a>, log in, and you&#8217;ll be in sync.</p>
<p>Once logged in, click the &#8220;Join Today&#8221; link on the right of the page:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog023-500x189.jpg" width="500" height="189" class="aligncenter size-large bordered" /></p>
<p>This next page is just a preview of the &#8220;what looks like 3 steps but is really 15&#8243; process. Take a deep breath, make sure you have half an hour to spend, and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog025-500x262.png" width="500" height="262" class="aligncenter size-large bordered" /></p>
<p>Choose to use your existing Apple Developer ID and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog027-500x337.jpg" width="500" height="337" class="aligncenter size-large bordered" /></p>
<p>The next page asks if you want to enroll as an individual or as a company. For this tutorial, I&#8217;m enrolling as an individual, as this is what most of you will do. If you choose to enroll as a company the process won&#8217;t be as easy. You will need to submit a lot more paperwork to prove your involvement in the company.</p>
<p>If you do choose to enroll as a company, read the requirements on the right to make sure you have everything you&#8217;ll need.</p>
<p>Otherwise, just click the &#8220;Individual&#8221; button on the left, and let&#8217;s continue.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog029-500x340.jpg" width="500" height="340" class="aligncenter size-large bordered" /></p>
<p>You&#8217;ll have to provide your newly-created credentials again. Fill out the form and click &#8220;Sign In.”</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog031-500x250.jpg" width="500" height="250" class="aligncenter size-large bordered" /></p>
<p>Enter your billing information to verify your identity. Apple will attempt to confirm this information with your credit card company, so make sure you enter it correctly:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog033-500x329.jpg" width="500" height="329" class="aligncenter size-large bordered" /></p>
<p>Scroll down, fill in the remaining fields and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog035-500x298.jpg" width="500" height="298" class="aligncenter size-large bordered" /></p>
<p>It&#8217;s time to select a program. This tutorial series focuses on the iOS Developer Program, so that is the only option I will select. If you want to develop OS X apps, you can also choose to enroll in the Mac Developer Program, but this will cost you another US$99. (You can always sign up later, so don&#8217;t worry if you aren&#8217;t sure.)</p>
<p>Once you&#8217;ve selected your programs, click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog037-500x327.jpg" width="500" height="327" class="aligncenter size-large bordered" /></p>
<p>Review your information, going back to correct any mistakes if necessary. When you&#8217;re ready to submit, click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog039-500x339.jpg" width="500" height="339" class="aligncenter size-large bordered" /></p>
<p>Next we have yet another &#8220;oh so fun&#8221; license agreement. So call your lawyer again, get the OK to check the box and click &#8220;I Agree&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog041-500x333.jpg" width="500" height="333" class="aligncenter size-large bordered" /></p>
<p><em>Note:</em> <i>The following steps only apply to the US and other countries with online Apple Stores. For countries without online Apple Stores, the process will be slightly different, requiring you to fax your credit card information to Apple. Unfortunately, readers in those countries, you&#8217;ll be on your own for the remainder of this section. So, follow Apple&#8217;s instructions and meet me again in the next section.</i></p>
<p>For everyone still following along, click &#8220;Add to cart&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog043-500x280.jpg" width="500" height="280" class="aligncenter size-large bordered" /></p>
<p>This brings us to the Apple Store to continue the process. Click &#8220;Check Out Now&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog045-500x215.jpg" width="500" height="215" class="aligncenter size-large bordered" /></p>
<p>You&#8217;ll have to log in yet again. Use your newly created Apple ID.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog047-500x346.jpg" width="500" height="346" class="aligncenter size-large bordered" /></p>
<p>You need to enter a “shipping address,” even though Apple is not going to ship you anything. I suggest you use the credit card billing address you entered previously (and will enter again shortly!). Fill out the form and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog049-500x269.jpg" width="500" height="269" class="aligncenter size-large bordered" /></p>
<p>Click &#8220;Continue&#8221; again:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog051-500x303.png" width="500" height="303" class="aligncenter size-large bordered" /></p>
<p>Now enter your billing information and credit card number once again, and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog053-500x369.png" width="500" height="369" class="aligncenter size-large bordered" /></p>
<p>We&#8217;re almost done. The page you are on now might show estimated sales tax, as mine did, which you may or may not be charged (I was not). Once you&#8217;re ready, click &#8220;Place Order Now&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog055-500x308.jpg" width="500" height="308" class="aligncenter size-large bordered" /></p>
<p>You&#8217;re welcome!</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog057-500x310.png" width="500" height="310" class="aligncenter size-large bordered" /></p>
<h2>Now, Let Me In!</h2>
<p>After submitting and paying for your iOS Developer registration, you&#8217;ll need to wait a day or so for Apple to process your order. </p>
<p>If you had to fax your information to Apple because you&#8217;re in a country without an online Apple Store, you&#8217;ll need a little more patience. This was the case for me when I originally signed up, and it took a week for Apple to get back to me.</p>
<p>In either case, eventually you should get an email from Apple like this one:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog059-500x361.jpg" width="500" height="361" class="aligncenter size-large bordered" /></p>
<p>At the same time, you should receive this email:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog061-500x215.jpg" width="500" height="215" class="aligncenter size-large bordered" /></p>
<p>It is as this point that you should <a href=“http://developer.apple.com/xcode/”>download Xcode</a>, the main iOS development tool, if you have not already, using your new iOS Developer credentials. While we will only be using Xcode very briefly in this tutorial, there are many other excellent <a href="http://www.raywenderlich.com/tutorials">tutorials</a> on RayWenderlich.com to teach you how to use it!</p>
<p>Now go to the <a href="http://developer.apple.com/devcenter/ios/index.action">iOS Dev Center</a> and click &#8220;Log In&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog063-500x257.jpg" width="500" height="257" class="aligncenter size-large bordered" /></p>
<p>After providing your credentials, you&#8217;ll be in! Finally!</p>
<p>The iOS Dev Center has a LOT of information. There are programming guides, downloads, documentation, videos, the very helpful developer forum and a support center. </p>
<p>Spend some time exploring to familiarize yourself with what&#8217;s available. Be aware that some of this information might be confidential, especially if it involves beta versions of iOS or Xcode.</p>
<p>In this tutorial, we&#8217;re going to focus on two portals that you&#8217;ll use a lot when developing your apps, the iOS Provisioning Portal and iTunes Connect:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog065-500x322.jpg" width="500" height="322" class="aligncenter size-large bordered" /></p>
<p>But first, a brief introduction to both is in order.</p>
<p><em>iOS Provisioning Portal</em></p>
<p>As we should all know, a non-jailbroken iOS device is only able to run apps approved by Apple and installed through the App Store.</p>
<p>Apple achieves this by requiring that every app run by iOS have a signed Apple Certificate. Apps installed from the App Store come bundled with a certificate, which the system verifies before it allows the app to run. If there&#8217;s no signature or if the signature is invalid, the app won&#8217;t run.</p>
<p>As developers, we need to be able to run our apps on our devices on a regular basis as we&#8217;re developing them. For this we need a way to create and sign our own certificates.</p>
<p>That&#8217;s where the iOS Provisioning Portal comes in. The portal allows you to generate what Apple calls &#8220;profiles.&#8221; Profiles, sometimes called &#8220;code signing identities,&#8221; are files generated by the portal that allow Xcode to sign your apps in a way that your devices&#8217; iOS will identify as valid.</p>
<p>There are two types of profiles: </p>
<ul>
<li><em>Development profiles.</em>  These are tied to specific devices, so the app can only run on those devices.</li>
<li><em>Distribution profiles.</em>  These are used to sign your app before you submit it to Apple for approval. They contain no device-specific information, but you can&#8217;t use them to install apps on any device yourself, because Apple still has to sign the app after the approval process.</li>
</ul>
<p>The Provisioning Portal can also generate push certificates in case your app wants to send push notifications (think: pop-ups sent from your server when something occurs).  </p>
<p><em>iTunes Connect</em></p>
<p>iTunes Connect is the portal you&#8217;ll use to submit your apps. This is where you&#8217;ll register a new app, enter the app&#8217;s description and screenshots, choose the price, and configure game center and in-app purchases.</p>
<p>This is also the portal you&#8217;ll use to agree to new contracts, set your financial data (so you can bank that profit) and check your sales.</p>
<p>We&#8217;ll spend the rest of this tutorial using the Provisioning Portal. Next time, in Part Two, I&#8217;ll introduce you to iTunes Connect.</p>
<h2>Certificates, Device Registration, and Profiles</h2>
<p>In this next part of the tutorial, we&#8217;re going to use the iOS Provisioning Portal to set up the information we need in order to deploy our app to our device (and later the App Store). </p>
<p>Note that there is a simpler way to do this in Xcode called Automatic Device provisioning, that we will cover in part two of the series.  But for now, I&#8217;m going to take you through the process step by step.  You&#8217;ll understand how things work better this way, which is especially important information to know when submitting to the App Store. </p>
<p>If you still have your iOS Dev Center page in front of you, just click the link on the upper right, or go <a href="https://developer.apple.com/ios/manage/overview/index.action">here</a>:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog067-500x377.jpg" width="500" height="377" class="aligncenter size-large bordered" /></p>
<p>There are a lot of things you&#8217;ll need to do from this portal. Some of them you&#8217;ll only have to do once, like generating your certificates and registering your devices. Other things you&#8217;ll have to repeat for every app you make, like generating development and distribution profiles.</p>
<p>First you&#8217;ll need to generate two certificates, one for your development profiles and another for your distribution profiles.</p>
<p>Let&#8217;s click on the &#8220;Certificates&#8221; link to get our certificates:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog069-500x259.jpg" width="500" height="259" class="aligncenter size-large bordered" /></p>
<p>Click &#8220;Request Certificate&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog071-500x164.jpg" width="500" height="164" class="aligncenter size-large bordered" /></p>
<p>The next page explains how to ask for a certificate. I&#8217;ll guide you through the process.</p>
<p>Let&#8217;s leave the browser for a little while and open the Keychain Access app on your Mac. If you don&#8217;t know where it is, search for it using Spotlight:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog073.jpg" width="347" height="199" class="aligncenter size-large bordered" /></p>
<p>Once the program is open, from under the Keychain Access menu choose “Certificate Assistant,” then “Request a Certificate From a Certificate Authority&#8230;”:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog075-500x191.jpg" width="500" height="191" class="aligncenter size-large bordered" /></p>
<p>In the Certificate Assistant window, fill in your email address and name, choose &#8220;Save to disk&#8221; and click &#8220;Continue&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog077-500x352.jpg" width="500" height="352" class="aligncenter size-large bordered" /></p>
<p>Save the file somewhere on your Mac.</p>
<p>Go back to your browser, find this file using the &#8220;Choose file&#8221; button and click &#8220;Submit&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog079-500x340.jpg" width="500" height="340" class="aligncenter size-large bordered" /></p>
<p>Apple will take some time to generate a certificate for you. I&#8217;ve seen it take a few minutes, but sometimes it can take a lot longer. While we wait (the status might say &#8220;Issued,&#8221; but the certificate isn&#8217;t actually ready until a download button appears), let&#8217;s do something else we need to do anyway.</p>
<p>You should see a message at the bottom of the “Current Development Certificates” section that says, “If you do not have the WWDR intermediate certificate installed, click here to download now.” Click that link.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog081-500x159.jpg" width="500" height="159" class="aligncenter size-large bordered" /></p>
<p>This will download a file called AppleWWDRCA.cer. Double-click this file to install it. It will open Keychain Access again in case you closed it. Once the file is installed, keep Keychain Access open because we&#8217;ll need it again in a few minutes.</p>
<p>You&#8217;ll also need a distribution certificate. Click the &#8220;Distribution&#8221; tab and submit the same certificate signing request as for the development certificate. I&#8217;m not a security expert, but I think reusing the same certificate signing request in this case is not a big deal. Feel free to correct me if I&#8217;m wrong.</p>
<p>Again, it sometimes takes a while for the certificate to become available for download. We need both certificates to continue, so if you&#8217;re still waiting, go get a coffee or peruse another great tutorial on the site, and come back later.</p>
<p>Once both are ready, click the &#8220;Download&#8221; button under the Development and Distribution tabs to get both certificates.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog083-500x167.jpg" width="500" height="167" class="aligncenter size-large bordered" /></p>
<p>Double-click both files to install them in your keychain. You should see your certificates as follows:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog085-500x82.png" width="500" height="82" class="aligncenter size-large bordered" /></p>
<p>Now you can close Keychain Access.</p>
<p>Let&#8217;s continue. The next step is to register our devices. On the left-side menu, click “Devices” and then on the right, &#8220;Add Devices&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog087-500x227.jpg" width="500" height="227" class="aligncenter size-large bordered" /></p>
<p>You need to get the UDID of the device(s) you want to use to run your apps. There are many ways to get a device&#8217;s UDID: there are free apps available that will do it for you, or you can use Xcode&#8217;s organizer. I&#8217;ll show you how to get a UDID using iTunes.</p>
<p>Open iTunes and plug the device into your computer. Select the device from the left-side menu. iTunes will display your device name, capacity, version, serial number and phone number. Click on the serial number and it will change to your device&#8217;s UDID:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog089-500x140.jpg" width="500" height="140" class="aligncenter size-large bordered" /></p>
<p>Now just use CMD-C to copy the UDID to your clipboard.</p>
<p>Go back to your browser, enter a device name and paste the UDID into the appropriate field. The device name can be anything you want. To register additional devices, click the &#8220;+&#8221; button. When you&#8217;re done, click &#8220;Submit.”</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog091-500x288.jpg" width="500" height="288" class="aligncenter size-large bordered" /></p>
<p>You can always go back later to register more devices, such a those belonging to friends and beta-testers. Apple allows you to register up to 100 devices per year to your account. (Note that if you register a device and later remove it, it still counts towards your total for the year.)</p>
<p>Now that our device is registered, we need to register an app ID. Every app you build will need its own app ID. Click &#8220;App IDs&#8221; on the left-side menu:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog093-500x171.jpg" width="500" height="171" class="aligncenter size-large bordered" /></p>
<p>You&#8217;ll see a detailed explanation of the app ID concept. In a nutshell, an app ID is a combination of a 10-character &#8220;seed&#8221; prefix generated by Apple, and a bundle ID created by you. Together they create a unique identifier for your app. </p>
<p>Here are some important things to know about app IDs:</p>
<ul>
<li>You can elect to have all of your apps share the same seed prefix, if you want to share keychain information between them. Say you have a suite of apps that all make use of the same website via a login. If the apps share the same seed prefix, and one app saves the user&#8217;s login information to the iOS keychain, any other app in the suite can get this login information from the keychain.</li>
<li>The bundle ID has to be unique for each of your apps. It will be used by Apple&#8217;s push service for in-app purchases and for iCloud storage.</li>
<li>Apple recommends that you use “a reverse-domain name style string” for the Bundle Identifier portion of the app ID. The suggested format is “com.domainname.applicationname.”</li>
<li>Apple does allow you to use an asterisk character to create a “wildcard” bundle ID that can be used across all your apps, but I don&#8217;t recommend this approach.
<p>
<p>
Using an wildcard may make it easier to create provisioning profiles, but you won&#8217;t be able to use push services or in-app purchases. You might not plan to use these services now, but if you change your mind, you won&#8217;t be able to change your app ID without creating a new app.</li>
</ul>
<p>Now that you know all about app IDs, let&#8217;s create one. Click &#8220;New App ID.&#8221;</p>
<p>Fill out the description (usually just your app&#8217;s name) and the bundle identifier. The seed ID will always be your team seed ID. (In the past there was a way to have more than one seed ID, but this seems to have been disabled.)</p>
<p>Remember to use a reverse-domain name style string for your bundle ID. Click &#8220;Submit&#8221; when done:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog095-500x301.jpg" width="500" height="301" class="aligncenter size-large bordered" /></p>
<p>Now we&#8217;re ready for the last step (for now) inside the Provisioning Portal: creating the provisioning and distribution profiles.</p>
<p>Click &#8220;Provisioning&#8221; on the left-side menu and &#8220;New Profile&#8221; on the right:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog097-500x238.jpg" width="500" height="238" class="aligncenter size-large bordered" /></p>
<p>Fill in your app&#8217;s name as the profile name, select the certificate, choose the app ID you just created, and finally select the devices you want to use to test your app. Click &#8220;Submit&#8221;:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog099-500x269.jpg" width="500" height="269" class="aligncenter size-large bordered" /></p>
<p>When you return to this page, the status of your profile will be &#8220;Pending&#8221; as Apple generates the file for you. Usually if you just click the &#8220;Development&#8221; tab again, you&#8217;ll see your status has already changed to &#8220;Active&#8221; and a &#8220;Download&#8221; button has appeared.</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog101-500x141.jpg" width="500" height="141" class="aligncenter size-large bordered" /></p>
<p>Before we download the file, let&#8217;s go ahead and generate the distribution profile. We won&#8217;t actually need the distribution profile until we&#8217;re ready to submit the app for approval, but since we&#8217;re here, let&#8217;s do it now.</p>
<p>Click the &#8220;Distribution&#8221; tab at the top of the page and click &#8220;New Profile.&#8221;</p>
<p>Choose &#8220;App Store&#8221; as the distribution method, enter a profile name (can&#8217;t be the same name you used in the development profile), choose your app ID and click &#8220;Submit.&#8221; </p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog103-500x270.jpg" width="500" height="270" class="aligncenter size-large bordered" /></p>
<p>I won&#8217;t go into detail here about Ad Hoc distribution, since distribution is not the subject of this tutorial. You can learn more about Ad Hoc distribution <a href="https://developer.apple.com/ios/manage/distribution/distribution.action">here</a>.</p>
<p>Your provisioning profile should be ready. Go back to the &#8220;Development&#8221; tab, click &#8220;Download&#8221; and save the file to your computer. Do the same for your distribution profile by clicking the &#8220;Distribution&#8221; tab and clicking &#8220;Download.”</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog105-500x146.jpg" width="500" height="146" class="aligncenter size-large bordered" /></p>
<p>Now find the files you just downloaded on your computer, and double-click them. They should open in Xcode&#8217;s organizer:</p>
<p><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/devprog107-500x218.png" width="500" height="218" class="aligncenter size-large bordered" /></p>
<h2>Where to Go From Here?</h2>
<p>If you completed the process along with me, then you&#8217;ve taken a huge step by becoming an official iOS Developer – congratulations! Even if you&#8217;re still contemplating it, I hope the process is now less intimidating.</p>
<p>In Part Two of this tutorial series, we&#8217;ll have some fun getting our app running on a device and submitting it to the App Store.  And I&#8217;ll show you how you can get your apps running on your device quickly with Xcode&#8217;s Automatic Device Provisioning feature!</p>
<p>If you have any questions or comments about what we&#8217;ve done so far, please join in the forum discussion below!</p>
<p><img src="http://www.raywenderlich.com/downloads/GustavoAmbrozio.jpg" class="photo alignleft" /><br />
<i>This is a blog post by iOS Tutorial Team member <a href="http://www.raywenderlich.com/about#gustavoambrozio">Gustavo Ambrozio</a>, a software engineer with over 20 years experience, including over three years of iOS experience. He is the  founder of <a href="http://www.codecrop.com/">CodeCrop Software.</a></i></p>
<p><a href="http://www.raywenderlich.com/8003/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1">How to Submit Your App to Apple: From No Account to App Store, Part 1</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton8003" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FAksOgE&amp;via=rwenderlich&amp;text=How%20to%20Submit%20Your%20App%20to%20Apple%3A%20From%20No%20Account%20to%20App%20Store%2C%20Part%201&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F8003%2Fhow-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/F9UZ7-OvWVU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/8003/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/8003/how-to-submit-your-app-to-apple-from-no-account-to-app-store-part-1</feedburner:origLink></item>
		<item>
		<title>iOS Apprentice Tutorial 4 Now For Sale!</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/d-Ka-aniyTw/ios-apprentice-tutorial-4-now-for-sale</link>
		<comments>http://www.raywenderlich.com/8595/ios-apprentice-tutorial-4-now-for-sale#comments</comments>
		<pubDate>Tue, 24 Jan 2012 15:00:12 +0000</pubDate>
		<dc:creator>Ray Wenderlich</dc:creator>
				<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[announcements]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=8595</guid>
		<description><![CDATA[TweetIf you signed up for the monthly iOS newsletter on this site, you have received a free copy of the [...]<p><a href="http://www.raywenderlich.com/8595/ios-apprentice-tutorial-4-now-for-sale">iOS Apprentice Tutorial 4 Now For Sale!</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton8595" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FxqQ2yB&amp;via=rwenderlich&amp;text=iOS%20Apprentice%20Tutorial%204%20Now%20For%20Sale%21&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F8595%2Fios-apprentice-tutorial-4-now-for-sale" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><div id="attachment_8596" class="wp-caption alignright" style="width: 260px"><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/iOSApprentice4.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/iOSApprentice4-250x234.png" alt="iOS Apprentice Tutorial 4: StoreSearch now available!" title="iOS Apprentice Tutorial 4: StoreSearch now available!" width="250" height="234" class="size-thumbnail wp-image-8596" /></a><p class="wp-caption-text">iOS Apprentice Tutorial 4: StoreSearch now available!</p></div>
<p>If you signed up for the <a href="http://www.raywenderlich.com/newsletter">monthly iOS newsletter</a> on this site, you have received a free copy of the iOS Apprentice Tutorial 1: Getting Started by <a href="http://www.raywenderlich.com/about#matthijshollemans">Matthijs Hollemans</a>.</p>
<p>In addition to the free tutorial you can get by signing up for the newsletter, you may have purchased tutorials 2 &#038; 3, which are for sale on the <a href="http://www.raywenderlich.com/store">raywenderlich.com store</a>.  </p>
<p>So far, the iOS Apprentice has been a massive success.  We&#8217;ve received a ton of happy feedback from customers, several teachers have decided to base coursework around the series, and the series has had over 1,000 customers so far!</p>
<p>If you enjoyed these tutorials, we have some good news &#8211; today a new iOS Apprentice Tutorial is available for purchase!  Keep reading to find out what it&#8217;s about, how you can get it, and what to do if you previously purchased the iOS Apprentice 1-3 Bundle.</p>
<p><span id="more-8595"></span></p>
<h2>iOS Apprentice Tutorial 4: StoreSearch</h2>
<p>So far in the iOS Apprentice, you learned how to use Xcode, Interface Builder, and Objective-C  to make apps ranging from a very simple game, to Storyboard-driven apps with multiple screens and Core Data models.</p>
<p>In this tutorial, you&#8217;ll take things to the next level by learning how to integrate your apps with web services!  In this tutorial, you&#8217;ll learn how to upload and download data from web services with HTTP requests and how to handle JSON data.</p>
<p>You will build an app that lets you search the iTunes store, that looks something like this:</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/Finished-app-iPhone1.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/Finished-app-iPhone1-500x242.png" alt="Make a web-service enabled app!" title="Make a web-service enabled app!" width="500" height="242" class="size-large wp-image-8597" /></a></p>
<p>In this tutorial you will connect to a web service to pull down a list of items for sale on the iTunes store and their images.  To make navigation easy, you&#8217;ll add search capability to your old friend, the table view.  You&#8217;ll also create an animated popup with extra information when you tap an item in the table.  and when you flip the iPhone over to landscape, the layout of the app will completely change to show the search results in another way.</p>
<p>You&#8217;ll also create an iPad version of the app:</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/Finished-app-iPad.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/Finished-app-iPad-500x413.png" alt="iPad version of app" title="iPad version of app" width="500" height="413" class="alignnone size-large wp-image-8598" /></a></p>
<p>And as you can see, you&#8217;ll learn how to make the app look quite stylish!  :]</p>
<p>Here&#8217;s what you’ll learn in this tutorial:</p>
<ul>
<li>How to use a web service from your apps and how to download images</li>
<li>Integrating third-party libraries such as AFNetworking</li>
<li>View controller containment: how to embed one view controller inside another</li>
<li>The rules for memory management so you can use older code in your ARC-enabled apps</li>
<li>Showing a completely different UI after rotating to landscape</li>
<li>Cool effects with keyframe animations</li>
<li>How to use scroll views and the paging control</li>
<li>Internationalization and supporting multiple languages</li>
<li>Changing the look of navigation bars and other UI elements</li>
<li>Making iPad apps with split-view controller and popovers</li>
<li>Using Ad Hoc distribution for beta testing</li>
<li>And finally, submitting your apps to the App Store!</li>
</ul>
<p>Just like the other tutorials in the iOS Apprentice series, this tutorial has easy to follow step-by-step instructions, and consists of more than 200 pages and 150 illustrations! You also get full source code, image files, and other resources you can re-use for your own projects.</p>
<h2>How Can I Get It?</h2>
<p>If you&#8217;re interested in purchasing the iOS Apprentice 4: StoreSearch, <a href="http://www.raywenderlich.com/store/ios-apprentice">click here</a> to visit its page on the raywenderlich.com store.</p>
<p>Note you have two options: you can purchase the tutorial individually ($24), or purchase all 4 tutorials for $54, which gives you a sweet discount ($18 in savings)!</p>
<h2>What If I Purchased the iOS Apprentice Tutorials 1-3 Bundle?</h2>
<p>If you purchased the iOS Apprentice Tutorials 1-3 Bundle in the past, we have good news for you!</p>
<p>To thank you for supporting the development of the iOS Apprentice and this website, you will get a massive discount on the iOS Apprentice Tutorial 4.  Individually the iOS Apprentice costs $24, but you can get it for just $9!</p>
<p>To get this special discount, check out the <a href="http://www.raywenderlich.com/forums/viewforum.php?f=11">iOS Apprentice private forums</a> for more details.  If you do not yet have access to the iOS Apprentice private forums, you can <a href="http://www.raywenderlich.com/store/ios-apprentice/forum-signup">sign up for access here</a>.  </p>
<p>I am also trying to set up a mailing list for iOS Apprentice customers to make future communication easier, but in the meantime just check out the forums.</p>
<h2>Thank You!</h2>
<p>Again, if you are interested in the iOS Apprentice, just <a href="http://www.raywenderlich.com/store/ios-apprentice">click here</a> to visit its page on the raywenderlich.com store.</p>
<p>Thank you all for your support &#8211; purchasing products like this is what makes everything we do on this site possible.  </p>
<p>If you have any questions or comments, please join the forum discussion below &#8211; Matthijs and I would be glad to help you out!  :]</p>
<p><a href="http://www.raywenderlich.com/8595/ios-apprentice-tutorial-4-now-for-sale">iOS Apprentice Tutorial 4 Now For Sale!</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton8595" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FxqQ2yB&amp;via=rwenderlich&amp;text=iOS%20Apprentice%20Tutorial%204%20Now%20For%20Sale%21&amp;related=rwenderlich&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F8595%2Fios-apprentice-tutorial-4-now-for-sale" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/d-Ka-aniyTw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/8595/ios-apprentice-tutorial-4-now-for-sale/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/8595/ios-apprentice-tutorial-4-now-for-sale</feedburner:origLink></item>
		<item>
		<title>How to Make a Game Like Jetpack Joyride Using LevelHelper &amp; SpriteHelper – Part 4</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/JHSipVB_p4Y/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4</link>
		<comments>http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4#comments</comments>
		<pubDate>Mon, 23 Jan 2012 15:00:17 +0000</pubDate>
		<dc:creator>Bogdan Vladu</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[box2D]]></category>
		<category><![CDATA[cocos2D]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=7358</guid>
		<description><![CDATA[TweetThis is a post by special contributor Bogdan Vladu, an iOS application developer and aspiring game developer living in Bucharest, [...]<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4">How to Make a Game Like Jetpack Joyride Using LevelHelper &#038; SpriteHelper – Part 4</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton7358" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FzdtBZ3&amp;via=rwenderlich&amp;text=How%20to%20Make%20a%20Game%20Like%20Jetpack%20Joyride%20Using%20LevelHelper%20%26%23038%3B%20SpriteHelper%20%E2%80%93%20Part%204&amp;related=vladubogdan&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7358%2Fhow-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%25e2%2580%2593-part-4" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><i>This is a post by special contributor <a href="http://twitter.com/vladubogdan">Bogdan Vladu</a>, an iOS application developer and aspiring game developer living in Bucharest, Romania.</i></p>
<div id="attachment_8542" class="wp-caption alignright" style="width: 260px"><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMouseFinalSmall.jpg"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMouseFinalSmall-250x250.jpg" alt="Create a game like Jetpack Joyride with LevelHelper and SpriteHelper!" title="Create a game like Jetpack Joyride with LevelHelper and SpriteHelper!" width="250" height="250" class="size-thumbnail wp-image-8542" /></a><p class="wp-caption-text">Create a game like Jetpack Joyride with LevelHelper and SpriteHelper!</p></div>
<p>Hello again, and welcome to the fourth and final installment of the tutorial series on LevelHelper and SpriteHelper. I said by the end of this series you would have an exciting game to play, and we&#8217;re almost there!</p>
<p>Last time, in <a href=“http://www.raywenderlich.com/?p=7244”>part three</a>, we came close to completing our game. We fully implemented collisions between the player and the other game elements, enabling the player to die and score points. We also made the player fly and added some nice sounds and animations. </p>
<p>But there are still a few improvements we can make. For one thing, we need a way to keep track of the score, and while we&#8217;re doing that we might as well add some cute moving bunnies that will be worth more points.</p>
<p>Our game looks nice, but I&#8217;ll take you through adding a background layer to give the illusion of a never-ending outdoor scene visible through the windows. We&#8217;ll make the background layer move so that the scene through the windows is always slightly different.</p>
<p>And wouldn&#8217;t it be interesting if we made some of our lasers rotate? Keep reading to complete the game!</p>
<p><span id="more-7358"></span></p>
<h2>Getting Started: Rotating Lasers</h2>
<p>We&#8217;re picking up with the project as we left it at the end of <a href=“http://www.raywenderlich.com/?p=7244”>Part Three</a>. You can download that version of the project <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/RocketMousePart3Finale.zip">here</a>. </p>
<p>The first thing we&#8217;re going to do is make our lasers rotate. We can&#8217;t have this game be too easy! So open up your project in LevelHelper, and make sure you have the latest version of your level open.</p>
<p>The first step is to go to Define Tags and define a new tag. Call it ROTATING_LASERS.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/rotatinglaserstags" rel="attachment wp-att-7365"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/rotatingLasersTags.png" alt="" width="498" height="454" class="alignnone size-full wp-image-7365" /></a></p>
<p>We will use this new tag to reference the lasers we select to rotate (not all of them&#8230; we&#8217;re not that evil, are we?) when we implement the rotation via code.</p>
<p>Now select a couple of the lasers in the level. Make sure the lasers you select have room to rotate. Assign the ROTATING_LASERS tag to these lasers.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/rotlaserstag" rel="attachment wp-att-7366"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/rotLasersTag-500x299.png" alt="" width="500" height="299" class="alignnone size-large wp-image-7366" /></a></p>
<p>Once you&#8217;re done tagging the lasers, save the level and open your Xcode project. Navigate to HelloWorldScene.h and add the following in the class definition:</p>

<div class="wp_codebox"><table><tr id="p7358153"><td class="code" id="p7358code153"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a><span style="color: #002200;">*</span> rotatingLasers;</pre></td></tr></table></div>

<p>Then place the following at the end the retrieveRequiredObjects method in HelloWorldScene.mm:</p>

<div class="wp_codebox"><table><tr id="p7358154"><td class="code" id="p7358code154"><pre class="objc" style="font-family:monospace;">rotatingLasers <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>lh spritesWithTag<span style="color: #002200;">:</span>ROTATING_LASERS<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>rotatingLasers retain<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Here we take reference to all the sprites that have the tag ROTATING_LASERS. Because the array returned by this method is autoreleased, we need to retain it. In order to not forget to release this retained array, go to the dealloc function and add the following before the LevelHelperLoader object release:</p>

<div class="wp_codebox"><table><tr id="p7358155"><td class="code" id="p7358code155"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>rotatingLasers release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Now that we&#8217;ve grabbed the lasers that we want, let&#8217;s make them rotate! Go inside your tick method and write this at the end:</p>

<div class="wp_codebox"><table><tr id="p7358156"><td class="code" id="p7358code156"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>LHSprite<span style="color: #002200;">*</span> laser <span style="color: #a61390;">in</span> rotatingLasers<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>laser transformRotation<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>laser rotation<span style="color: #002200;">&#93;</span><span style="color: #002200;">+</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>If you run the game now, the chosen lasers will rotate, but the player won&#8217;t die when it makes contact with them. This is because we haven&#8217;t registered a collision event for the ROTATING_LASERS tag. To do that, add the following inside the setupCollisionHandling method:</p>

<div class="wp_codebox"><table><tr id="p7358157"><td class="code" id="p7358code157"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>lh registerPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>ROTATING_LASERS idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseLaserCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Running the game now, the lasers will rotate and kill the player just as the non-rotating lasers do. Congrats, you have just added another layer of difficulty to our game. That said, you may want to play-test the game some more at this point to make sure the player can still get through the level!</p>
<h2>Moving Bunnies</h2>
<p>Running and flying over sleeping cats and dogs and dodging rotating lasers is all fun, but let&#8217;s add  another reward component to the game to give it more complexity. I have just the ticket: bunnies. Cute, helpless bunnies that our player can kill for points.</p>
<p>Open SpriteHelper, and go to File\New.  Then open Finder and navigate to the ArtPack folder (download it from <a href=”http://www.raywenderlich.com/?p=6929”>Part One</a> if you don&#8217;t have it),  select the bunnies images and drag them into the SpriteHelper window. Then click the &#8220;Pack Sprites&#8221; button.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/packbunnies" rel="attachment wp-att-7367"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/packBunnies-500x334.png" alt="" width="500" height="334" class="alignnone size-large wp-image-7367" /></a></p>
<p>Now select all the sprites in the list and enable the “Is Sensor” option from the Physic Properties menu. (Note that our other reward component, coins, are also sensors.)</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnyissensor" rel="attachment wp-att-7368"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyIsSensor-500x182.png" alt="" width="500" height="182" class="alignnone size-large wp-image-7368" /></a></p>
<p>Next, create the following two animations using the bunny frames:</p>
<p>Animation Name: BunnyRun<br />
Speed: 0.400<br />
StartAtLaunch: YES<br />
LoopForever: YES<br />
Frames: bunny_1, bunny2</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnyrunanim" rel="attachment wp-att-7369"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyRunAnim.png" alt="" width="239" height="354" class="alignnone size-full wp-image-7369" /></a></p>
<p>Animation Name: BunnyDie<br />
Speed: 0.400<br />
StartAtLaunch: YES<br />
LoopForever: NO<br />
Frames: bunny__die_1, bunny_die_2</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnydieanim" rel="attachment wp-att-7370"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyDieAnim.png" alt="" width="238" height="353" class="alignnone size-full wp-image-7370" /></a></p>
<p>When you&#8217;re done creating the animations, save the scene inside the Images folder of your Xcode project.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/savebunny" rel="attachment wp-att-7371"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/saveBunny-500x342.png" alt="" width="500" height="342" class="alignnone size-large wp-image-7371" /></a></p>
<p>Going back to LevelHelper, we can see the bunny animations inside the Animations section. Select the BunnyRun animation and drag it into your level.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragbunnyinlevel" rel="attachment wp-att-7372"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragBunnyInLevel-500x354.png" alt="" width="500" height="354" class="alignnone size-large wp-image-7372" /></a></p>
<p>We now have a bunny in the level, but it will not be visible because we need to move him with the<br />
parallax.</p>
<p>To add the bunny to the parallax, go to the Parallax tab, select our parallax and add the bunny sprite to it. Set the x component of the movement ratio to 1 so that the bunny moves at the same speed as our level.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnytoparrallax" rel="attachment wp-att-7373"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyToParrallax-332x500.png" alt="" width="332" height="500" class="alignnone size-large wp-image-7373" /></a></p>
<p>If we run the level in Scene Tester we cannot see the bunny. This is because we need to set the batch z order on the bunny image. </p>
<p>Navigate the the Images section and set the batch z on the bunny image to 3. This will put the bunny in front of everything except the player.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnybatchz" rel="attachment wp-att-7374"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyBatchZ-168x500.png" alt="" width="168" height="500" class="alignnone size-large wp-image-7374" /></a></p>
<p>Running the level again in Scene Tester, we can see the bunny moving with the parallax, but the bunny appears to be moving in place. We want the bunny to move from right to left and from left to right. To do this, we have to make him move on a path.</p>
<p>Navigate to the Beziers section and click NEW to create a new bezier that will describe the path of the bunny.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/createbezier" rel="attachment wp-att-7375"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/createBezier-163x500.png" alt="" width="163" height="500" class="alignnone size-large wp-image-7375" /></a></p>
<p>Click within the level view to draw the bunny&#8217;s path, and click the Finish button when you&#8217;re happy with the bezier.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/finishbezier" rel="attachment wp-att-7376"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/finishBezier-500x253.png" alt="" width="500" height="253" class="alignnone size-large wp-image-7376" /></a></p>
<p>Now select the &#8220;Path&#8221; option in the bezier properties to tell LevelHelper to treat that bezier as a path.</p>
<p><em>Note:</em></p>
<ul><i></p>
<li>By default, beziers are a simple line. You can make them true beziers by deselecting &#8220;Line&#8221; in the  properties menu.</li>
<li>You can edit a bezier by pressing the Edit button and then dragging the points.</li>
<li>While editing the bezier, you can create a new point by holding COMMAND and clicking on a line of the bezier.</li>
<li>While editing the bezier you can remove a point by holding ALT (Option) and clicking on a point. </li>
</ul>
<p></i></p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bezierpath" rel="attachment wp-att-7377"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bezierPath-162x500.png" alt="" width="162" height="500" class="alignnone size-large wp-image-7377" /></a></p>
<p>Well, now we have our path defined, but the bunny sprite doesn&#8217;t know that it should move on that path. Let&#8217;s make the bunny aware of this.</p>
<p>Select the bunny, then in the General Properties section, click on Path.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/clickpath" rel="attachment wp-att-7378"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/clickPath.png" alt="" width="354" height="309" class="alignnone size-full wp-image-7378" /></a></p>
<p>In the Path Settings dialogue, select the bezier that describes the path from the list, then make the motion cyclic by selecting &#8220;Cyclic Motion.&#8221;</p>
<p>Set the amount of time you want the bunny to take to move from one end of the bezier to the other. Then select &#8220;Flip Sprite X At Ends&#8221; so that the bunny is always facing in the direction it is moving. Otherwise, it will appear to be moving backwards half the time.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnypath" rel="attachment wp-att-7379"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyPath.png" alt="" width="316" height="222" class="alignnone size-full wp-image-7379" /></a></p>
<p>If you run the level inside Scene Tester now, the bunny should move on the path and also move with the parallax. This is cool, eh? LevelHelper is the only editor that lets you move sprites on a path and  with a continuos scrolling parallax at the same time.</p>
<p>The last step in LevelHelper is to define a new tag called &#8220;BUNNY&#8221; and assign it to the bunny sprite. Once you&#8217;ve done this, save the level.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnytag" rel="attachment wp-att-7380"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyTag.png" alt="" width="405" height="330" class="alignnone size-full wp-image-7380" /></a></p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bunnytagassignemt" rel="attachment wp-att-7381"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bunnyTagAssignemt.png" alt="" width="200" height="196" class="alignnone size-full wp-image-7381" /></a></p>
<p>Time to move back to our Xcode project. We have created a new image: the bunny. So we have to add the new image files to the Images folder inside Xcode.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/addbunnyimage" rel="attachment wp-att-7382"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/addBunnyImage-473x500.png" alt="" width="473" height="500" class="alignnone size-large wp-image-7382" /></a></p>
<p>The contents of your project&#8217;s Images folder should now look like this:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/includedbunnyimage" rel="attachment wp-att-7383"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/includedBunnyImage.png" alt="" width="161" height="168" class="alignnone size-full wp-image-7383" /></a></p>
<p>Now let&#8217;s write some code that will make the bunny die when the player collides with it.</p>
<p>Add the following at the end of the setupCollisionHandling method:</p>

<div class="wp_codebox"><table><tr id="p7358158"><td class="code" id="p7358code158"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>lh registerBeginOrEndCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>BUNNY idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseBunnyCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This informs LevelHelper of the collision callback, but we also need to define the method for the callback. Add the following before the setupCollisionHandling method:</p>

<div class="wp_codebox"><table><tr id="p7358159"><td class="code" id="p7358code159"><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>mouseBunnyCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHContactInfo<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>    
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>playerIsDead<span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
&nbsp;
    LHSprite<span style="color: #002200;">*</span> bunny <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>contact spriteB<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> bunny<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>bunny animationName<span style="color: #002200;">&#93;</span> isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bunnyRun&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>self scoreHitAtPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>bunny position<span style="color: #002200;">&#93;</span> withPoints<span style="color: #002200;">:</span><span style="color: #2400d9;">500</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bunnyHit.wav&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
        <span style="color: #002200;">&#91;</span>bunny startAnimationNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bunnyDie&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>bunny pathNode<span style="color: #002200;">&#93;</span> setPaused<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the above code, we test for a dead player and do nothing if that&#8217;s the case. We then take the bunny sprite from the contact and test if it&#8217;s valid and whether it&#8217;s running the &#8220;bunnyRun&#8221; animation. </p>
<p>If the bunny sprite meets both conditions, we give 500 points to the user and play the bunny hit sound (part of the <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/MusicPack.zip">sound pack</a> downloaded in <a href=“http://www.raywenderlich.com/?p=7244”>Part Three</a> of this series). We then start the bunnyDie animation on the bunny sprite and stop the sprite&#8217;s movement on the path.</p>
<p>If you run the game now, you&#8217;ll see that the bunny dies when he collides with the mouse. But if you loop through the entire level, you&#8217;ll see that the bunny you killed doesn&#8217;t reappear in the level: it stays dead! So let&#8217;s do the same thing we did with the coins in <a href=“http://www.raywenderlich.com/?p=7244”>Part Three</a> and reset the bunny.</p>
<p>Find your spriteInParallaxHasReset method and modify it to look like this:</p>

<div class="wp_codebox"><table><tr id="p7358160"><td class="code" id="p7358code160"><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> spriteInParallaxHasReset<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHSprite<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sprite
<span style="color: #002200;">&#123;</span>    
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>COIN <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>sprite tag<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>sprite setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>BUNNY <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>sprite tag<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>        
        <span style="color: #002200;">&#91;</span>sprite startAnimationNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bunnyRun&quot;</span><span style="color: #002200;">&#93;</span>;        
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sprite pathNode<span style="color: #002200;">&#93;</span> setPaused<span style="color: #002200;">:</span><span style="color: #a61390;">false</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>    
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Above, we simply check for the BUNNY tag, and when we find a sprite that is a bunny, we set its animation to bunnyRun and unpause its path movement.</p>
<p>The complete project up to this point can be downloaded <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/RocketMouseBunny.zip">here</a>.</p>
<h2>Displaying the Score</h2>
<p>We&#8217;re going to implement a dynamic way of showing the score. Instead of having a fixed display at the top or bottom of the screen, we&#8217;re going to show the score in a temporary pop-up window each time the user scores.</p>
<p>Begin by adding the following inside HelloWorldScene.h:</p>

<div class="wp_codebox"><table><tr id="p7358161"><td class="code" id="p7358code161"><pre class="objc" style="font-family:monospace;">CCLabelTTF <span style="color: #002200;">*</span>scoreText;</pre></td></tr></table></div>

<p>Now define this new method somewhere before the init method inside HelloWorldScene.mm:</p>

<div class="wp_codebox"><table><tr id="p7358162"><td class="code" id="p7358code162"><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> setupScore
<span style="color: #002200;">&#123;</span>
    score <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
    CGSize winSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span> winSize<span style="color: #002200;">&#93;</span>;
    scoreText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCLabelTTF labelWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Score: 0&quot;</span> dimensions<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">200</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</span> alignment<span style="color: #002200;">:</span>UITextAlignmentLeft fontName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Arial&quot;</span> fontSize<span style="color: #002200;">:</span><span style="color: #2400d9;">22</span><span style="color: #002200;">&#93;</span>;
    scoreText.color <span style="color: #002200;">=</span> ccWHITE;
    scoreText.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, winSize.height<span style="color: #002200;">-</span><span style="color: #2400d9;">25</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>scoreText z<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here we create a new label and assign it to our scoreText variable. We then add this label to the Cocos layer, in our case &#8220;self.&#8221; And we set the label position to be the top left corner.</p>
<p>Now call this new method inside the init method right after the call to setupAudio:</p>

<div class="wp_codebox"><table><tr id="p7358163"><td class="code" id="p7358code163"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self setupScore<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This is good, but right now we&#8217;re not showing the actual score to the user. We&#8217;re just showing some text. Let&#8217;s make the text change when the user gets points.</p>
<p>Modify your scoreHitAtPosition method to look like this:</p>

<div class="wp_codebox"><table><tr id="p7358164"><td class="code" id="p7358code164"><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>scoreHitAtPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>position withPoints<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>points
<span style="color: #002200;">&#123;</span>
    score <span style="color: #002200;">+=</span> points;
    <span style="color: #002200;">&#91;</span>scoreText setString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Score: %d&quot;</span>, score<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> curScoreTxt <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;+ %d&quot;</span>, points<span style="color: #002200;">&#93;</span>;
    CCLabelTTF <span style="color: #002200;">*</span>curScore <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCLabelTTF labelWithString<span style="color: #002200;">:</span>curScoreTxt fontName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Marker Felt&quot;</span> fontSize<span style="color: #002200;">:</span><span style="color: #2400d9;">24</span><span style="color: #002200;">&#93;</span>;
    curScore.color <span style="color: #002200;">=</span> ccWHITE;
    curScore.position <span style="color: #002200;">=</span> position;
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>curScore z<span style="color: #002200;">:</span><span style="color: #2400d9;">20</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">id</span> opacityAct1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCActionTween actionWithDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span> key<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;opacity&quot;</span> from<span style="color: #002200;">:</span><span style="color: #2400d9;">255</span> to<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">id</span> actionCallFunc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCCallFuncN actionWithTarget<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>removeScoreText<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">id</span> seq <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCSequence actionOne<span style="color: #002200;">:</span>opacityAct1 two<span style="color: #002200;">:</span>actionCallFunc<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>curScore runAction<span style="color: #002200;">:</span>seq<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here we add the new points to the score variable. Then we set the text of our score label to be the newly updated score.</p>
<p>We then create a new label that will be displayed at the position we get in the attribute of this method. As shown in the code, this is the position of the coin or bunny triggering the score update. The coin or bunny vanishes upon collision with the player, and a score display temporarily appears in its place.</p>
<p>We add the new label to the layer (in this case self). We then create two actions. The first action makes the new score label disappear in one second by changing the opacity property of the sprite from 255 (full visibility) to 0 (not visible). The second action calls a new method, removeScoreText.</p>
<p>Lastly, we create a sequence using the two defined actions and run that sequence on the new label sprite.</p>
<p>We still need to define the removeScoreText method called by the second action. Add the following above the scoreHitAtPosition method:</p>

<div class="wp_codebox"><table><tr id="p7358165"><td class="code" id="p7358code165"><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>removeScoreText<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCLabelTTF<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>scoreLabel
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>scoreLabel removeFromParentAndCleanup<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This new method just removes the score display from the layer.</p>
<p>Run the game now and see how nicely this works. Every time the player makes contact with a coin or a bunny, a label pops up showing the updated score, displays for one second, then disappears.</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/ScoreDisplayed.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/ScoreDisplayed.png" alt="Score displayed in Rocket Mouse" title="Score displayed in Rocket Mouse" width="480" height="320" class="alignnone size-full wp-image-8540" /></a></p>
<h2>Adding An Outside Layer</h2>
<p>Now for our last tweak: an “outdoor” scene that will be visible scrolling through the windows in the level, adding a nice sense of depth to the gameplay.</p>
<p>Open SpriteHelper and create a new scene with File\New. Then open Finder, navigate to the ArtPack folder, select the bgWindow, window_background, and window_foreground images, and drag then on the SpriteHelper window.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragoutsidelayer" rel="attachment wp-att-7387"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragOutsideLayer-500x341.png" alt="" width="500" height="341" class="alignnone size-large wp-image-7387" /></a></p>
<p>Press Pack Sprites to pack the new sprites. Because one of the images doesn&#8217;t have alpha, a new window will ask you if you want to crop the image using a selected color. Choose Don&#8217;t Crop.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dontcropart" rel="attachment wp-att-7390"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dontCropArt-500x188.png" alt="" width="500" height="188" class="alignnone size-large wp-image-7390" /></a></p>
<p>Select all the sprites and set the &#8220;No Physics&#8221; option from the Physics tab. Save the scene as &#8220;outside&#8221; and place it in the Images folder.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/saveoutside" rel="attachment wp-att-7391"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/saveOutside-500x341.png" alt="" width="500" height="341" class="alignnone size-large wp-image-7391" /></a></p>
<p>Back in LevelHelper, let&#8217;s create a new level in order to see the steps. With level03 opened, press Command-Shift-S to save the level as a new level. Give it the name level04 and save it inside the Levels folder.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/savelevel" rel="attachment wp-att-7392"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/saveLevel-500x342.png" alt="" width="500" height="342" class="alignnone size-large wp-image-7392" /></a></p>
<p>Now inside the level view, select all the window sprites as well as all the background sprites from under the windows. </p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/removebackground" rel="attachment wp-att-7393"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/removeBackground.png" alt="" width="242" height="420" class="alignnone size-full wp-image-7393" /></a></p>
<p>Delete the sprite by pressing the Delete key or clicking the red minus sign button on the left.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/removeselected" rel="attachment wp-att-7396"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/removeSelected.png" alt="" width="187" height="181" class="alignnone size-full wp-image-7396" /></a></p>
<p>After the removal of these sprites, the scene should look something like this:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/afterremoval" rel="attachment wp-att-7397"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/afterRemoval.png" alt="" width="233" height="430" class="alignnone size-full wp-image-7397" /></a></p>
<p>Double-check that you&#8217;ve removed all of the window and under-window background sprites, and save the scene.</p>
<p>Now filter the outside sprites by checking the “filter” option next to outside.png. This will make it so that we only see the sprites from that image in the list above. Set the batch z order for outside.png to -1 to put all the sprites in outside.png behind everything else.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/filtersprites" rel="attachment wp-att-7398"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/filterSprites-170x500.png" alt="" width="170" height="500" class="alignnone size-large wp-image-7398" /></a></p>
<p>Next, drag the bgWindow sprite and place it in the empty spaces left when you removed the windows and background sprites.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragemptywindow" rel="attachment wp-att-7399"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragEmptyWindow-500x437.png" alt="" width="500" height="437" class="alignnone size-large wp-image-7399" /></a></p>
<p>When you&#8217;re ready, select all the new sprites with the window cut (all bgWindow sprites) and set their Z Order to 1.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/zforwindows" rel="attachment wp-att-7400"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/zForWindows-335x500.png" alt="" width="335" height="500" class="alignnone size-large wp-image-7400" /></a></p>
<p>Add these new sprites to the parallax and set the x component of the movement ratio to 1. Save the level.</p>
<p>Press the Test Level button to run the scene in Scene Tester. You may notice some of the new sprites have “noise.” That&#8217;s because some of the positions of the new sprites aren&#8217;t lining up correctly with the old sprites.</p>
<p>There are two ways to fix this problem:</p>
<ol>
<li>Adjust every mismatch individually by selecting each sprite and moving the sprite using the arrow keys until it lines up correctly with the other sprites.</li>
<li>Select all of the misaligned sprites and set Scale X in the General Properties menu to 1.01, or a value that cancels the noise.</li>
</ol>
<p>Save the level when you&#8217;re satisfied with how the sprites are positioned.</p>
<p>Now that everything is in place, let&#8217;s create a background that will move at a different speed from the rest of the parallax.</p>
<p>First let&#8217;s lock the sprites already in the level so that we don&#8217;t accidentally move them. To do this, select all the sprites from the list on the left, and click the Lock button.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/locksprites" rel="attachment wp-att-7401"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/lockSprites-150x500.png" alt="" width="150" height="500" class="alignnone size-large wp-image-7401" /></a></p>
<p><em>Note:</em> <i>To unlock sprites, select them in the list and press the Lock button again.</i></p>
<p>With the sprites locked, drag the window_background sprite into the scene and place it above the main screen. Make sure that the left vertical border of the window_background sprite is lined up exactly with that of the main screen.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragbg1" rel="attachment wp-att-7402"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragBg1-500x406.png" alt="" width="500" height="406" class="alignnone size-large wp-image-7402" /></a></p>
<p>Now make sure the window_background sprite is selected, and duplicate it to the right until the end of the game world by using the green + button (or pressing Command-D). Make sure that you have the correct anchor point selected.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/clonebg1" rel="attachment wp-att-7403"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/cloneBg1-500x279.png" alt="" width="500" height="279" class="alignnone size-large wp-image-7403" /></a></p>
<p>The level should look like this when you&#8217;re finished:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/levelbg1" rel="attachment wp-att-7404"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/levelBg1-500x77.png" alt="" width="500" height="77" class="alignnone size-large wp-image-7404" /></a></p>
<p>Now select all the new window_background sprites and add them to the parallax. Set the x component of the movement ratio to 0.3.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bg1parrallax" rel="attachment wp-att-7405"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bg1Parrallax-159x500.png" alt="" width="159" height="500" class="alignnone size-large wp-image-7405" /></a></p>
<p>Repeat this process for the window_foreground sprite.</p>
<p>Drag it into the scene and position it above and in alignment with the window_background sprites:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragbg2" rel="attachment wp-att-7406"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragBg2-500x377.png" alt="" width="500" height="377" class="alignnone size-large wp-image-7406" /></a></p>
<p>Clone it until the end of the game world:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/levelbg2" rel="attachment wp-att-7407"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/levelBg2-500x106.png" alt="" width="500" height="106" class="alignnone size-large wp-image-7407" /></a></p>
<p>Add it to the parallax and set the x component of the movement ratio to 0.7:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/bg2parallax" rel="attachment wp-att-7408"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/bg2Parallax-154x500.png" alt="" width="154" height="500" class="alignnone size-large wp-image-7408" /></a></p>
<p>Now select all the window_background sprites and drag them down onto the main scene so that you can see them through the window.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragbg1window" rel="attachment wp-att-7409"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragBg1Window.png" alt="" width="325" height="213" class="alignnone size-full wp-image-7409" /></a></p>
<p>Repeat the process for the window_foreground sprites. Drag all of the sprites down until you can see them through the window.</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/dragbg2window" rel="attachment wp-att-7410"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragBg2Window.png" alt="" width="385" height="291" class="alignnone size-full wp-image-7410" /></a></p>
<p>Save the level when you&#8217;re done.</p>
<p>Running Scene Tester now, you should see the outside world moving at a different speed than the inside world. This makes it seem to the user that they are never seeing quite the same outside image through the window, making the game world feel more three-dimensionally “real.”</p>
<p>The last thing we have to do is add the new level and images to our Xcode project. Drop the new level (“level04”) into the Levels folder in Xcode. Your new Levels folder should look like this:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/resourcelevels" rel="attachment wp-att-7411"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/resourceLevels.png" alt="" width="176" height="88" class="alignnone size-full wp-image-7411" /></a></p>
<p>Then add the new image files (outside.png, outside-hd.png, outside.pshs) to the Images folder. Your new Images folder in Xcode should look like this:</p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/imagesoutsideres" rel="attachment wp-att-7412"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/imagesOutsideRes.png" alt="" width="170" height="226" class="alignnone size-full wp-image-7412" /></a></p>
<p>Inside HelloWorldScene.mm, change the loading level code so that it loads the level04 file:</p>

<div class="wp_codebox"><table><tr id="p7358166"><td class="code" id="p7358code166"><pre class="objc" style="font-family:monospace;">lh <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>LevelHelperLoader alloc<span style="color: #002200;">&#93;</span> initWithContentOfFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;level04&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>There&#8217;s nothing left but to run the game and enjoy!</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMouseFinal.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMouseFinal.png" alt="Rocket Mouse final project!" title="Rocket Mouse final project!" width="480" height="320" class="alignnone size-full wp-image-8541" /></a></p>
<h2>Where to Go From Here?</h2>
<p>Congratulations, you&#8217;ve finished the game! It&#8217;s been a long journey. I hope you enjoyed creating this wonderful game with me, and hardly noticed you were learning in the process. Thanks for reading!</p>
<p>An example project with all of the code from the entire tutorial series can be downloaded <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/RocketMousePart4Finale.zip">here</a>. </p>
<p>As always, I am available to answer questions in the forums for this tutorial series, both here and on the <a href=“http://www.levelhelper.org/phpBB3/index.php”>LevelHelper forum</a>.</p>
<p><img src="http://www.raywenderlich.com/downloads/BogdanVladu.png" class="photo left" width="100" height="100"><br />
<i>This is a post by special contributor <a href="http://twitter.com/vladubogdan">Bogdan Vladu</a>, an iOS application developer and aspiring game developer living in Bucharest, Romania.</i></p>
<p><a href="http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4">How to Make a Game Like Jetpack Joyride Using LevelHelper &#038; SpriteHelper – Part 4</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton7358" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FzdtBZ3&amp;via=rwenderlich&amp;text=How%20to%20Make%20a%20Game%20Like%20Jetpack%20Joyride%20Using%20LevelHelper%20%26%23038%3B%20SpriteHelper%20%E2%80%93%20Part%204&amp;related=vladubogdan&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7358%2Fhow-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%25e2%2580%2593-part-4" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/JHSipVB_p4Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/7358/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-%e2%80%93-part-4</feedburner:origLink></item>
		<item>
		<title>How to Make a Game Like Jetpack Joyride Using LevelHelper &amp; SpriteHelper – Part 3</title>
		<link>http://feedproxy.google.com/~r/RayWenderlich/~3/HbHjck0mHiI/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3</link>
		<comments>http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3#comments</comments>
		<pubDate>Sat, 21 Jan 2012 15:00:52 +0000</pubDate>
		<dc:creator>Bogdan Vladu</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[box2D]]></category>
		<category><![CDATA[cocos2D]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[sample code]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.raywenderlich.com/?p=7244</guid>
		<description><![CDATA[TweetThis is a post by special contributor Bogdan Vladu, an iOS application developer and aspiring game developer living in Bucharest, [...]<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3">How to Make a Game Like Jetpack Joyride Using LevelHelper &#038; SpriteHelper &#8211; Part 3</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton7244" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FxijdeA&amp;via=rwenderlich&amp;text=How%20to%20Make%20a%20Game%20Like%20Jetpack%20Joyride%20Using%20LevelHelper%20%26%23038%3B%20SpriteHelper%20%26%238211%3B%20Part%203&amp;related=vladubogdan&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7244%2Fhow-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><i>This is a post by special contributor <a href="http://twitter.com/vladubogdan">Bogdan Vladu</a>, an iOS application developer and aspiring game developer living in Bucharest, Romania.</i></p>
<div id="attachment_8506" class="wp-caption alignright" style="width: 302px"><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMousePart3Small.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMousePart3Small.png" alt="Create a game like Jetpack Joyride with LevelHelper and SpriteHelper!" title="Create a game like Jetpack Joyride with LevelHelper and SpriteHelper!" width="292" height="209" class="size-full wp-image-8506" /></a><p class="wp-caption-text">Create a game like Jetpack Joyride with LevelHelper and SpriteHelper!</p></div>
<p>Welcome back to our Jetpack Joyride tutorial series!  In this tutorial series, we are making a game similar to <a href="http://bit.ly/wbIUWV">Jetpack Joyride</a> using Cocos2D and Box2D, and the <a href="http://bit.ly/wjIn37">LevelHelper</a> and <a href="http://bit.ly/yuD0cB">SpriteHelper</a> tools.</p>
<p>So far, we&#8217;ve got a mouse that can use his jetpack to fly through a scrolling level, complete with animations and endless scrolling.  Check out how we did it in <a href="http://www.raywenderlich.com/6929/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-1">part one</a> and <a href="http://www.raywenderlich.com/8183/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-2">part two</a>.</p>
<p>I hope you enjoyed the first two parts, but, psst&#8230; part three is where we get to the really fun stuff!</p>
<p>In this part of the series, we&#8217;ll make our game fully capable of handling collisions. </p>
<p>In other words, by the end of this part we&#8217;ll be able to reward and kill the mouse!  :]</p>
<p>We&#8217;ll also add sounds, more sophisticated animations, and we&#8217;ll iron-out some problems with game play.</p>
<p>So what are you waiting for &#8211; time to give our mouse the ride of his life!</p>
<p><span id="more-7244"></span></p>
<h2>Getting Started</h2>
<p>To continue with this part of the tutorial series, first make sure that you have the complete project from Part Two, available <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/RocketMouseCompletePart1.zip">here</a>.<br />
You will also need to download <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/MusicPack.zip">this sound pack</a>, which we will be using later on.</p>
<p>To open our project in LevelHelper, navigate to your last project folder in Finder and double click on the file RocketMouse.lhproject.</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/doubleclickonlhproject" rel="attachment wp-att-7247"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/DoubleClickOnLHProject.png" alt="" width="223" height="135" class="alignnone size-full wp-image-7247" /></a></p>
<p>LevelHelper should open the last level we were working on. If for some reason LevelHelper opens an earlier version of the level, just go to the Levels section to find and open the latest version.</p>
<h2>Implementing Collisions: Overview</h2>
<p>Our game has a flying mouse and shooting lasers (with the help of animations), and it also has coins for the player to collect.  However, if you run into the coins or lasers nothing happens &#8211; so it&#8217;s time to add some gameplay for that!</p>
<p>Before we do so, however, let&#8217;s take a look at how things are currently set up.</p>
<p>In LevelHelper, right click the coin in the right sidebar (first tab) and select Open SpriteHelper Scene.  Select the coin in SpriteHelper, and you&#8217;ll see that in the physics section, we have the &#8220;Is Sensor&#8221; option checked:</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/CoinSettings.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/CoinSettings.png" alt="Settings for the coin in SpriteHelper has Is Sensor set to yes" title="Settings for the coin in SpriteHelper has Is Sensor set to yes" width="227" height="244" class="alignnone size-full wp-image-8475" /></a></p>
<p>We have this checked because we want the player to trigger a collision response when it touches a coin, so that we know to give points to the user, but we don&#8217;t want the mouse to behave like it&#8217;s colliding with the coin.</p>
<p>What about the laser? If we look at the laser property, this sprite does not have the &#8220;Is Sensor&#8221; option selected:</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/LaserSettings.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/LaserSettings.png" alt="Settings for the laser does not have is sensor checked" title="Settings for the laser does not have is sensor checked" width="231" height="241" class="alignnone size-full wp-image-8476" /></a></p>
<p>Why? Don&#8217;t we want the same behavior? We do, but our need to keep track of our lasers&#8217;  animation requires us to to handle them differently.</p>
<p>If a sprite has Is Sensor selected, the collision will be triggered only when the mouse first touches the sprite. But for our lasers, we need the possibility of continuous collisions (every frame), because from one frame to another the animation of the laser might change (from off to on, for example) while the player is still in contact with the laser.</p>
<p>If we have Is Sensor enabled for the lasers, and the player makes contact with a laser when the laser is off, we have a collision response &#8211; but we wouldn&#8217;t fry the mouse, because after all, the laser is off.</p>
<p>But then the laser turns on. But because the collision already has been performed, we have no way of knowing if the player is still touching the laser.</p>
<p>How can we solve this? Well, it&#8217;s easy. Using LevelHelper&#8217;s collisions behavior together with Box2d, we can disable the collision response so that the mouse will move over the laser without behaving like it&#8217;s colliding with it. This way we will have collision trigger on every frame, and we&#8217;ll know if we need to kill the player.</p>
<h2>Implementing Collisions: Coins</h2>
<p>OK, finally time to code!  Open up your Xcode project, navigate to HelloWorldScene.mm and declare this new method before init:</p>

<div class="wp_codebox"><table><tr id="p7244191"><td class="code" id="p7244code191"><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> setupCollisionHandling
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>lh useLevelHelperCollisionHandling<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>lh registerBeginOrEndCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>COIN idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseCoinCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Next, call this new method at the end of init (right after the call to retrieveRequiredObjects):</p>

<div class="wp_codebox"><table><tr id="p7244192"><td class="code" id="p7244code192"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self setupCollisionHandling<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>So what have we done here?</p>
<p>In the setupCollisionHandling method, we first tell to the LevelHelperLoader instance that we want to use its collision handling and not create our own collision handling. My advice is: always use this, because its fast, easy and painless.</p>
<p>On the second call, we register a method that we want LevelHelper to call whenever a collision between a sprite with the tag PLAYER (in our case the mouse sprite) and a sprite with the tag COIN (in our case any coin sprite) happens. </p>
<p>Remember how we set up collision tags for these in <a href=“http://www.raywenderlich.com/?p=8183”>part two</a>?  Well LevelHelper automatically generates constants for these so we can use them in code!  You can control-click on one of them and choose &#8220;Go to definition&#8221; if you&#8217;re curious where they are defined.</p>
<p>LevelHelper collision handling has multiple types of collision but here we use beginOrEnd because our coin sprites are defined as sensors and Box2d handles collision for sensor objects only on &#8220;begin&#8221; collision types.</p>
<p>Now let&#8217;s define the methods that will be used for collision responses between the mouse and coin.  Add this new method right before setupCollisionHandling:</p>

<div class="wp_codebox"><table><tr id="p7244193"><td class="code" id="p7244code193"><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>mouseCoinCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHContactInfo<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>        
    LHSprite<span style="color: #002200;">*</span> coin <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>contact spriteB<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> coin<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>coin visible<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>self scoreHitAtPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>coin position<span style="color: #002200;">&#93;</span> withPoints<span style="color: #002200;">:</span><span style="color: #2400d9;">100</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
        <span style="color: #002200;">&#91;</span>coin setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, this method gets a LHContactInfo* object as an argument. This is a special class that will give us info about the collision. </p>
<p>So how do we get the coin sprite from this collision? Well, we register the coin sprite as being tagB in a call to registerBeginOrEndCollisionCallbackBetweenTagA andTagB. So if tag B is the coin, then we take the sprite using [contact spriteB].</p>
<p>If you want to learn more about the LHContactInfo class, check out the official <a href="http://www.levelhelper.org/app/documentation/docs/COCOS2D_Box2d_collision.htm">LevelHelper documentation</a>.</p>
<p>Next, we make sure the coin is not nil. This is not strictly necessary, but it&#8217;s a good way to avoid errors.  As a general note, it&#8217;s always good to check against nil.</p>
<p>If the coin is visible, we call a method that we&#8217;ll write in a minute to give points to the user for taking the coin. We then make the coin invisible in order to hide it from the screen and give visual feedback to the user that he now has the coin in his virtual wallet.</p>
<p>Compiling now will give an error, because we did not define the scoreHitAtPosition method. Let&#8217;s define it now. Put the following before the mouseCoinCollision method:</p>

<div class="wp_codebox"><table><tr id="p7244194"><td class="code" id="p7244code194"><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>scoreHitAtPosition<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>position withPoints<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>points
<span style="color: #002200;">&#123;</span>    
    score <span style="color: #002200;">+=</span> points;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Declare the variable score inside the HelloWorldScene.h:</p>

<div class="wp_codebox"><table><tr id="p7244195"><td class="code" id="p7244code195"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> score;</pre></td></tr></table></div>

<p>Compile and run the game, and you&#8217;ll see that when the mouse collides with the coins, the coins disappear!</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/CoinsCollected.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/CoinsCollected.png" alt="Collecting coins in our game" title="Collecting coins in our game" width="480" height="320" class="alignnone size-full wp-image-8490" /></a></p>
<h2>Implementing Collisions: Lasers</h2>
<p>The next step is to handle the collision between the lasers and the mouse.</p>
<p>Inside the setupCollisionHandling method, add the following at the end:</p>

<div class="wp_codebox"><table><tr id="p7244196"><td class="code" id="p7244code196"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>lh registerPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>LASER idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseLaserCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Here we&#8217;ve registered a pre collision callback between the player and the lasers. Now we need to define the mouseLaserCollision method.<br />
We did this because the laser sprites are not sensors and we want to receive notification about this collision on every frame in order to kill the mouse when the laser has become active.</p>
<p>Put the following after the mouseCoinCollision:</p>

<div class="wp_codebox"><table><tr id="p7244197"><td class="code" id="p7244code197"><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>mouseLaserCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHContactInfo<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>        
    LHSprite<span style="color: #002200;">*</span> laser <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>contact spriteB<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">int</span> frame  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>laser currentFrame<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// If we make the laser a sensor, the callback will be called only once - at first collision.</span>
    <span style="color: #11740a; font-style: italic;">// This is not good as we want to kill the player when the laser changes to active.</span>
    <span style="color: #11740a; font-style: italic;">// So we disable the contact so that the player and laser don't collide, but trigger a collision.</span>
    <span style="color: #11740a; font-style: italic;">// Disabling the contact is only active for one frame,</span>
    <span style="color: #11740a; font-style: italic;">// so on the next frame the contact will be active again, triggering the collision.</span>
&nbsp;
    b2Contact<span style="color: #002200;">*</span> box2dContact <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>contact contact<span style="color: #002200;">&#93;</span>;    
    box2dContact<span style="color: #002200;">-</span>&gt;SetEnabled<span style="color: #002200;">&#40;</span><span style="color: #a61390;">false</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>playerIsDead<span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>frame <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self killPlayer<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the above code, we take the sprite B from the contact info. In our case sprite B is the laser. Then we take the current frame from the sprite, because we need to test if the laser is active and kill the player if it is.</p>
<p>We then take the Box2d contact information from the LevelHelper contact object and disable the contact so that no collision behavior will occur. We check if the player is dead. If so, we do nothing.</p>
<p>Finally, we test if the frame number is not 0. If it is 0, then the laser is not on, so we don&#8217;t need to kill the player. If it&#8217;s not 0, it means we need to kill the player. We cancel the collision callback because we don&#8217;t need it anymore, and kill the player.</p>
<p>Notice we&#8217;re using two things that we have not yet defined: the killPlayer method and the playerIsDead variable. So let&#8217;s define them.</p>
<p>Inside HelloWorldScene.h, put the following in the class declaration:</p>

<div class="wp_codebox"><table><tr id="p7244198"><td class="code" id="p7244code198"><pre class="objc" style="font-family:monospace;">bool playerIsDead;</pre></td></tr></table></div>

<p>Then define the killPlayer method inside HelloWorldScene.mm (add after mouseCoinCollision):</p>

<div class="wp_codebox"><table><tr id="p7244199"><td class="code" id="p7244code199"><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>killPlayer
<span style="color: #002200;">&#123;</span>     
    playerVelocity <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
    playerShouldFly <span style="color: #002200;">=</span> <span style="color: #a61390;">false</span>;
    playerIsDead <span style="color: #002200;">=</span> <span style="color: #a61390;">true</span>;
    playerWasFlying <span style="color: #002200;">=</span> <span style="color: #a61390;">false</span>;
    <span style="color: #002200;">&#91;</span>rocketFlame setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>player startAnimationNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mouseDie&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>paralaxNode setSpeed<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    CGSize winSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span> winSize<span style="color: #002200;">&#93;</span>;
    CCLabelTTF <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCLabelTTF labelWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Game Over&quot;</span> fontName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Marker Felt&quot;</span> fontSize<span style="color: #002200;">:</span><span style="color: #2400d9;">64</span><span style="color: #002200;">&#93;</span>;
    label.color <span style="color: #002200;">=</span> ccRED;
    label.position <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span>winSize.width<span style="color: #002200;">*</span><span style="color: #2400d9;">0.5</span>, winSize.height<span style="color: #002200;">*</span><span style="color: #2400d9;">0.75</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>label<span style="color: #002200;">&#93;</span>;
&nbsp;
    CCMenuItem <span style="color: #002200;">*</span>item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenuItemFont itemFromString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Restart&quot;</span> target<span style="color: #002200;">:</span> self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>restartGame<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    CCMenu <span style="color: #002200;">*</span>menu <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CCMenu menuWithItems<span style="color: #002200;">:</span>item, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>menu alignItemsVertically<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>menu<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the killPlayer method above, we set the velocity of the player to 0. If the player was flying, it will now fall to the ground.</p>
<p>We then set the playerIsDead variable to true so we know the player is dead in the methods where this information is a factor. We hide the rocket flame. </p>
<p>Finally, we start the death animation on the player sprite by using a LevelHelper method that will take as arguments the animation&#8217;s unique name and the sprite on which we want to perform the animation. We can take the animation name from the Animations section inside LevelHelper.</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/animationnames" rel="attachment wp-att-7252"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/animationNames.png" alt="" width="245" height="222" class="alignnone size-full wp-image-7252" /></a></p>
<p>For more details on using animations with LevelHelper, check out the <a href=""http://www.levelhelper.org/app/documentation/docs/COCOS2D_animation.htm">official LevelHelper documentation</a>.</p>
<p>After that, we stop the parallax from moving by setting its speed to 0. </p>
<p>Now we need to create a Cocos2d menu so that we can restart the game if the player dies. Let&#8217;s define the restartGame method before the killPlayer method as follows:</p>

<div class="wp_codebox"><table><tr id="p7244200"><td class="code" id="p7244code200"><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>restartGame
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCDirector sharedDirector<span style="color: #002200;">&#93;</span> replaceScene<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>HelloWorldScene scene<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Compile and run, and now your mouse can die if he collides with a laser!</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/MouseDead.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/MouseDead.png" alt="Mouse dies upon colliding with a laser" title="Mouse dies upon colliding with a laser" width="480" height="320" class="alignnone size-full wp-image-8495" /></a></p>
<h2>Gratuitous Sound Effects</h2>
<p>If you&#8217;ve read game tutorials on this blog before, you know we&#8217;d never leave you hanging without some gratuitous (and awesome) sound effects!  :]</p>
<p>Navigate to your Xcode Resources folder in Finder. Then, open a new Finder window and navigate to where you saved the sounds pack that you downloaded at the beginning of this tutorial.</p>
<p>Inside the Resources folder, create a new folder called Music, and drag all the sound files from the sound pack into this new folder.</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/dragsounds" rel="attachment wp-att-7248"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/dragSounds-500x358.png" alt="" width="500" height="358" class="alignnone size-large wp-image-7248" /></a></p>
<p>Now go back to Xcode and include the music folder inside your Resources by right-clicking (or Control-clicking) on the Resources folder and choosing &#8220;Add Files to RocketMouse.&#8221;</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/addsoundstorocket" rel="attachment wp-att-7249"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/addSoundsToRocket.png" alt="" width="389" height="252" class="alignnone size-full wp-image-7249" /></a></p>
<p>In the new window, navigate to your Resources folder, select the Music folder, and click the Add button.</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/addsoundsdialogue" rel="attachment wp-att-7250"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/addSoundsDialogue-478x500.png" alt="" width="478" height="500" class="alignnone size-large wp-image-7250" /></a></p>
<p>Your new Resources folder inside Xcode should look something like this:</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/newresources" rel="attachment wp-att-7251"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/newResources.png" alt="" width="195" height="207" class="alignnone size-full wp-image-7251" /></a></p>
<p>Now that we&#8217;ve added our sound files to our project, let&#8217;s code the sound so we can make some noise!</p>
<p>At the top of HelloWorldScene.mm import the audio engine:</p>

<div class="wp_codebox"><table><tr id="p7244201"><td class="code" id="p7244code201"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;SimpleAudioEngine.h&quot;</span></pre></td></tr></table></div>

<p>Then add this new method that will load our sounds (put this right above the init method):</p>

<div class="wp_codebox"><table><tr id="p7244202"><td class="code" id="p7244code202"><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> setupAudio
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playBackgroundMusic<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;backgroundMusic.m4a&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;coin.wav&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fly.wav&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ground.wav&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hitObject.wav&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;laser.wav&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lose.wav&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> preloadEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bunnyHit.wav&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Then call this method in init (right after the call to setupCollisionHandling):</p>

<div class="wp_codebox"><table><tr id="p7244203"><td class="code" id="p7244code203"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self setupAudio<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This is where we load all the sounds in the pack. We load backgroundMusic.m4a as a music asset and we preload the rest of the files as effects.</p>
<p>Now we have to match the effects with the methods that will generate them. In the mouseCoinCollision method add the following:</p>

<div class="wp_codebox"><table><tr id="p7244204"><td class="code" id="p7244code204"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;coin.wav&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>In the mouseLaserCollision method add the following inside the if(frame != 0) statement:</p>

<div class="wp_codebox"><table><tr id="p7244205"><td class="code" id="p7244code205"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;laser.wav&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Let&#8217;s also add the flying sound. Inside the tick method, add the following inside the if playerShouldFly block:</p>

<div class="wp_codebox"><table><tr id="p7244206"><td class="code" id="p7244code206"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fly.wav&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Compile and run the game, and enjoy the new music and sound effects!  :]</p>
<p>The complete project up to this point can be downloaded <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/rocketMouseLaserCoinsCollision.zip">here</a>.</p>
<h2>Collisions Between Mouse, Cats and Dogs</h2>
<p>So far our mouse can die if he hits a laser, but he&#8217;s getting off to easy when it comes to the cats and dogs!  So let&#8217;s add some collision handling for them as well.</p>
<p>Add the following at the end of setupCollsionHandling method:</p>

<div class="wp_codebox"><table><tr id="p7244207"><td class="code" id="p7244code207"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>lh registerPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>DOG idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseDogCatCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#91;</span>lh registerPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>CAT idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseDogCatCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Let&#8217;s now define the method for the callback. Because our game will perform the same action for collisions with cats and dogs, we only need one method. Add this somewhere before the setupCollisionHandling method:</p>

<div class="wp_codebox"><table><tr id="p7244208"><td class="code" id="p7244code208"><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>mouseDogCatCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHContactInfo<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>    
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hitObject.wav&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>lh cancelPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>DOG<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>lh cancelPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>CAT<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>self killPlayer<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here we play the needed sound, then we cancel the callback between the player and the cat or dog, because we no longer need them. Then we kill the player.</p>
<p>Compile and run the game, and now your mouse can die when he hits a cat or dog too &#8211; so watch out!  :]</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/MouseDeadDog.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/MouseDeadDog.png" alt="Mouse dies from hitting a dog" title="Mouse dies from hitting a dog" width="480" height="320" class="alignnone size-full wp-image-8502" /></a></p>
<h2>Tweaking Gameplay</h2>
<p>Our game is looking good so far, but there are still a number of problems.</p>
<p>The first of these problems is that if you touch the screen after the mouse dies, he starts flying around like he&#8217;s been resurrected!  Although &#8220;Zombie Mouse&#8221; might make for a popular game on the App Store, this is not the effect we&#8217;re going for ;]</p>
<p>Let&#8217;s put a stop to this zombie behavior before it gets out of hand! Just add the following check to the beginning of ccTouchesBegan:</p>

<div class="wp_codebox"><table><tr id="p7244209"><td class="code" id="p7244code209"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>playerIsDead<span style="color: #002200;">&#41;</span>
    <span style="color: #a61390;">return</span>;</pre></td></tr></table></div>

<p>The only thing we&#8217;re doing differently here is testing to see if the player is dead. If they are, then we do nothing.</p>
<p>If we run the game now, the zombie behavior won&#8217;t occur anymore. But there are other issues. </p>
<p>For example, in my level there are places where the player can&#8217;t get past a laser because it&#8217;s active, but he can&#8217;t go under it because there&#8217;s a cat in the way. Your level may have similar problems.</p>
<p>It&#8217;s time to play-test the level and make any necessary modifications to be sure it&#8217;s possible for the player to successfully finish the game.</p>
<p>Open up LevelHelper with our level03 level, and drag the cats, dogs and laser sprites as necessary so that the player has a way through. (But don&#8217;t make it too easy!)</p>
<p>Make sure to save the level!</p>
<p>Since we&#8217;re modifying the level anyway, let&#8217;s create a way to know when the player touches the ground. To do this we&#8217;ll define a new tag and add that tag to the bottom border of the physic boundary. Click the Define Tag button (as we did in <a href=“http://www.raywenderlich.com/?p=8183”>Part Two</a>) and add the tag &#8220;GROUND.&#8221;</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/definegroundtag" rel="attachment wp-att-7253"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/defineGroundTag.png" alt="" width="410" height="361" class="alignnone size-full wp-image-7253" /></a></p>
<p>Now that we have this new tag, let&#8217;s assign it to the bottom part of the Physic Boundary shape.<br />
To do this click the Physic Boundaries button.</p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/setgroundtopboundary" rel="attachment wp-att-7254"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/12/setGroundToPBoundary.png" alt="" width="473" height="186" class="alignnone size-full wp-image-7254" /></a></p>
<p>Save your level in LevelHelper.</p>
<p>We still have to define the collision callback between the player and the ground. Add the following at the end of setupCollisionHandling method:</p>

<div class="wp_codebox"><table><tr id="p7244210"><td class="code" id="p7244code210"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>lh registerPreCollisionCallbackBetweenTagA<span style="color: #002200;">:</span>PLAYER andTagB<span style="color: #002200;">:</span>GROUND idListener<span style="color: #002200;">:</span>self selListener<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>mouseGroundCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Define the mouseGroundCollision method somewhere above the setupCollisionHandling method, as follows:</p>

<div class="wp_codebox"><table><tr id="p7244211"><td class="code" id="p7244code211"><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>mouseGroundCollision<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHContactInfo<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contact
<span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>playerIsDead<span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>playerWasFlying<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SimpleAudioEngine sharedEngine<span style="color: #002200;">&#93;</span> playEffect<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ground.wav&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span>player startAnimationNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mouseFall&quot;</span> 
                     endObserverObj<span style="color: #002200;">:</span>self 
                     endObserverSel<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>fallAnimHasEnded<span style="color: #002200;">:</span>animName<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
          shouldObserverLoopForever<span style="color: #002200;">:</span>FALSE<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#125;</span>
    playerWasFlying <span style="color: #002200;">=</span> <span style="color: #a61390;">false</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We test if the player is dead so we can return if they are. We then test if the player was flying when they touched the ground. If they were, we play landing sound.</p>
<p>Then we start the mouseFall animation, but we start it using a notification. That way when the animation ends, we get notified so that we can set another animation on the player sprite.</p>
<p>So let&#8217;s define the method that gets called when the mouseFall animation ends. Add it above mouseGroundCollision, as follows:</p>

<div class="wp_codebox"><table><tr id="p7244212"><td class="code" id="p7244code212"><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> fallAnimHasEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHSprite<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>spr animName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>animName
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>player startAnimationNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mouseRun&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This new method takes as arguments the sprite that had the animation and the animation name. It then  starts the animation &#8220;mouseRun&#8221; on the player sprite the normal way.</p>
<p>Playing the game now, it looks like we have almost everything in place. But there is still one thing we need to fix!</p>
<p>You may have noticed that if we run the game now, coins taken from the level don&#8217;t reappear when the parallax restarts. We hid them, remember, after player collisions, so we need to add a way to make them visible again when the parallax starts over. </p>
<p>Inside the retrieveRequiredObjects method declaration, add the following line, after you&#8217;ve taken the pointer to the parallax node:</p>

<div class="wp_codebox"><table><tr id="p7244213"><td class="code" id="p7244code213"><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> retrieveRequiredObjects
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// existing lines</span>
    paralaxNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>lh paralaxNodeWithUniqueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Parallax_1&quot;</span><span style="color: #002200;">&#93;</span>;
    NSAssert<span style="color: #002200;">&#40;</span>paralaxNode<span style="color: #002200;">!=</span><span style="color: #a61390;">nil</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Couldn't find the parallax!&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// add this new line</span>
    <span style="color: #002200;">&#91;</span>paralaxNode registerSpriteHasMovedToEndListener<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>spriteInParallaxHasReset<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
    <span style="color: #11740a; font-style: italic;">// rest of code...</span></pre></td></tr></table></div>

<p>This new method registers on the parallax another new method (spriteInParallaxHasReset) that will be called whenever a sprite in the parallax is reset to the back of the parallax. This method gets called when the sprite exits the view.</p>
<p>Define spriteInParallaxHasReset before the retrieveRequiredObjects declaration:</p>

<div class="wp_codebox"><table><tr id="p7244214"><td class="code" id="p7244code214"><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> spriteInParallaxHasReset<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LHSprite<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sprite
<span style="color: #002200;">&#123;</span>    
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>COIN <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>sprite tag<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>sprite setVisible<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here we test the tag of the sprite that was reset by the parallax, and if it&#8217;s tagged as a COIN we make it visible again.</p>
<p>Compile and run your game, and see how long you can play without dying!  :]</p>
<p><a href="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMousePart3.png"><img src="http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2012/01/RocketMousePart3.png" alt="Rocket mouse part 3 complete!" title="Rocket mouse part 3 complete!" width="480" height="320" class="alignnone size-full wp-image-8503" /></a></p>
<h2>Where to Go From Here?</h2>
<p>If you&#8217;ve followed along this far, you should have a smoothly-functioning game that is very similar to Jetpack Joyride! It&#8217;s not quite complete (stay tuned for Part Four), but very close, with animations, collisions, sounds, and correct gameplay.</p>
<p>In case you need it, here is an <a href="http://www.levelhelper.org/app/tutorials/rocketMouse/RocketMousePart3Finale.zip">example project</a> that includes all of the code for this tutorial series up to this point.</p>
<p>In the fourth and final part of this tutorial series, we&#8217;ll increase the difficulty of the game by making some of the lasers rotate. But we&#8217;ll also add another point-scoring opportunity in the form of bunnies running through the level that the player can kill, and display the score as it increases.</p>
<p>Finally, as the crowning touch for our game, we&#8217;ll create a second scene in SpriteHelper and add it to our level as a background, so that the user will be able to look through the windows at the “outside” world.</p>
<p>Until then, feel free to participate in forum discussion both below and on the <a href=“http://www.levelhelper.org/phpBB3/index.php”>LevelHelper site</a>.</p>
<p><img src="http://www.raywenderlich.com/downloads/BogdanVladu.png" class="photo left" width="100" height="100"><br />
<i>This is a post by special contributor <a href="http://twitter.com/vladubogdan">Bogdan Vladu</a>, an iOS application developer and aspiring game developer living in Bucharest, Romania.</i></p>
<p><a href="http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3">How to Make a Game Like Jetpack Joyride Using LevelHelper &#038; SpriteHelper &#8211; Part 3</a> is a post from: <a href="http://www.raywenderlich.com">Ray Wenderlich</a></p>
<div id="tweetbutton7244" class="tw_button" style="float:left;"><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FxijdeA&amp;via=rwenderlich&amp;text=How%20to%20Make%20a%20Game%20Like%20Jetpack%20Joyride%20Using%20LevelHelper%20%26%23038%3B%20SpriteHelper%20%26%238211%3B%20Part%203&amp;related=vladubogdan&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.raywenderlich.com%2F7244%2Fhow-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://d1xzuxjlafny7l.cloudfront.net/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><img src="http://feeds.feedburner.com/~r/RayWenderlich/~4/HbHjck0mHiI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.raywenderlich.com/7244/how-to-make-a-game-like-jetpack-joyride-using-levelhelper-spritehelper-part-3</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 6.659 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-02-03 11:05:11 -->

