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

<channel>
	<title>iCodeBlog &#187; iPhone Game Programming</title>
	<atom:link href="http://icodeblog.com/category/iphone-game-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 07 Sep 2010 17:27:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>iPhone Coding Tutorial &#8211; Inserting A UITextField In A UIAlertView</title>
		<link>http://icodeblog.com/2009/11/09/iphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview/</link>
		<comments>http://icodeblog.com/2009/11/09/iphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 02:46:18 +0000</pubDate>
		<dc:creator>AppStoreMod</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1463</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/11/09/iphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview/"><img align="left" hspace="5" width="150" src="http://icodeblog.com/wp-content/uploads/2009/11/Screen-shot-2009-11-09-at-8.12.11-AM-copy.png" class="alignleft wp-post-image tfe" alt="Screen shot 2009-11-09 at 8.12.11 AM copy" title="" /></a>This will be a simple tutorial showing you how to put a UITextField in a UIAlertView. This is simple and just a couple lines if code. You will learn CGAffineTransform and coding UITextField programmatically. Heres a screenshots of what we [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F11%2F09%2Fiphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F11%2F09%2Fiphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview%2F&amp;source=elctech&amp;style=normal&amp;hashtags=%23iphonedev" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignleft size-full wp-image-1473" src="http://icodeblog.com/wp-content/uploads/2009/11/Screen-shot-2009-11-09-at-8.12.11-AM-copy.png" alt="Screen shot 2009-11-09 at 8.12.11 AM copy" width="200" height="96" />This will be a simple tutorial showing you how to put a UITextField in a UIAlertView. This is simple and just a couple lines if code. You will learn CGAffineTransform and coding UITextField programmatically.<span id="more-1463"></span></p>
<p>Heres a screenshots of what we should get.</p>
<p><img class="size-full wp-image-1472 alignnone" src="http://icodeblog.com/wp-content/uploads/2009/11/Screen-shot-2009-11-09-at-8.12.11-AM.png" alt="Screen shot 2009-11-09 at 8.12.11 AM" width="260" height="388" /></p>
<p>So lets go ahead and get started&#8230;</p>
<p><span style="font-family: helvetica, arial, sans-serif;line-height: 40px;font-size: 34px;color: #225e8a;letter-spacing: -2px">1. Create A New View Based Application</span><br />
<span style="font-family: helvetica, arial, sans-serif;line-height: 40px;font-size: 34px;color: #225e8a;letter-spacing: -2px"><span style="color: #000000;font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;letter-spacing: normal;line-height: 19px;font-size: 13px">You can name it whatever you want, I am gonna name it TextFieldInAlert.</span></span><br />
<span style="font-family: helvetica, arial, sans-serif;line-height: 40px;font-size: 34px;color: #225e8a;letter-spacing: -2px"> </span></p>
<p><span style="font-family: helvetica, arial, sans-serif;line-height: 40px;font-size: 34px;color: #225e8a;letter-spacing: -2px">2. Implementing The Code</span><br />
Jump in the viewcontroller.m or if you called it TextFieldInAlert then TextFieldInAlert.m Now find the -(void)viewDidLoad method. Uncomment it and put this code in there.</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">- (<span style="color: #c800a7">void</span>)viewDidLoad {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #000000"> [</span><span style="color: #c800a7">super</span><span style="color: #000000"> </span>viewDidLoad<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo"><span> </span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #7b1caf">UIAlertView</span><span style="color: #000000"> *alert = [[</span><span style="color: #7b1caf">UIAlertView</span><span style="color: #000000"> </span>alloc<span style="color: #000000">] </span>initWithTitle<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;Enter Name Here&#8221;</span><span style="color: #000000"> </span>message<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;this gets covered!&#8221;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #dd0005"> </span>delegate<span style="color: #000000">:</span><span style="color: #c800a7">self</span><span style="color: #000000"> </span>cancelButtonTitle<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;Dismiss&#8221;</span><span style="color: #000000"> </span>otherButtonTitles<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;OK!&#8221;</span><span style="color: #000000">, </span><span style="color: #c800a7">nil</span><span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo"><span style="color: #7b1caf">UITextField</span> *myTextField = [[<span style="color: #7b1caf">UITextField</span> <span style="color: #470086">alloc</span>] <span style="color: #470086">initWithFrame</span>:<span style="color: #470086">CGRectMake</span>(<span style="color: #4100e0">12</span>, <span style="color: #4100e0">45</span>, <span style="color: #4100e0">260</span>, <span style="color: #4100e0">25</span>)];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #000000">[myTextField </span>setBackgroundColor<span style="color: #000000">:[</span><span style="color: #7b1caf">UIColor</span><span style="color: #000000"> </span>whiteColor<span style="color: #000000">]];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">addSubview</span>:myTextField];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">show</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">release</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[myTextField <span style="color: #470086">release</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">}</p>
</blockquote>
<p>So we are basically calling a UIAlertView and then we are adding the UITextField programmatically. You might have noticed in the message part of the UIAlertView we put &#8220;this gets covered!&#8221;, if we didn&#8217;t put that sentence then the alerts buttons would go up more and the UITextField will be messed. You can try taking that line out and see what happens. Now Build and Run the app. Now you got the UITextField to be inside the UIAlertView. Now try tapping the UITextField. Uh oh, why is the Keyboard covering the UIAlertView? Well there is just a simple fix to this. We just add two more lines of code and it will fix that. Add this to your code</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #7b1caf">CGAffineTransform</span><span style="color: #000000"> myTransform = </span>CGAffineTransformMakeTranslation<span style="color: #000000">(</span><span style="color: #4100e0">0</span><span style="color: #000000">, </span><span style="color: #4100e0">60</span><span style="color: #000000">);</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">setTransform</span>:myTransform];</p>
</blockquote>
<p>So now your full code should be looking like this.</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #7b1caf">UIAlertView</span><span style="color: #000000"> *alert = [[</span><span style="color: #7b1caf">UIAlertView</span><span style="color: #000000"> </span>alloc<span style="color: #000000">] </span>initWithTitle<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;Enter Name Here&#8221;</span><span style="color: #000000"> </span>message<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;this gets covered!&#8221;</span><span style="color: #000000"> </span>delegate<span style="color: #000000">:</span><span style="color: #c800a7">self</span><span style="color: #000000"> </span>cancelButtonTitle<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;Dismiss&#8221;</span><span style="color: #000000"> </span>otherButtonTitles<span style="color: #000000">:</span><span style="color: #dd0005">@&#8221;OK!&#8221;</span><span style="color: #000000">, </span><span style="color: #c800a7">nil</span><span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo"><span style="color: #7b1caf">UITextField</span> *myTextField = [[<span style="color: #7b1caf">UITextField</span> <span style="color: #470086">alloc</span>] <span style="color: #470086">initWithFrame</span>:<span style="color: #470086">CGRectMake</span>(<span style="color: #4100e0">12</span>, <span style="color: #4100e0">45</span>, <span style="color: #4100e0">260</span>, <span style="color: #4100e0">25</span>)];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #7b1caf">CGAffineTransform</span><span style="color: #000000"> myTransform = </span>CGAffineTransformMakeTranslation<span style="color: #000000">(</span><span style="color: #4100e0">0</span><span style="color: #000000">, </span><span style="color: #4100e0">60</span><span style="color: #000000">);</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">setTransform</span>:myTransform];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo;color: #470086"><span style="color: #000000">[myTextField </span>setBackgroundColor<span style="color: #000000">:[</span><span style="color: #7b1caf">UIColor</span><span style="color: #000000"> </span>whiteColor<span style="color: #000000">]];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">addSubview</span>:myTextField];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">show</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px;font: 11.0px Menlo">[alert <span style="color: #470086">release</span>];</p>
<p style="font: normal normal normal 11px/normal Menlo;margin: 0px">[myTextField <span style="color: #470086">release</span>];</p>
</blockquote>
<p>Now if you Build and Run, you will notice the UITextField is a little higher and when you tap the UITextField the Keyboard doesn&#8217;t cover it up anymore. That is what the CGAffineTransform was for. So that is basically it! There is a video tutorial also available and if you like video tutorial more then written ones you can check it out out by clicking <a href="http://www.youtube.com/watch?v=1BvuD6MAPmU">HERE</a>. You can download the source code below. Happy iCoding!</p>
<p><a href="http://xcode.appstoremod.com/TextFieldInAlert.zip">iPhone Tutorial &#8211; UITextField In A UIAlertView</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/11/09/iphone-coding-tutorial-inserting-a-uitextfield-in-a-uialertview/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>iPhone Coding Tutorial &#8211; Creating an Online Leaderboard For Your Games</title>
		<link>http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/</link>
		<comments>http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 16:14:50 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone game]]></category>
		<category><![CDATA[leaderboard]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1426</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/"><img align="left" hspace="5" width="150" height="150" src="http://icodeblog.com/wp-content/uploads/2009/10/screenshot_011-150x150.png" class="alignleft wp-post-image tfe" alt="screenshot_01" title="screenshot_01" /></a>As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard.  This will also give you a very simple introduction to interfacing with web services.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F10%2F29%2Fiphone-coding-tutorial-creating-an-online-leaderboard-for-your-games%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F10%2F29%2Fiphone-coding-tutorial-creating-an-online-leaderboard-for-your-games%2F&amp;source=elctech&amp;style=normal&amp;hashtags=iphone,iPhone+Coding,iphone+game,leaderboard" height="61" width="50" /><br />
			</a>
		</div>
<p>As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard for use in your iPhone games.  For those of you who don&#8217;t know, a leaderboard is essentially a high scores list.  This tutorial will also give you a very simple introduction to interfacing with web services.</p>
<p>The first part of this tutorial will discuss how to build the server.  We will be doing this using PHP and MYSQL.  I will also assume you have some working knowledge of PHP and MYSQL (if not, use the Google machine to get up to speed).</p>
<p>Since this tutorial is so long, I have decided to split it up into pages.  You will notice the page number at the bottom.  Please use them to navigate between parts of this post.  I feel that I have to make this explicit as I will undoubtably get some dude in the comments saying &#8220;Where is the rest of the tutorial&#8221;.  </p>
<p>Skill level: <font color="red">Advanced</font></p>
<h2>Creating The Database</h2>
<p>When creating a leaderboard, you will need some way of storing the data.  We will be using MYSQL for our storage.  You can also be lame and simply use flat files.  This would add quite a bit of code in the long run as you would have to write all of the sorting and pagination functionality yourself. Don&#8217;t do it.</p>
<p>One thing to note about my php server files.  I know they could be cleaned up a little and optimized (you could create a config.php that contains all the db interface code), but the goal of this tutorial is not to show you how to code killer PHP.  It&#8217;s to show you how to create code that you can connect your iPhone apps to.</p>
<p>I like to create one file that does a complete flash of the database.  That way, when I&#8217;m testing or switch from staging to production, it is a very simple process.  So, with that being said, here is my code for create_db.php.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #666666; font-style: italic;">// create_db.php</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database name */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database username */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database password */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL hostname */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_HOST'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_ENV</span><span style="color: #009900;">&#123;</span>DATABASE_SERVER<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;highscores&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialization</span>
	<span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span>DB_USER<span style="color: #339933;">,</span>DB_PASSWORD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>DB_NAME<span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Error checking</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Drop existing table if exists</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DROP TABLE <span style="color: #006699; font-weight: bold;">$table</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$retval</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CREATE TABLE <span style="color: #006699; font-weight: bold;">$table</span>(
		id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
		udid VARCHAR(45),
		name VARCHAR(25),
		score FLOAT(10,2),
		date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$retval</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Database created...&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Could not create database &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I&#8217;m not going to go into too much detail about this code, however I will give a high level description of what&#8217;s going on.  One thing to note about this code is it assumes you already have the database created.</p>
<p>First, we are defining variables that contain our database information.  Make sure you replace my empty strings with your database info. After we connect to the database, we drop the table if it already exists.  This is useful if you want to wipe out your test data.  Finally, we create the scores table.  Navigate to this file in your browser to run it and create the scores table.  Pretty easy right?</p>
<p><font color="red">You will want to make sure to remove this file from your server after running it to avoid people resetting your database.</font></p>
<p>Now that our database table has been created, it&#8217;s time to implement the web service code to publish new scores to our leaderboard.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial: Animating A Game Sprite</title>
		<link>http://icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/</link>
		<comments>http://icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 17:52:15 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone animation]]></category>
		<category><![CDATA[iPhone Coding]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1170</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/"><img align="left" hspace="5" width="150" height="150" src="http://icodeblog.com/wp-content/uploads/2009/07/screenshot_011-150x150.png" class="alignleft wp-post-image tfe" alt="screenshot_01" title="screenshot_01" /></a>One thing I have noticed about many of the games in the app store is they lack animation. Of course, the huge companies like Sega and PopCap have some pretty amazing animation, but what about us indie iPhone game developers? [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F07%2F24%2Fiphone-programming-tutorial-animating-a-game-sprite%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F07%2F24%2Fiphone-programming-tutorial-animating-a-game-sprite%2F&amp;source=elctech&amp;style=normal&amp;hashtags=iphone+animation,iPhone+Coding,iPhone+Game+Programming" height="61" width="50" /><br />
			</a>
		</div>
<p>One thing I have noticed about many of the games in the app store is they lack animation.  Of course, the huge companies like Sega and PopCap have some pretty amazing animation, but what about us indie iPhone game developers?</p>
<p><center>[See post to watch QuickTime movie]</center></p>
<p>Well, Apple has made it quite simple to do animations.  I really feel this method is often overlooked.  I will show you in just a few lines of code, how to completely animate your game images.  I will walk you through creating a simple application that uses animation.  If you don&#8217;t care about creating the app and just want the animation code, <a href="#animation">you can skip to this step</a>.  We will be making an animation of Ryu throwing some punches.</p>
<h2>1. Create  A View Based Application</h2>
<p>I&#8217;m not going to explain this one&#8230;</p>
<h2>2. Add These Images To Your Resources Folder</h2>
<p>Download<a href="http://icodeblog.com/wp-content/uploads/2009/07/ryu.zip">ryu.zip</a><a href="http://brandontreb.com/wp-content/uploads/2009/07/ryu1.zip"></a> and unzip it.  Then drag the unzipped files into your <strong>Resources</strong> folder.  Note: Sprite sheet downloaded from<a href="http://panelmonkey.org/"> http://panelmonkey.org/</a>.  This file contains 12 images of Ryu from Street Fighter punching.  It also has the background to Blanca&#8217;s stage just for fun.</p>
<h2>3. Create The Background</h2>
<p>You don&#8217;t have to do this step, it&#8217;s just to make it pretty.</p>
<p>Double click on <strong>whateveryoucalledyourapplicationViewController.xib</strong> to open it in Interface Builder.  Click the arrow button on the view to rotate it.  If you don&#8217;t what I am talking about, check out <a href="http://icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/">this post</a>.</p>
<p>Drag a UIImageView on to the screen and stretch it to fill the entire iPhone screen.  In the Attributes inspector select <strong>sfst-blanka.jpg</strong>.  Make sure mode is set to <strong>center</strong> as this image has very low resolution. It should look something like this (I have added a black background color).</p>
<p style="text-align: center;"><img class="size-full wp-image-1175 aligncenter" title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2009/07/screenshot_011.png" alt="screenshot_01" width="480" height="342" /></p>
<h2>4. Creating The Animation</h2>
<p><a name="#animation"> </a></p>
<p>Open up <strong>yourApplicationViewController.m </strong>and add the following code to the <strong>viewDidLoad</strong> method.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> imageArray  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;2.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;3.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;4.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;5.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;6.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;7.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;8.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;9.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;10.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;11.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;12.png&quot;</span><span style="color: #002200;">&#93;</span>,
							<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	UIImageView <span style="color: #002200;">*</span> ryuJump <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>
		CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">125</span>, <span style="color: #2400d9;">150</span>, <span style="color: #2400d9;">130</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	ryuJump.animationImages <span style="color: #002200;">=</span> imageArray;
	ryuJump.animationDuration <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.1</span>;
	ryuJump.contentMode <span style="color: #002200;">=</span> UIViewContentModeBottomLeft;
	<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>ryuJump<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>ryuJump startAnimating<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For all of you &#8220;1337&#8243; coders that are going to post in the comments telling me &#8220;Why don&#8217;t you use a for loop to load the images&#8221; (very nerdy voice): I am doing it like this, so it is obvious what is going on.  I want to show that you must populate the array with images.</p>
<p>So here is what is going on in this code:</p>
<p>We first create an array of <strong>UIImage</strong>s.  Next, we allocate our <strong>UIImageView</strong>.<strong> </strong>The next part is where the magic happens&#8230; Apple has given you a property of UIImageView that is an array of images.  The UIImageView class has a built in functionality to cycle through images at a given interval (hence animating them).</p>
<p>The next variable we see is the animation duration.  This is the number of seconds it takes to cycle through all of the images.  The default value for this is the number of images multiplied by 1/30.  This will give you a 30 fps frame rate.  Since we have 12 images and not 30, this duration would make our Ryu look like he was on crack.</p>
<p>In our case, the default would be 12 * (1/30) or .4 . We are going to slow this down significantly to 1.1.  Go ahead and play with this number when creating your animation.</p>
<p>The next variable is the <strong>contentMode</strong>.  The content mode determines how the image will fit inside the UIImageView frame.  Since our animation images vary in size, we just make the frame as large as the largest image and set the contentMode to UIViewContentModeBottomLeft.  What this means is, draw the image withough scaling and place it in the bottom left of the UIImageView.  Read up on contentMode to figure out what will be right for your applicaiton.</p>
<p>Finally, we just add the UIImageView to our main view and call the <strong>startAnimating </strong>method on it.  This will start the animation of these images.  There are also some other helpful methods you might use when doing animation.  They include <strong>stopAnimating</strong> and <strong>isAnimating</strong>.</p>
<h2>5. Make Sure The Device Starts In Landscape Mode</h2>
<p>In this example, we assumed that the device was in landscape mode.  Again, read <a href="../2009/07/09/integrating-twitter-into-your-applications/">this post</a> to see how to do this.  It involves adding<strong> Initial interface orientation</strong> to the info.plist file and adding this code to your viewController.m file.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>shouldAutorotateToInterfaceOrientation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIInterfaceOrientation<span style="color: #002200;">&#41;</span>interfaceOrientation <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Return YES for supported orientations</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>interfaceOrientation <span style="color: #002200;">==</span> UIInterfaceOrientationLandscapeLeft<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>That concludes our simple animation tutorial.  Post a question or <a href="http://twitter.com/brandontreb">ask me on Twitter</a> if you need help.  You can download the source for this tutorial <a href="http://icodeblog.com/wp-content/uploads/2009/07/AnimationApp.zip">here</a>. Happy iCoding! </p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/07/24/iphone-programming-tutorial-animating-a-game-sprite/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
<enclosure url="http://icodeblog.com/wp-content/uploads/2009/07/RuyPunch.mov" length="349110" type="video/quicktime" />
		</item>
		<item>
		<title>iPhone Game Programming Tutorial Part 4 &#8211; Basic Game Audio</title>
		<link>http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/</link>
		<comments>http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/#comments</comments>
		<pubDate>Mon, 04 May 2009 23:47:30 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone game programming tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=719</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/"><img align="left" hspace="5" width="150" height="150" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_01-150x150.png" class="alignleft wp-post-image tfe" alt="screenshot_01" title="screenshot_01" /></a>Ok, I will be finishing up this series by showing you guys how to add basic audio to your game.  Since this is a beginner tutorial, I won&#8217;t go into complex audio using OpenAL or Audio Queues.  Today, we will [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F05%2F04%2Fiphone-game-programming-tutorial-part-4-basic-game-audio%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F05%2F04%2Fiphone-game-programming-tutorial-part-4-basic-game-audio%2F&amp;source=elctech&amp;style=normal&amp;hashtags=iphone+development,iphone+game+programming+tutorial" height="61" width="50" /><br />
			</a>
		</div>
<p>Ok, I will be finishing up this series by showing you guys how to add basic audio to your game.  Since this is a beginner tutorial, I won&#8217;t go into complex audio using OpenAL or Audio Queues.  Today, we will just be playing short sounds (less than 30 seconds).  This will be the easiest way to do audio if you don&#8217;t require any background music (I guess you could do a nasty hack and loop a 30 sec clip but that would be shameful <img src='http://icodeblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).  The downfall of doing audio this way is only one audio file can be playing at a time.  Ok, let&#8217;s get started&#8230; Make sure you are starting with the base code from the <a href="http://icodeblog.com/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/">previous tutorial</a> (either use yours or download a fresh copy).  We will be using 2 sounds today, 1 for when the ball is hit and another for when someone scores a point.</p>
<p>Here are the sound files you will need to download for this tutorial:</p>
<ul>
<li><a style="text-decoration: none;" href="http://icodeblog.com/wp-content/uploads/2009/05/clapping-crowd-studio-01.caf">clapping-crowd-studio-01</a></li>
<li><a href="http://icodeblog.com/wp-content/uploads/2009/05/tennis-volley-01.caf">tennis-volley-01</a></li>
</ul>
<p>Notice that all of the audio files have a .caf extension.  This is the audio type all of your sound files must be in to be played by the iPhone.  Luckily Apple has provided us a utility for converting audio to this format. You can read up on it <a href="http://developer.apple.com/iphone/prerelease/library/codinghowtos/AudioAndVideo/index.html#PROCESSING-CONVERT_AN_AUDIO_FILE_TO_THE_PREFERRED_FORMAT_FOR_IPHONE_OS">here</a>.  It&#8217;s pretty straight forward, just open up a Terminal and type</p>
<p><strong>/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}</strong></p>
<p>Where {INPUT} is the path to your input audio file and {OUTPUT} is the file you want it to save it to with a .caf extension.</p>
<h2>Adding the Audio Files To You Project</h2>
<p>Start by downloading these files.  Then drag them to your Resources Folder inside of your project.  You may also want to add a subfolder called Audio to keep organized. Make sure you check the box to copy the files into your project&#8217;s directory.</p>
<p style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_01.png"><img class="alignnone size-full wp-image-724" title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_01.png" alt="screenshot_01" width="232" height="240" /></a></p>
<h2>Add The AudioToolbox.framework to Your Project</h2>
<p>Since we need to call functions within this framework, we will need to add it to our project. The best way I have found to locate a framework on my mac is to search for it.<span style="white-space:pre"> </span> Right Click on the <strong>Frameworks</strong> folder in your project and select <strong>Add -&gt; Existing Frameworks</strong>.  In the search box type <strong>AudioToolbox.framework </strong> and select the framework among the search results. (Make sure you have <strong>Computer</strong> selected to search your entire computer)</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_02.png"><img class="alignnone size-full wp-image-725" title="screenshot_02" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_02.png" alt="screenshot_02" width="532" height="427" /></a></p>
<p style="text-align: left; ">When you find it, click <strong>Add.</strong> We are now ready to start coding&#8230;</p>
<h2>Add the Header Declarations</h2>
<p>Open up <strong>iTennisViewController.h</strong> and add the following code.</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_11.png"><img class="alignnone size-full wp-image-729" title="screenshot_11" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_11.png" alt="screenshot_11" width="325" height="356" /></a></p>
<p style="text-align: left; ">First we see an import for the AudioServices.h. This is needed to call the functions required for us to play audio.  Next, we declare 2 SystemSoundID&#8217;s.  These are basically just Integers (could actually use an int instead but you&#8217;d get a warning).  Next, open up <strong>iTennishViewController.m</strong> and add the following code to synthesize these variables.</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_09.png"><img class="alignnone size-full wp-image-728" title="screenshot_09" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_09.png" alt="screenshot_09" width="258" height="27" /></a></p>
<p style="text-align: left; ">Now let&#8217;s initialize our sounds objects.  Go to the viewDidLoad method and add the following code.</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_12.png"><img class="size-full wp-image-730 aligncenter" title="screenshot_12" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_12.png" alt="screenshot_12" width="709" height="223" /></a></p>
<p style="text-align: left; ">
<p style="text-align: left; ">
<p style="text-align: left; ">
<p style="text-align: left; ">Pretty straight forward.  The first line gets the path of the audio file.  The next line converts the path to a CFURLRef.  Finally, we load that audio file and associate it with the given sound ID. Notice we pass &amp;clappingFileID.  This is a pass by reference and allows the AudioServicesCreateSystemSoundID method to modify this variable.  We do the same thing to load the volley sound.  Now, let&#8217;s play the sounds.</p>
<h2>Playing The Sounds</h2>
<p>It&#8217;s actually quite simple to play the sounds we have loaded.  Simply call the AudioServicesPlaySystemSound method and pass it the soundID of the file you want to play.  So to play the clapping sound when someone scores, add this code to your reset method.</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_14.png"><img class="alignnone size-full wp-image-731" title="screenshot_14" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_14.png" alt="screenshot_14" width="322" height="52" /></a></p>
<p style="text-align: left; ">Notice, we are passing the clappingFileID to indicate we want to play the clapping sound.  Now, add the following code to gameLoop to play the racquet sound when someone hits the ball.</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_15.png"><img class="alignnone size-full wp-image-732" title="screenshot_15" src="http://icodeblog.com/wp-content/uploads/2009/05/screenshot_15.png" alt="screenshot_15" width="381" height="209" /></a></p>
<p style="text-align: left; ">Pretty straight forward Right? Now click Build and Go and check out the game audio.  That concludes this tutorial.  If you have any questions or comments, feel free to leave them in the comments section of this post or write them to me on <a href="http://twitter.com/brandontreb">Twitter</a>.  You can also download the sample code <a href="http://icodeblog.com/wp-content/uploads/2009/05/itennis-4.zip">here</a></p>
<p>Happy iCoding!</p>
<p style="text-align: left; ">
<p style="text-align: left; ">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>iPhone Game Programming Tutorial Part 3 &#8211; Splash Screen</title>
		<link>http://icodeblog.com/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/</link>
		<comments>http://icodeblog.com/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 20:01:04 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=703</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/"><img align="left" hspace="5" width="150" height="150" src="http://icodeblog.com/wp-content/uploads/2009/03/splash-150x150.png" class="alignleft wp-post-image tfe" alt="splash" title="splash" /></a>Ok, So it&#8217;s time to wrap up this series.  Today I will be showing you how to add a few &#8220;bells and whistles&#8221; that will make your game more complete.  Let&#8217;s start by adding a splash screen to your game [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F03%2F18%2Fiphone-game-programming-tutorial-part-3-splash-screen%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F03%2F18%2Fiphone-game-programming-tutorial-part-3-splash-screen%2F&amp;source=elctech&amp;style=normal&amp;hashtags=%23iphonedev" height="61" width="50" /><br />
			</a>
		</div>
<p>Ok, So it&#8217;s time to wrap up this series.  Today I will be showing you how to add a few &#8220;bells and whistles&#8221; that will make your game more complete.  Let&#8217;s start by adding a splash screen to your game (Again I truly apologize for the lack of graphics skillz. Photoshop and I are not friends).</p>
<h3>Creating a Splash Page</h3>
<p>We will be adding a splash page that will fade out into our main game screen.  Start by downloading this image and adding it to your project&#8217;s <strong>Resources</strong> Folder. Make sure you check the box to copy this image to the project&#8217;s directory.</p>
<p style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/03/splash.png"><img class="alignnone size-full wp-image-704" title="splash" src="http://icodeblog.com/wp-content/uploads/2009/03/splash.png" alt="splash" width="320" height="480" /></a></p>
<p style="text-align: left;">Now we need to add a View Controller to our project that will handle the Splash View.  Go ahead and add a new file to your project that is a <strong>UIViewController </strong>subclass. Name this file <strong>SplashViewController.</strong> Make sure you check to create the .h file as well.</p>
<p style="text-align: left;">
<div style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_01.png"><img class="alignnone size-full wp-image-705" title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_01.png" alt="screenshot_01" width="600" height="442" /></a></div>
<div style="text-align: left;">Next, we need to change our AppDelegate to load this view controller instead of our main view controller.  Open up <strong>iTennisAppDelegate.h </strong>and change it to look like this:</div>
<div style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_02.png"><img class="alignnone size-full wp-image-706" title="screenshot_02" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_02.png" alt="screenshot_02" width="466" height="194" /></a></div>
<div style="text-align: left;">We are basically replacing iTennisViewController with SplashViewController.  Next, open up <strong>iTennisAppDelegate.m </strong>and change it to look like this:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_03.png"><img class="alignnone size-full wp-image-707" title="screenshot_03" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_03.png" alt="screenshot_03" width="418" height="365" /></a></div>
<div style="text-align: left; ">Again, all we are really doing is changeing iTennisViewController with SplashViewController.  This is because we want to load the splash page initially and not the main game screen.  One main difference to note here is we are allocating a new instance of the SplashViewController. We didn&#8217;t have to do that with the iTennisViewController because it was being loaded from a nib and our application initialized it for us.  Since we are building SplashViewController programatically (without a nib), we need to instantiate it.  Next, let&#8217;s implement the Splash View.  Open up <strong>SplashViewController.h</strong> and add the following code:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_04.png"><img class="alignnone size-full wp-image-708" title="screenshot_04" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_04.png" alt="screenshot_04" width="406" height="231" /></a></div>
<div style="text-align: left; ">Let me explain what&#8217;s going on here.  First, we see an NSTimer.  This will be used to display the splash page for a certain amount of time before fading to our main game screen.  Next, there is a UIImageView.  This will simply be the imageview of our splash image.</div>
<div style="text-align: left; ">Finally, we see the <strong>iTennisViewController</strong>.  This is the view controller that we replaced inside of our application&#8217;s delegate.  We will be loading it from our splash view. Now, open up <strong>SplashViewController.m</strong> and add the following code:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_05.png"><img class="alignnone size-full wp-image-709" title="screenshot_05" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_05.png" alt="screenshot_05" width="301" height="25" /></a></div>
<div style="text-align: left; ">This is just synthesizing all of our properties.  Now, add the following code to our <strong>l</strong><strong>oadView</strong> method:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_07.png"><img class="alignnone size-full wp-image-711" title="screenshot_07" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_07.png" alt="screenshot_07" width="761" height="259" /></a></div>
<div style="text-align: left; ">Lot&#8217;s of new code here.  First off, since we are loading this view programatically without a nib, we have to create the view.  So we get the frame that the application is running in and use it to allocate a new view.  Then we set the view of the SplashViewController to this newly created view.  We have to create the frame to basically tell the application to create a view that is 320&#215;480.</div>
<div style="text-align: left; ">The next thing we do is create the splashImageView from the Splash.png image.  We also need to create a frame for this images.  Think of a frame as an empty container that we will put our image in.  Next we add the imageview to our main view.  This will display it immediately.</div>
<div style="text-align: left; ">Next, we initialize our view main controller by passing it the nib it will load from.  Next, the view&#8217;s alpha transparency is set to 0.0.  This makes it completely invisible.  Finally, we add it to our view.  Note that it is on top of the splashimageview but is not visible because the alpha transparency is set to 0.0.</div>
<div style="text-align: left; ">Finally, we start the timer.  This will show the splash screen for 2 seconds before calling the &#8220;fadeScreen&#8221; method.  Let&#8217;s implement the fadescreen method.  I must note that I took the fadescreen method from <a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/12998-how-fade-between-views-transitioning-through-black.html">this post</a>.</div>
<div style="text-align: left; ">Add the following code:</div>
<div style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_08.png"><img class="alignnone size-full wp-image-712" title="screenshot_08" src="http://icodeblog.com/wp-content/uploads/2009/03/screenshot_08.png" alt="screenshot_08" width="578" height="317" /></a></div>
<div style="text-align: left;">Lots of animation stuff.  Pretty well commented so I wont go into it too much.  Basically, we fade the view out in fadeScreen, then it calls finishedFading when its done.  Finished fading fades the view back in as well as fades viewController&#8217;s view back in.  It will now display our main view.  Make sure you remove the splash from the superview or you will get a weird transition.</div>
<div style="text-align: left;">That&#8217;s it for the splash page.  You can Build and Go to see the view transition from a splash to the main game.</div>
<p>That&#8217;s all for today.  Join me next time when I will show you how to add audio to your game. If you have any questions or comments, feel free to leave them in the comments section or <a href="http://twitter.com/brandontreb">write me on Twitter</a>.</p>
<p>You can download the source for this tutorial <a href="http://icodeblog.com/wp-content/uploads/2009/03/itennis-3.zip">here</a></p>
<p>Happy iCoding! </p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/feed/</wfw:commentRss>
		<slash:comments>106</slash:comments>
		</item>
		<item>
		<title>iPhone Game Programming Tutorial Part 2- User Interaction, Simple AI, Game Logic</title>
		<link>http://icodeblog.com/2009/02/18/iphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic/</link>
		<comments>http://icodeblog.com/2009/02/18/iphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 22:35:25 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[iphone game tutorial]]></category>
		<category><![CDATA[itennis]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=656</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/02/18/iphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic/"><img align="left" hspace="5" width="150" height="150" src="http://icodeblog.com/wp-content/uploads/2009/02/screenshot_01-150x150.jpg" class="alignleft wp-post-image tfe" alt="screenshot_01" title="screenshot_01" /></a>Ok folks, here it is. The next tutorial in our iPhone game programming tutorial (sorry for the delay).  Today, I will be discussing the basics of player interaction, simple game AI, and game logic.  We will also be exploring how [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F02%2F18%2Fiphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F02%2F18%2Fiphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic%2F&amp;source=elctech&amp;style=normal&amp;hashtags=game,iPhone+Game+Programming,iphone+game+tutorial,itennis" height="61" width="50" /><br />
			</a>
		</div>
<p>Ok folks, here it is. The next tutorial in our iPhone game programming tutorial (sorry for the delay).  Today, I will be discussing the basics of player interaction, simple game AI, and game logic.  We will also be exploring how to do simple collision detection so we know when the ball hits a paddle. Per popular request, I will be adding &#8220;Challenges&#8221; to the bottom of the tutorials from now on to give some more advanced ideas for improvement. Let&#8217;s begin. Start by opening your code from part 1&#8230;</p>
<h3>User Interaction</h3>
<p>The first thing we will implement is user interaction.  All we really want to do is move the paddle&#8217;s X location to the X location of the touch from the user.  This will be a very simple implementation and could be much better (I will add this as a challenge at the bottom of the tutorial).  Open <strong>iTennisViewController.m </strong>and add the following code.</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/screenshot_01.jpg"><img class="alignnone size-full wp-image-657" title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2009/02/screenshot_01.jpg" alt="screenshot_01" width="442" height="218" /></a></p>
<p style="text-align: left; ">Just as we did in a previous tutorial, we are overriding the <strong>touchesMoved</strong> method.  This will detect when the user &#8220;drags&#8221; their finger on the screen.  First, I added the &#8220;else if&#8221; statement inside of <strong>touchesBegan</strong> that simply forwards all events to <strong>touchesMoved</strong> if the game is in a running state.</p>
<p style="text-align: left; ">The first 2 lines inside of <strong>touchesMoved </strong>simply detect the location of the user&#8217;s touch.  Next, we need to create a new CGPoint from the X location of the touch and the Y location of the yellow racquet (player racquet).  Objective-C won&#8217;t simply let us say racquet_yellow.center.x = location.x.  This is probably because CGPoint is immutable (not editable).</p>
<p style="text-align: left; ">Finally, the center of the player&#8217;s racquet is set to our new location.</p>
<h3>Collision Detection</h3>
<p>*Update, the user Naren has pointed out a much simpler collision detection.  The code has been updated to reflect it. Inside the gameLoop method of iTennisViewController.m add the following code</p>
<p style="text-align: center; "><span style="color: #0000ee; text-decoration: underline;"><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-1.png"></a><a href="http://icodeblog.com/wp-content/uploads/2009/02/screenshot_011.jpg"><img class="alignnone size-full wp-image-677" title="screenshot_011" src="http://icodeblog.com/wp-content/uploads/2009/02/screenshot_011.jpg" alt="screenshot_011" width="370" height="175" /></a></span></p>
<p style="text-align: left; ">So, Apple has provided us with a very handy methods to check if to object frames collide.  Its called <strong>CGRectIntersectsRect.</strong>  We simply hand this method the frame of our ball and racquet.  When the ball collides with the racquet, we want to reverse its Y velocity.  The next if statement is required because sometimes we get in a state where the ball gets &#8220;trapped&#8221; on a paddle bouncing back and for and not going anywhere.  So, we want to ensure the ball&#8217;s velocity only gets reversed if it is front of each racquet. (Note, the NSlog is not needed, it was just for debugging)</p>
<h3>Simple Game AI</h3>
<p style="text-align: left; ">Next, we are going to discuss how simple AI can be added to allow a computer player to play iTennis with you.  Many of you might not know just how much is involved in a decent Artificial intelligence.  I could go on and on with nerdy math, philosophy, heuristics and the like, but I&#8217;m not.  I will show you some super NOOB, very easy to understand game AI.  Basically, the computer will &#8220;watch&#8221; the ball and move in the direction of it in hopes of hitting it.  Let&#8217;s get started&#8230; </p>
<p style="text-align: left; ">We first need to define a constant that will define how fast the computer player can move.  Add the following define to the top of iTennisViewController.m</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-2.png"><img class="alignnone size-full wp-image-660" title="picture-2" src="http://icodeblog.com/wp-content/uploads/2009/02/picture-2.png" alt="picture-2" width="156" height="20" /></a></p>
<p style="text-align: left; ">As you start testing, you can adjust this number.  This is basically defining how fast the computer player can move in order to get to the ball.  The higher you make this number, the &#8220;better&#8221; the computer player will be.  You could actually make your computer unbeatable if this number were high enough.  Now, add the following code right under your collision detection code:</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-3.png"><img class="alignnone size-full wp-image-661" title="picture-3" src="http://icodeblog.com/wp-content/uploads/2009/02/picture-3.png" alt="picture-3" width="663" height="174" /></a></p>
<p style="text-align: left; ">The first &#8220;if&#8221; statement is to add some difficulty for the computer.  It basically checks to see if the ball is on &#8220;his&#8221; side of the court.  The computer will not move or respond to the ball unless it&#8217;s on his side.  It could be omitted, but makes for a more interesting game.  The next if statements check to see if the X coordinates of the center of the ball are different than the X coordinates of the center of the racquet.  If the ball is to the right of the computer&#8217;s racquet, the X coordinate of the computer&#8217;s racquet is increased by <strong>kCompMoveSpeed</strong>. If the ball is to the left of the computer&#8217;s racquet, the X coordinate of the computer&#8217;s racquet is decreased by <strong>kCompMoveSpeed. </strong></p>
<p style="text-align: left; ">It should now be clear how adjusting the kCompMoveSpeed variable, will affect the computer&#8217;s performance&#8230;</p>
<p style="text-align: left; ">Now, you can actually hit Build and Go to see the game in action.  As you can see the computer responds to the ball and hits it most of the time.  There is still one last piece to make the game fun. Scoring!</p>
<h3>Game Mechanics: Scoring</h3>
<p style="text-align: left; ">Now, we need a way to keep score.  This is actually some more simple collision detection.  We are basically checking to see if the ball collides with the back walls.  First, let&#8217;s define a few variables and a method.  Open up iTennisViewController.h.  Add the following highlighted code:</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-4.png"><img class="alignnone size-full wp-image-662" title="picture-4" src="http://icodeblog.com/wp-content/uploads/2009/02/picture-4.png" alt="picture-4" width="398" height="450" /></a></p>
<p style="text-align: left; ">We need integer representations of the score so we can add points when the player and the computer score.  Also, we will need a function called reset that will be called to reset the ball to the center of the screen.  Now we need to define one more variable.  Add the following line to your defines at the top of iTennisViewController.m</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-5.png"><img class="alignnone size-full wp-image-663" title="picture-5" src="http://icodeblog.com/wp-content/uploads/2009/02/picture-5.png" alt="picture-5" width="132" height="18" /></a></p>
<p style="text-align: left; ">This variable is pretty self explanatory, but in case you didn&#8217;t catch on, it defines the amount of points needed to win.  I just made it 5 for a quick game.  Now, let&#8217;s check to see if a player or computer scored.  Right after your AI code, add the following code:</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-6.png"><img class="alignnone size-full wp-image-664" title="picture-6" src="http://icodeblog.com/wp-content/uploads/2009/02/picture-6.png" alt="picture-6" width="346" height="150" /></a></p>
<p style="text-align: left; ">Ok, first we see 2 if statements.  The are basically checking to see if the ball has hit/passed the top or bottom of the screen.  If it passes the top, the player score gets incremented.  If it passes the bottom, the computer score gets incremented.  The next line contains a little bit of fanciness if you are new to programming.  We are calling the reset function, but what is that expression we are passing to it? </p>
<p style="text-align: left; ">Well, if you look at the definition of reset, it takes a BOOL value that determines if the game is over.  So this can either be true or false.  We are simply passing true or false when the expression is evaluated.  So, (player_score_value &gt;= kScoreToWin) will evaluate to false when the player_score_value variable is less than 5.  Once this variable reaches 5, it will return true and pass it into the reset function.</p>
<p style="text-align: left; ">Why do this? Well it saves us lines of code and complexity.  So now you don&#8217;t have to do if(player_score_value &gt;= kScoreToWin) ) [self reset:YES]; }else{[self reset:NO];} . make sense?</p>
<p style="text-align: left; ">Now, let&#8217;s define the reset function. Add the following method to iTennisViewController.m</p>
<p style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/02/picture-7.png"><img class="alignnone size-full wp-image-665" title="picture-7" src="http://icodeblog.com/wp-content/uploads/2009/02/picture-7.png" alt="picture-7" width="496" height="272" /></a></p>
<p style="text-align: left; ">The first thing we do is pause the game.  Remember pausing the game causes the &#8220;Tap to Begin&#8221; Message to Display. Next, we center the ball on the screen.  If YES/true was passed in for the newGame variable, we need to do a few things.  First, we check who won by comparing the computer and player scores.  Next, we update the &#8220;tapToBegin&#8221; message to notify the player who won.  You could add another label for this, but I am just recycling this one&#8230; Finally, we reset the player and computer scores to 0 because a new game is starting.</p>
<p style="text-align: left; ">If it is not a new game, we need to reset the tapToBegin message to display &#8220;tapToBegin&#8221;.  This has to be done in case the message was altered to say &#8220;Player/Computer wins!&#8221;. Finally, we update the labels on the screen to reflect the new scores.  Now you should be good to go&#8230;</p>
<p style="text-align: left; ">Click Build and Go and battle the computer in an epic game of iTennis!  If you have any comments or questions, leave them here or <a href="http://twitter.com/brandontreb">ask me on Twitter</a>.  You can also download the source for this tutorial <a href="http://icodeblog.com/wp-content/uploads/2009/02/itennis-2.zip"></a><a href="http://icodeblog.com/wp-content/uploads/2009/02/itennis-21.zip">here</a></p>
<h3>As Promised, Here are some challenges</h3>
<p> </p>
<ul>
<li>Improve on user interaction &#8211; Make it so when the user taps, the racquet moves towards the tap rather than moves directly to the tap location</li>
<li>Improve collision detection &#8211; When the ball hits the paddle, use some simple physics to make the speed of the paddle affect the speed (and direction) of the ball</li>
<li>Improve on the AI &#8211; add some randomness to your AI, make it attempt to &#8220;predict&#8221; where the ball is going to be</li>
<li>Improve Scoring &#8211; Make it used tennis scores 15, 30 , etc&#8230;</li>
<li>Improve scoring &#8211; Make it so you must win by 2 points</li>
</ul>
<p> </p>
<p style="text-align: left; ">Stay tuned for the next tutorial when I will be going over game audio, splash screen, about, and some other polishing&#8230; Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/02/18/iphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic/feed/</wfw:commentRss>
		<slash:comments>137</slash:comments>
		</item>
		<item>
		<title>iPhone Game Programming Tutorial &#8211; Part 1</title>
		<link>http://icodeblog.com/2009/01/15/iphone-game-programming-tutorial-part-1/</link>
		<comments>http://icodeblog.com/2009/01/15/iphone-game-programming-tutorial-part-1/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 22:19:53 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=572</guid>
		<description><![CDATA[<a href="http://icodeblog.com/2009/01/15/iphone-game-programming-tutorial-part-1/"><img align="left" hspace="5" width="150" height="150" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_011-150x150.jpg" class="alignleft wp-post-image tfe" alt="screenshot_011" title="screenshot_011" /></a>This is the first in a multipart series where I will be teaching you to create an iPhone game from the ground up.  As many of you know, OpenGL and Quartz have quite a steep learning curve.  This tutorial will [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2009%2F01%2F15%2Fiphone-game-programming-tutorial-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2009%2F01%2F15%2Fiphone-game-programming-tutorial-part-1%2F&amp;source=elctech&amp;style=normal&amp;hashtags=%23iphonedev" height="61" width="50" /><br />
			</a>
		</div>
<p>This is the first in a multipart series where I will be teaching you to create an iPhone game from the ground up.  As many of you know, OpenGL and Quartz have quite a steep learning curve.  This tutorial will simplify the development process and not use either one of those technologies.  For our graphics, we will simply use UIImageViews.  </p>
<p>In this tutorial series, I will teach you about every aspect of developing an iPhone game.  The game will include graphics, sounds, game mechanics, and even some simple computer AI. </p>
<p>The game we will be creating is called iTennis.  It is essentially Pong with a tennis theme.  It will follow all of the same rules and mechanics of Pong.  I chose this as a first game tutorial as iTennis does not require any special animation.  If there is significant interest, I will move on to a more complex game in a later series. Here is a screenshot of the game we will be making:</p>
<p> </p>
<p style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_011.jpg"><img class="alignnone size-full wp-image-609" title="screenshot_011" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_011.jpg" alt="screenshot_011" width="386" height="742" /></a></p>
<p>Let&#8217;s get started&#8230; Here are the images you will need for the project. (I know my Photoshop skillz are weak, so be easy on me).  Click the link below to download the zipped images.<br />
   </p>
<p> </p>
<dt class="wp-caption-dt" style="text-align: center;"><a href="http://icodeblog.com/wp-content/uploads/2009/01/itennis_images.zip"><img class="size-full wp-image-580" title="iTennis_Images.zip" src="http://icodeblog.com/wp-content/uploads/2009/01/packageicon.png" alt="iTennis_Images.zip" width="123" height="113" /></a></dt>
<p> </p>
<p>Open up XCode and create a View Based Application. Name this application iTennis.</p>
<p> </p>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_01.jpg"><img class="alignnone size-full wp-image-583" title="screenshot_01" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_01.jpg" alt="screenshot_01" width="600" height="442" /></a></div>
<div style="text-align: left; ">After you unzip all of the images, drag them into the project Resources folder.  After you have done this, the resources folder should look like this.</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_02.jpg"><img class="alignnone size-full wp-image-584" title="screenshot_02" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_02.jpg" alt="screenshot_02" width="240" height="306" /></a></div>
<div style="text-align: left; ">Now we are ready to start coding.  We need to first establish our IBOutlet connections so that we can interact with our images in code.  We will also add some of the game variables that we will be using in this tutorial.  Open up <strong>iTennisViewController.h </strong>and add the following code:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_05.jpg"><img class="alignnone size-full wp-image-585" title="screenshot_05" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_05.jpg" alt="screenshot_05" width="397" height="405" /></a></div>
<div style="text-align: left; ">As you can see, we have quite a few IBOutlets.  This is because there are many objects we need to interact with.  For now, ignore the ballVelocity and gameState variables.  Now, we are ready to build our interface.  Double click on the file <strong>iTennishViewController.xib </strong>to open it up inside of Interface Builder.  Here is a video of me setting up the interface.</div>
<div style="text-align: left; ">
[See post to watch QuickTime movie]</div>
<div style="text-align: left; ">Now that you have made the connections, close Interface Builder and return to XCode.  We need to set up some constants.  You can hardcode these values in somewhere, but making them constants makes them easier to change when you are tweaking the mechanics of your game. Open up <strong>iTennisViewController.m</strong> and add the following code.</div>
<div style="text-align: center; "><strong><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_06.jpg"><img class="alignnone size-full wp-image-586" title="screenshot_06" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_06.jpg" alt="screenshot_06" width="656" height="153" /></a></strong></div>
<div style="text-align: left; ">Let me explain the purpose of the constants.  The first 2 are game states.  We use these to determine what is to be shown on screen.  Is the game paused, is it running, is it at the title screen?  We will add more states later on in the game.  The next 2 variables are speeds for the ball to travel.  Since the game is 2D, the ball can have an X and Y speed to make up its velocity vector.  The next line synthesizes the variables we will use to create their getter and setter methods automatically.</div>
<div style="text-align: left; ">Now let&#8217;s write the viewDidLoad method. Add the following code:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_07.jpg"><img class="alignnone size-full wp-image-587" title="screenshot_07" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_07.jpg" alt="screenshot_07" width="701" height="115" /></a></div>
<div style="text-align: left; ">First, we are setting our game state to a paused game state.  This is so the game doesn&#8217;t start right away when the view loads.  Next, we create a vector for the ball&#8217;s velocity.  Adjust the constants that we defined earlier for a faster or slower ball speed.  Finally, we create an NSTimer instance.  The first parameter is the timestep.  We have set the time step to 0.05 seconds.  Adjusting this will change the speed of the entire game. Next we set the target.  This is telling the timer where the callback is located.  The callback is the next parameter.  Basically, we are telling the timer to call self.gameLoop every 0.05 seconds.  We don&#8217;t need to worry about the userInfo param.  The last parameter just tells the timer to repeat.</div>
<div style="text-align: left; ">Now that we have initialized our game, let&#8217;s create the game loop.  Add the following method:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_08.jpg"><img class="alignnone size-full wp-image-588" title="screenshot_08" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_08.jpg" alt="screenshot_08" width="609" height="263" /></a></div>
<div style="text-align: left; ">First, we are checking to see if the game is in a running state (we don&#8217;t want to move the ball in a paused state).  If the game is not running, we will show the tapToBegin label.  This is just the label defined earlier that tells the user to tap the screen to begin.</div>
<div style="text-align: left; ">If the game is running, we start by moving the ball according to its velocity vector.  The next few lines do some bounds checking.  If the ball hits the side of the screen, we want to reverse its velocity so that it &#8220;bounces&#8221;.  Without this code, the ball would fly off the screen.</div>
<div style="text-align: left; ">The last bit of code we will need to write is the touchesBegan method.  We will use this method to start the game when the game is in a paused state.  Add the following code:</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_09.jpg"><img class="alignnone size-full wp-image-589" title="screenshot_09" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_09.jpg" alt="screenshot_09" width="409" height="91" /></a></div>
<div style="text-align: left; ">What this does is first checks to see if we are in a paused state.  If so, we hide the tapToBegin label and set the game in a running state.  This will get the game running.  Eventually, we will write the code to control the player paddle in this function.</div>
<div style="text-align: left; ">Finally, being good iPhone developers, we need to clean up the resources that we allocated.  Add the following code to the dealloc method.</div>
<div style="text-align: center; "><a href="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_10.jpg"><img class="alignnone size-full wp-image-590" title="screenshot_10" src="http://icodeblog.com/wp-content/uploads/2009/01/screenshot_10.jpg" alt="screenshot_10" width="184" height="135" /></a></div>
<div style="text-align: left; ">That concludes part 1 of our game development tutorial series.  If you have any questions or comments, feel free to leave them in the comments section of this post.  You can download the source code <a href="http://icodeblog.com/wp-content/uploads/2009/01/itennis.zip">here</a>. Happy iCoding!</div>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/01/15/iphone-game-programming-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>237</slash:comments>
<enclosure url="http://icodeblog.com/wp-content/uploads/2009/01/Game_1.mov" length="14481335" type="video/quicktime" />
		</item>
	</channel>
</rss>
