<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ziray Studio Labs &#187; code</title>
	<atom:link href="http://www.mikeziray.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikeziray.com</link>
	<description>Life, Logged Through Light</description>
	<lastBuildDate>Mon, 26 Jul 2010 13:40:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Building a Game</title>
		<link>http://www.mikeziray.com/2010/07/09/building-a-game/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2010/07/09/building-a-game/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 14:29:48 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[ansca]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[corona]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=1047</guid>
		<description><![CDATA[A lot of my peers went to school to become game developers. Instead, most went on to work for big software companies in northern Virginia. I studied human-computer interaction and wanted to fix everything from poorly designed user interfaces to frustratingly complex household items. Instead, I became a Mac, iPhone, Flash and game developer. In [...]<p><a href="http://www.mikeziray.com/2010/07/09/building-a-game/">Building a Game</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/' rel='bookmark' title='Permanent Link: Corona Game Template in Lua'>Corona Game Template in Lua</a></li>
<li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>A lot of my peers went to school to become game developers. Instead, most went on to work for big software companies in northern Virginia. I studied human-computer interaction and wanted to fix everything from poorly designed user interfaces to frustratingly complex household items. Instead, I became a Mac, iPhone, Flash and game developer. In a way, we both went where the money was.</p>
<h2>How to start your game</h2>
<p>I&#8217;ll assume you already have a game idea. No matter how ambitious you want to be though, think in milestones</p>
<ol>
<li>Display some graphics.</li>
<li>Animate your graphics, either manually, or with basic input controls</li>
<li>Get hit detection working.</li>
<li>Everything else! (level loading, artificial intelligence, etc.)</li>
</ol>
<p>If you can even make it to the fourth milestone, you&#8217;re farther than 90% of most game developers. If you make it past milestone four, you&#8217;re farther than 99% of game developers. Only the passionate, motivated and able ship. This is how I&#8217;ve started all of my games and it&#8217;s important to achieve tangible results to keep yourself motivated. In fact, this same step-by-step process can be used in regular application development as well.</p>
<h2>Design</h2>
<p>Start your development with a skeleton application. This does one of two things: first, it forces you to plan the architecture of your application by creating the objects (in files) that you&#8217;ll need in your development and second, it provides an outline for you to work from. So first, think about what display controllers and object controllers you&#8217;ll need, what model objects you&#8217;ll need and then go ahead and create the files for those. Also go ahead and fill in some functions too for the basic functionality. The point is, make sure you break up the functionality into bite size chunks. It&#8217;ll make your life easier, trust me.</p>
<p>Put in the functions you think you&#8217;ll want and then just put in print or trace statements to make sure they&#8217;re being called correctly. Then, just build it piece by piece, usually starting with getting something like a player character on the screen (DisplayController, PlayerController, GameController), then getting him to move, moving him via inputs (InputController), then applying constraints like hit detection or getting him to jump (implementing basic gravity), etc, etc. Just keep introducing and implementing new features, one at a time, playing the game, realizing you want to tweak something and playing it some more. This is called <a href="http://en.wikipedia.org/wiki/Iterative_design" target="_blank">iterative design</a>.</p>
<p>Get those milestones done as quickly as possible, keeping your code organized in your skeleton application. Don&#8217;t worry about graphics at first. Personally, I stick in multi-colored boxes instead spending a lot of time on graphics I may not use in the end. Objects will be added as you realize you need them, but the idea is to keep it organized, simple and cohesive.</p>
<p>When designing your class structures and diagrams, keep the low level functionality, like file access and keyboard inputs, abstracted and hidden in wrapper classes. The reason you abstract objects is for a couple of reasons: One is to be able to add in hooks before and after a part of code. For example, instead of calling the system clock directly, you want to calculate the elapsed time if your game was paused or suspended on the computer. If you abstracted this in the beginning with a GameClock object, you&#8217;d only have to change it once, in a single place. If you called it directly, you&#8217;d have to go back to every place in your code and calculate that value. Keep object responsibilities where they belong!</p>
<h2>Start Programming!</h2>
<p>To start you off, check out <a href="http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/?utm_campaign=feed&utm_medium=feed&utm_source=blog" target="_blank">a game template </a>designed for the <a href="http://anscamobile.com/corona/" target="_blank">Corona SDK</a> (free 30 day trial), written in Lua. I recommend you create a template of all your project tyes as it&#8217;ll help jump start your next big idea and also keep you organized from the start.</p>
<p><a href="http://www.mikeziray.com/2010/07/09/building-a-game/">Building a Game</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/' rel='bookmark' title='Permanent Link: Corona Game Template in Lua'>Corona Game Template in Lua</a></li>
<li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2010/07/09/building-a-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>Why Your Code Sucks &#8211; Naming Conventions</title>
		<link>http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 19:38:04 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[advice]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[blacksburg]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[virginia]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=1071</guid>
		<description><![CDATA[I&#8217;ve seen a lot of poorly written and ugly looking code in my time. That&#8217;s not even considering the undocumented and uncommented code. We all know we should comment our code but how many of us do it consistently? I admit, when I&#8217;m in a coding frenzy, stopping to write comments just gets in the [...]<p><a href="http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/">Why Your Code Sucks &#8211; Naming Conventions</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/' rel='bookmark' title='Permanent Link: Corona Game Template in Lua'>Corona Game Template in Lua</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/' rel='bookmark' title='Permanent Link: Dynamic Textfields in Flash AS3 and Their Bugs'>Dynamic Textfields in Flash AS3 and Their Bugs</a></li>
<li><a href='http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/' rel='bookmark' title='Permanent Link: Custom Mouse Cursor for ActionScript 3'>Custom Mouse Cursor for ActionScript 3</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen a lot of poorly written and ugly looking code in my time. That&#8217;s not even considering the undocumented and uncommented code. We all know we should comment our code but how many of us do it consistently? I admit, when I&#8217;m in a coding frenzy, stopping to write comments just gets in the way of my thought process and can be distracting. I will however write a bunch of comments before I write the code, of the operations I think I need, in order to give me an outline of where I need to get to. For example:</p>
<div class="codesnip-container" >// Get URL string for video file<br />
// Load video file from URL request<br />
// Load video file into video player<br />
// Set up video player<br />
// Play video</div>
<p>I may not know all the APIs to load and play the video just yet, but at least I&#8217;ve given myself an outline and as I fill in the code below each comment, I can see exactly what I&#8217;m doing and where I need to go.</p>
<h2>Why Your Code Sucks</h2>
<p>The point of this article is not to talk about comments, but how to avoid writing them in the first place within your functions and still be clear for everyone else, for the most part. This is your code:</p>
<div class="codesnip-container" >newPlPt = crt2pl( nmc.x, nmc.y );</div>
<p>Not even a comment could concisely convey the meaning of that hideous statement. Instead, why not:</p>
<div class="codesnip-container" >newPolarPoint = cartesianToPolar( newMediaContext.x, newMediaContext.y );</div>
<p>Well written code is self documenting. Let me restate that: if you can write code so that someone can jump to any point of that code and understand what&#8217;s going on at that point, read it like a sentence, and not have to decipher minute details like what each variable means, then you don&#8217;t have to comment most of your code.</p>
<p>There&#8217;s no reason you need to shorten the names in your code. Most programmers have never had file size limitations to deal with, so there&#8217;s no reason for it. Stop pretending you live in the 1960s and embrace nearly infinite file storage. To save keystrokes perhaps? Please, use a real IDE with code completion and stop doing stupid things like:</p>
<div class="codesnip-container" >public function updateP( p:Player, d:MovieClip, b:MovieClip, t:Textfield);</div>
<h2>Function Names</h2>
<p>Also, because your functions are essentially actions, they need to reflect that in the name, so put a verb in the beginning of your function such as: getData(), setStatus(), enableWiFi(), hideControls(), handleGraphicException(), launchBall(), etc.</p>
<h2>Class Variables</h2>
<p>Get rid of the underscores in front of your class variables. For example: _dg; _myNumber;. All class variables should be private anyway, so why do you need that ridiculous convention? It&#8217;s a hold over from C where there was no &#8220;private&#8221; keyword, so you&#8217;re using it and you have no idea why.</p>
<p>Also, what&#8217;s with the &#8220;my&#8221; naming convention? You sound stupid when you have those: myInstanceName, myMovieClip, myGraphicsContext. Of course it&#8217;s yours, whose would it be, if not yours? If you&#8217;re programming with a colleague, do you refer to his variable references as yourInstanceName? Or hisVideoFileURL? Of course not. Don&#8217;t be that stupid; you&#8217;re reading too many stupid online tutorials by uncreative people who only code because they have nothing else better to do while living in their parent&#8217;s basement.</p>
<p>In the very least, be consistent with your naming conventions.</p>
<h2>Hungarian vs. Polish notation</h2>
<p>Depending on which language you&#8217;re coding in,  you may need to use a notation to help you with type casting. Let me rephrase that: if you&#8217;re using a loosely typed language, use Hungarian notation. You should probably use this with strongly typed languages anyway because with abstract types, you never know what you could get into and it&#8217;s just generally less confusing.</p>
<p><strong>Hungarian Notation</strong>: vendorNameTextField or vendorName_txt</p>
<p><strong>Polish Notation</strong>: txtVendorName or textFieldVendorName</p>
<p>Why not Polish notation? Not only is it ugly, but why would I sort on variable type instead of the variable name like I can do in Hungarian notation? The notation names come from how the speakers of those languages modify their verbs and nouns. To say: &#8220;My ball&#8221; in Hungarian is:  &#8221;lab<em>á</em>m&#8221; where &#8220;labda&#8221; is the root word and the &#8216;m&#8217; singifies a first person possessive. Hence the ending of the word shows the crucial information. It&#8217;s the opposite in Polish notation where the beginning of the word is modified. In the interest of full disclosure, I love the Polish, but I am Hungarian, but I promise that&#8217;s not why I prefer one over the other.</p>
<h2>Final Thoughts</h2>
<p>In the end, be consistent, be clear and spell out your variable and function names.</p>
<p><a href="http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/">Why Your Code Sucks &#8211; Naming Conventions</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/' rel='bookmark' title='Permanent Link: Corona Game Template in Lua'>Corona Game Template in Lua</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/' rel='bookmark' title='Permanent Link: Dynamic Textfields in Flash AS3 and Their Bugs'>Dynamic Textfields in Flash AS3 and Their Bugs</a></li>
<li><a href='http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/' rel='bookmark' title='Permanent Link: Custom Mouse Cursor for ActionScript 3'>Custom Mouse Cursor for ActionScript 3</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
		<item>
		<title>Corona Game Template in Lua</title>
		<link>http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 00:18:29 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[ansca]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[corona]]></category>
		<category><![CDATA[game edition]]></category>
		<category><![CDATA[game template]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[ipod touch]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=1048</guid>
		<description><![CDATA[This is a template I threw together for the Corona SDK from Ansca Mobile. Since Lua is not a language most people are familiar with, I figured I&#8217;d throw together some sample code for Corona and share what I learned so far with the rest of the Corona SDK community by including the template here. [...]<p><a href="http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/">Corona Game Template in Lua</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2010/07/09/building-a-game/' rel='bookmark' title='Permanent Link: Building a Game'>Building a Game</a></li>
<li><a href='http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/' rel='bookmark' title='Permanent Link: Why Your Code Sucks &#8211; Naming Conventions'>Why Your Code Sucks &#8211; Naming Conventions</a></li>
<li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This is a template I threw together for <a href="http://anscamobile.com/corona/" target="_blank">the Corona SDK from Ansca Mobile</a>. Since Lua is not a language most people are familiar with, I figured I&#8217;d throw together some sample code for Corona and share what I learned so far with the rest of the Corona SDK community by including the template here.</p>
<p>The template includes example code for:</p>
<ul>
<li>Display groups</li>
<li>Orientation and accelerometer</li>
<li>Touch with buttons</li>
<li>File reading</li>
<li>Example level file for scripting game play</li>
</ul>
<p>The template separates functionality into modules for an object oriented approach. I&#8217;m still learning the techniques for Lua, so if you notice a better way to do it, please don&#8217;t hesitate to let me know in the comments below. Some things in the template are not the best way to accomplish some things, but should give people a good idea on best practices of software design. This will also with work Corona&#8217;s Game Edition.</p>
<p>If you find the template useful, please consider buying a copy of <a href="http://bit.ly/BarNinjaApp" target="_blank">our current application</a> to support us. It&#8217;d mean a lot. Also, check out <a href="http://www.ltzllc.com" target="_blank">our company website</a> if you&#8217;re an iPhone programmer as we&#8217;re looking to bring on some independent programmers.</p>
<p><a href="http://www.mikeziray.com/dev/corona/CoronaGameTemplate.zip?utm_campaign=feed&utm_medium=feed&utm_source=blog" target="_blank">Download the Template Code</a></p>
<p>Again, if this helps you out and you want to support us, check out the links on the side. Thanks!</p>
<p><a href="http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/">Corona Game Template in Lua</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2010/07/09/building-a-game/' rel='bookmark' title='Permanent Link: Building a Game'>Building a Game</a></li>
<li><a href='http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/' rel='bookmark' title='Permanent Link: Why Your Code Sucks &#8211; Naming Conventions'>Why Your Code Sucks &#8211; Naming Conventions</a></li>
<li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
	</item>
		<item>
		<title>BarNinja &#8211; An iPhone App Postmortem</title>
		<link>http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 18:42:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[personal]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[BarNinja]]></category>
		<category><![CDATA[hci]]></category>
		<category><![CDATA[mixed drink]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[ux]]></category>
		<category><![CDATA[WWDC]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=986</guid>
		<description><![CDATA[The BarNinja iPhone app is a mixed drink application with over 730 drink recipes with additional features including a random drink selector, bartending tips, powerful search, and a shots per bottle calculator for party planning. The Beginning As far as we could tell when we started the BarNinja iPhone app, there weren&#8217;t any other apps [...]<p><a href="http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/">BarNinja &#8211; An iPhone App Postmortem</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
<li><a href='http://www.mikeziray.com/2010/06/29/photos-of-the-week-black-and-white/' rel='bookmark' title='Permanent Link: Photos of the Week &#8211; Black and White'>Photos of the Week &#8211; Black and White</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The BarNinja iPhone app is a mixed drink application with over 730 drink recipes with additional features including a random drink selector, bartending tips, powerful search, and a shots per bottle calculator for party planning.</p>
<h2>The Beginning</h2>
<p>As far as we could tell when we started <a href="http://itunes.apple.com/us/app/barninja/id366557970?mt=8" target="_blank">the BarNinja iPhone app</a>, there weren&#8217;t any other apps like it on the Apple App Store. That was back in April of 2009. While I was in San Francisco back in March, I got a Facebook message from Jason asking if I was interested in doing an iPhone app. I had done a bunch of Mac development and even independently sold a previous application. I had also attended Apple&#8217;s developer conference (WWDC) on two separate occasions so I was quite comfortable with developing for Apple products. When I got back from California, we sat down at a local bar in Blacksburg and wrote down our ideas for the app he wanted to make. That was prior to iPhone OS 3.0 being released and we were counting on some key features in that release for our app. In the end, we ultimately failed when the features didn&#8217;t work out exactly as we expected. Luckily, only a little code had been written and that was enough to get me up to speed with the current iPhone development platform.</p>
<p>Instead of quitting, we got <a href="http://www.barninja.com" target="_blank">BarNinja</a>, another local Blacksburg startup involved in another app idea. We wanted to leverage the shake feature (iPhones can detect when you shake them) that was relatively new and make a mixed drink app based around that.</p>
<p>Soon after we started, a few other apps starting hitting the app store with exactly those features. But the competition didn&#8217;t deter us, but instead pushed the bar higher for us. We now knew we couldn&#8217;t just make another mixed drink app with a shaker feature, we had to make <em>the best</em> mixed drink app with a shaker feature. We were also creating a 730+ recipe database (including ingredients and other drink information) that would hopefully give us an added advantage. Luckily, at the last WWDC (2005) that I attended, they announced a technology called CoreData which allows developers to place small databases easily within their applications. They also included it in the iPhone OS and I ran with it.</p>
<h2>Problems and Solutions</h2>
<p>The problem with having 730+ recipes is that it becomes nearly impossible to find a drink in a list. The list view we had, took minutes to scroll through and even tired out your finger having to swipe so much! We added an index on the side to quickly scroll through the list alphabetically. We also added a search to find a drink by name and even added a filter to sort out if the drink was a &#8220;mixed drink&#8221; or a &#8220;shooter.&#8221; But that still wasn&#8217;t good enough.</p>
<p>I could find drinks like &#8220;Vodka Martini,&#8221; but what if I wanted a drink with vodka as an ingredient? So we added in a search that would find ingredients too. That worked out well, but often times vodka isn&#8217;t named vodka, it&#8217;s also names just &#8220;Absolut,&#8221; which everyone knows is vodka, but the iPhone doesn&#8217;t, so we had to teach it what kind of alcohol those products were.</p>
<p>More progress, and we were rolling right along until we tested it out on the actual device; it was painfully slow. The user would hit a key on the virtual iPhone keyboard and the key would get stuck in the &#8220;up&#8221; position until the search was done and only then could you hit the next key. Searching 730+ recipes is easy, but searching on average 6 ingredients for 730+ (~730 x 6 = a lot) takes 4 or 5 seconds on an iPod Touch 2nd generation. So we had to get a little creative.</p>
<p>My solution was to split off the search into a separate thread (a simultaneous process within the application) and perform the search in the background. This introduced a few other problems that I had to fix but the responsiveness improved dramatically. Then, using Apple&#8217;s performance tools, increased the search algorithm by about seven times. Thanks to all those boring Computer Science classes I took at <a href="http://www.vt.edu" target="_blank">Virginia Tech</a> I was able to speed it up and also make it appear to speed up.</p>
<p>Updating the table view, after finding each recipe that matches the criteria, is slow (thanks to the performance tools for reminding me of this). So I returned the first 5 results instantly because those are the only ones you can see initially on the screen and then the rest were updated in a decaying frequency as they were found until the search was done.</p>
<h2>Iteration</h2>
<div id="attachment_988" class="wp-caption aligncenter" style="width: 266px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/2009-09-23_Shaker-03.jpg?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-988" title="Early Search" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/2009-09-23_Shaker-03.jpg" alt="Early Search" width="256" height="488" /></a><p class="wp-caption-text">Early Search</p></div>
<div id="attachment_989" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.15.21.png?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-989" title="Icons, glass name and index" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.15.21.png" alt="Icons, glass name and index" width="320" height="480" /></a><p class="wp-caption-text">Icons, glass name and index</p></div>
<div id="attachment_990" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.26.07.png?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-990" title="Early drink details view" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.26.07.png" alt="Early drink details view" width="320" height="480" /></a><p class="wp-caption-text">Early drink details view</p></div>
<h2>The Final Product</h2>
<div id="attachment_991" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot1.jpg?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-991" title="Initial Screen" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot1.jpg" alt="Initial Screen" width="320" height="460" /></a><p class="wp-caption-text">Initial Screen</p></div>
<div id="attachment_992" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot3.jpg?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-992" title="New search screen" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot3.jpg" alt="New search screen" width="320" height="460" /></a><p class="wp-caption-text">New search screen</p></div>
<div id="attachment_993" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot4.jpg?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-993" title="New drink details view" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot4.jpg" alt="New drink details view" width="320" height="460" /></a><p class="wp-caption-text">New drink details view</p></div>
<h2>Results</h2>
<p>They say if you&#8217;re happy with your 1.0 version of your application, you didn&#8217;t release it early enough. While we definitely have plenty of work yet to do on this application, we didn&#8217;t release it soon enough. It did not take a year of programming to get this done. The programming was the easy part, it was all the other stuff that got in the way, distracted me and things I had to wait on that took the longest. Back in the day, when doing my Computer Science homework, the motto was &#8220;Start early, and start often.&#8221; That rings true for every long term task.</p>
<p>We didn&#8217;t run any real usability tests. This upsets me a bit, but we never really got to show it to too many people. Now LTZ has a few more developers working on more apps, so we have a larger pool of testers, but in the end, we still need to test it out on real human beings and put all those Human Computer Interaction (HCI) skills I spent a lot of time learning, to use.</p>
<p>I did try to apply those skills with the layout of the app. When the app launches, the first screen is the random drink screen, which you can tailor to your tastes in the options. We wanted to give the person using our app something to do right away. We didn&#8217;t want to over whelm them with a list of 730+ ingredients; it doesn&#8217;t impress anyone. In our limited testing everyone figured it out and got a kick out of the shake feature. With other apps, we noticed, they presented a huge list of options that just left you wondering what you should do next. It&#8217;s that classic &#8220;uuuuh&#8221; moment. With ours, right after the launch of the app you can start enjoying the functionality.</p>
<p>The next screen is the raw list of drinks, plain and simple. Our search is straight forward and easy to figure out and the index on the right helps users navigate down the list quickly. We also stuck to using as many user interface elements from the standard set of Apple iPhone as possible. Many other apps try to be creative and diverge from the standard set of UI elements and consequently from the Human Interface Guidelines that Apple has set forth. The result is a poorly designed, clunky looking application that takes time to figure out how to use. Since we&#8217;re using a standard search bar, people using our app know right away what that means and can start right away in using the search feature.</p>
<p>We also chose a dark theme for our colors, not because it looks cool, but because most of our users, we assume, will be using it in a bar, or bar like conditions such as a party, etc. But we also didn&#8217;t want to give it a depressing theme since it was suppose to be fun and helpful, so there are a few bright colors in there. The design will most likely change and be updated, but that&#8217;s the rationale for the initial release. We&#8217;ll see what our users think and how they use the app.</p>
<p>We know our app isn&#8217;t perfect, but we&#8217;re confident we know where to go from here to improve it and fix some of the problems we know exist in it. Through additional usability testing, we&#8217;ll slowly improve upon BarNinja to make it the best drink application on the app store. We know we&#8217;re not there yet with the initial release, but we look forward to adding great functionality that people will love.</p>
<p><a href="http://itunes.apple.com/us/app/barninja/id366557970?mt=8&utm_campaign=feed&utm_medium=feed&utm_source=blog" target="_blank"><img class="aligncenter size-full wp-image-1017" title="Available on App Store" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/05/Available-on-App-Store.png" alt="" width="160" height="61" /></a></p>
<p><a href="http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/">BarNinja &#8211; An iPhone App Postmortem</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
<li><a href='http://www.mikeziray.com/2010/06/29/photos-of-the-week-black-and-white/' rel='bookmark' title='Permanent Link: Photos of the Week &#8211; Black and White'>Photos of the Week &#8211; Black and White</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/2009-09-23_Shaker-03-150x150.jpg" />
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/2009-09-23_Shaker-03.jpg" medium="image">
			<media:title type="html">Early Search</media:title>
			<media:description type="html">Early Search</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/2009-09-23_Shaker-03-150x150.jpg" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.15.21.png" medium="image">
			<media:title type="html">Icons, glass name and index</media:title>
			<media:description type="html">Icons, glass name and index</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.15.21-150x150.png" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.26.07.png" medium="image">
			<media:title type="html">Early drink details view</media:title>
			<media:description type="html">Early drink details view</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot-2009.12.08-21.26.07-150x150.png" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot1.jpg" medium="image">
			<media:title type="html">Initial Screen</media:title>
			<media:description type="html">Initial Screen</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot1-150x150.jpg" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot3.jpg" medium="image">
			<media:title type="html">New search screen</media:title>
			<media:description type="html">New search screen</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot3-150x150.jpg" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot4.jpg" medium="image">
			<media:title type="html">New drink details view</media:title>
			<media:description type="html">New drink details view</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/04/Screenshot4-150x150.jpg" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/05/Available-on-App-Store.png" medium="image">
			<media:title type="html">Available on App Store</media:title>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/05/Available-on-App-Store-150x61.png" />
		</media:content>
	</item>
		<item>
		<title>Handling Your Initial View Controllers for iPhone</title>
		<link>http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 16:50:54 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[advice]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[appdelegate]]></category>
		<category><![CDATA[class diagram]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[software engineering]]></category>
		<category><![CDATA[tabbed bar controller]]></category>
		<category><![CDATA[viewcontrollers]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=884</guid>
		<description><![CDATA[Programming starts with a design A lot of webpages and articles talk about how to program, but they don&#8217;t talk about how to design a program. Good program design separates the weekend programmers from the professionals. Sure, anyone can piece together a working application but a good design allows a power and flexibility of adding [...]<p><a href="http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/">Handling Your Initial View Controllers for iPhone</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
<li><a href='http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/' rel='bookmark' title='Permanent Link: BarNinja &#8211; An iPhone App Postmortem'>BarNinja &#8211; An iPhone App Postmortem</a></li>
<li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/07/09/building-a-game/' rel='bookmark' title='Permanent Link: Building a Game'>Building a Game</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h1>Programming starts with a</h1>
<h1>design</h1>
<p>A lot of webpages and articles talk about how to program, but they don&#8217;t talk about how to design a program. Good program design separates the weekend programmers from the professionals. Sure, anyone can piece together a working application but a good design allows a power and flexibility of adding new features, for example, without having to recode half of your application.</p>
<p>Here, we&#8217;ll talk about handling your initial views when your iPhone application loads. This might be a simple task if your application is simple, but take the case of having a welcome screen followed by a login screen followed by a tabbed view controller. You might set it up so that one activates the other upon being dismissed itself.</p>
<p>Here&#8217;s an example of how the process might look:</p>
<p>Program loads AppDelegate</p>
<p>Load Welcome view from AppDelegate</p>
<p>Dismiss Welcome view -&gt; Tell Welcome view to load Login view</p>
<p>Dismiss Login view -&gt; Tell Login view to load Tabbed view</p>
<p>What if you had to add a view or remove one, say if the login was no longer needed if you gave your users the option to save their credentials. You&#8217;d have to rewrite two different view controllers at least. And this is just a simple example so you can see how complicated this chain of views would get once your application grows. For most programmers this way is fine, and sadly enough most of the time, this is the way it&#8217;s done. But true programmers are lazy. Lazy programmers are good programmers. So be lazy and write good code so that when (not if) you go back and edit your code, it&#8217;ll be a snap.</p>
<h1>A design starts on paper</h1>
<p>Your programs should be conceived on paper. Even with applications that attempt to do this, I still find paper the ultimate programming tool (even though I did the one below on a computer).</p>
<div id="attachment_895" class="wp-caption aligncenter" style="width: 512px"><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/MultiViewExample.png?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="size-full wp-image-895" title="Class Diagram" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/MultiViewExample.png" alt="Class Diagram" width="502" height="426" /></a><p class="wp-caption-text">Class Diagram</p></div>
<p>First off, your controllers should be logically separated based on their tasks that they need to accomplish (not based on how the user will view them). So, you should have a controller or controllers to handle logging in and session creation and have appropriate UIViews and UIViewControllers to get and display info to your user. This may not be a one-to-one relationship though. For example, you may not have a UIViewController that&#8217;s associated with creating your sessions at all.</p>
<p>Next you will have your application&#8217;s business logic view controllers, controllers and all that jazz. The controllers sit independently to the view controllers except, of course, where there&#8217;s coupling or interaction. They are not tied directly, or rely upon any particular view. You illustrate those connections on paper (or in this case in the example diagram above). So if your &#8220;Add New Event&#8221; feature, let&#8217;s say, needs access to the session data, draw a line from your SessionController to your EventsController with an arrow pointing in the direction of the flow (from EventsController to SessionController in our case since the Events controller needs to know about the SessionController but not vise versa).</p>
<h1>Abstraction solves everything</h1>
<p>Since your controller code should be independent of how your views are laid out, you won&#8217;t have your AddNewEventViewController (the view that lets the user add an event) handling the actual creation and saving of events but will instead gathering and packaging the information taken from the user and given to an EventsController to handle the manipulation. Who knows, you may have several views that edit an Event in one way or another and if you change something in how the Event is saved, you&#8217;ll have to go into the code of each one that touches Event and rewrite code &#8212; exactly what we&#8217;re trying to avoid.</p>
<p>Abstracting the data in this way is called indirection and &#8220;<em>All problems in computer science can be solved by another level of indirection</em>;&#8221; &#8212; <a title="David Wheeler (computer scientist)" href="http://en.wikipedia.org/wiki/David_Wheeler_(computer_scientist)">David Wheeler</a>, except as <a title="Kevlin Henney" href="http://en.wikipedia.org/wiki/Kevlin_Henney">Kevlin Henney</a> says, <em>&#8220;&#8230;except for the problem of too many layers of indirection.&#8221;</em></p>
<h1>Singletons, under used</h1>
<h1>and under appreciated</h1>
<p>So, you should have a session controller that sits somewhere ready to be called by who ever needs to validate a session or login (those views that would need access could be your tabbed views, could be your app delegate when your app starts, or could be in your settings views where you type your login info or when you log out). This is usually accomplished with a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">singleton </a>class where any controller can access that one (and only) instance of your controller without having to pass around references to that controller. It not only cleans up your code, but also helps data integrity by not allowing multiple objects to edit the same data.</p>
<h1>Handling your initial views</h1>
<p>You should really have a separate controller handle organizing your views rather than the app delegate since the app delegate should really only be used to initialize your app and handle delegation of your application, not controlling business logic. So the structure should look like:</p>
<p>AppDelegate -&gt; MainViewController -&gt; LoginViewController</p>
<p>I like to have an overall MainViewController that handles all the subviews, be it a tab bar view controller, a table view controller, or whatever your main view is going to be. This level of indirection allows you to easily change what your main view controller is later, should your requirements or ideas change. Having said that, the MainViewController is going to need to get and maintain a reference to the application&#8217;s window.</p>
<p>To actually add the views, stack them on top of each other. Add the Tab Bar Controller&#8217;s view to the Window&#8217;s view first, then if needed, add your login views after that to stack them on top, and then remove them as needed when you&#8217;re done with them. By removing them from the top, it reveals the bottom view.</p>
<p>In MainViewController&#8217;s implementation file, your viewDidLoad: will look like:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>viewDidLoad<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>super viewDidLoad<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>mainWindow addSubview<span class="sy0">:</span> tabBarController.view<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>mainWindow addSubview<span class="sy0">:</span> loginViewController.view<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>mainWindow addSubview<span class="sy0">:</span> welcomeViewController.view<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Where mainWindow is an IBOutlet to your Window in your MainWindow.xib file (or you could get it programatically if you prefer). Also, loginViewController is a pointer to your view controller that you put on top of the tab bar controller. On top of your login view, put a welcome view displaying any text, graphics or video to show after your application loads; note that this is different from your Default.png initial image. Then when you&#8217;re done with your login view, call removeFromSuperview on them to remove them from the view stack. So something like this in your MainViewController (since it handles all manipulation of your main views):</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>removeWelcomeView<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>welcomeViewController.view removeFromSuperview<span class="br0">&#93;</span>;<br />
<span class="co2">// Any other code you need to clean up after your welcome view is removed</span><br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>removeLoginView<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>loginViewController.view removeFromSuperview<span class="br0">&#93;</span>;<br />
<span class="co2">// Any other code you need to clean up after your login view is removed</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Obviously, when you remove the welcome screen view, under it (since you added it on the stack before the welcome view) is your login view. Then, removing the login view will show the tab bar view since that was added first in your view stack:</p>
<p>(Top)</p>
<p>Welcome View</p>
<p>Login View</p>
<p>Tab Bar View</p>
<p>This way we&#8217;re loading all of our views at once and stacking them so the one on top hides the one below. This has a slight overhead since you&#8217;re adding all your views when your applications starts but you can always delay adding the tab bar controller to the Window&#8217;s view until all the initial views are removed, for example. Then, if you&#8217;d like, using the MainViewController, you can add views on top of the tab bar view during execution, for example if you need to login again.</p>
<p>There are a few different ways to handle multiple views and each case is different. This is just one idea, with a few general guidelines. Feel free to ask any questions in the comments if anything wasn&#8217;t clear enough.</p>
<h2>Sample Code</h2>
<p>The following sample code is provided as is for educational and demonstration purposes. I threw it together really quickly and is only used to demonstrate the use of a MainViewController. Normally, you&#8217;ll want to make your MainViewController a singleton class, but in this example I passed the MainViewController instance to the subviews in order for them to call different methods on MainViewController to change the current views. By using a singleton design, you can avoid passing around this reference since there is only ever one MainViewController.</p>
<p>Also, this code demonstrates how to create a tab bar programmatically. It needs to be the root view of your window, but that doesn&#8217;t mean it needs to be the root view controller. We are still allowed to use a MainViewController as our root to handle all subviews and their controllers.</p>
<p><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/2010-02-11_TabBarTest.zip?utm_campaign=feed&utm_medium=feed&utm_source=blog">Download sample code (2010-02-11_TabBarTest.zip)</a></p>
<p><a href="http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/">Handling Your Initial View Controllers for iPhone</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
<li><a href='http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/' rel='bookmark' title='Permanent Link: BarNinja &#8211; An iPhone App Postmortem'>BarNinja &#8211; An iPhone App Postmortem</a></li>
<li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/07/09/building-a-game/' rel='bookmark' title='Permanent Link: Building a Game'>Building a Game</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/MultiViewExample-150x150.png" />
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/MultiViewExample.png" medium="image">
			<media:title type="html">Class Diagram</media:title>
			<media:description type="html">Class Diagram</media:description>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/MultiViewExample-150x150.png" />
		</media:content>
	</item>
		<item>
		<title>How to Program for the iPhone &#8211; A Plan</title>
		<link>http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 22:58:45 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[advice]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[core data]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[ipod]]></category>
		<category><![CDATA[itouch]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tableview]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=813</guid>
		<description><![CDATA[This is not so much how to make applications for the iPhone, since there are so many on of those on the web already. This is more a syllabus on how to get started with the vast amount of information already out there and where to start from to quickly become proficient at iPhone development [...]<p><a href="http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/">How to Program for the iPhone &#8211; A Plan</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/' rel='bookmark' title='Permanent Link: BarNinja &#8211; An iPhone App Postmortem'>BarNinja &#8211; An iPhone App Postmortem</a></li>
<li><a href='http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/' rel='bookmark' title='Permanent Link: Corona Game Template in Lua'>Corona Game Template in Lua</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iphone_views.png?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="aligncenter size-full wp-image-862" title="iphone" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iphone_views.png" alt="" width="360" height="192" /></a></p>
<p>This is not so much how to make applications for the iPhone, since there are so many on of those on the web already. This is more a syllabus on how to get started with the vast amount of information already out there and where to start from to quickly become proficient at iPhone development without getting frustrated or discouraged.</p>
<h2>Development Requirements</h2>
<p>An Intel-based Apple Mac<br />
$99 (optional &#8211; if you want to actually publish your app)<br />
That&#8217;s all!</p>
<h2>Apple Developer Account</h2>
<p>Development for the iPhone is initially free. So feel free to head over to <a href="http://developer.apple.com" target="_blank">http://developer.apple.com</a> and sign up for a developer account if you don&#8217;t already have one. This will give you access to the Integrated Development Environment (IDE) called XCode, which is required to develop applications for the iPhone and iPod Touch. It also serves as a treasure chest of free information, sample code, tutorials, how-to videos and news, all of which I&#8217;ll talk about how to use later.</p>
<p>Once you&#8217;ve signed up for a developer account, you&#8217;ll need to download XCode and install it on your Mac. PC users are, as they always are, out of luck since a Mac is required for development. Once that&#8217;s done, put it aside as you won&#8217;t need it for a bit.</p>
<h2>Learn to Program</h2>
<h3>An Important Design Pattern: Model View Controller</h3>
<p>If you already know how to program a little bit, but haven&#8217;t gone through the rigors of a four year university Computer Science degree or something similar, start with learning an important design patterns, namely one called <a href="http://developer.apple.com/iphone/library/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html" target="_blank">Model View Controller</a> (MVC). Of course, a 4-year degree is not necessary, but you need to understand basic software engineering principles, design patterns and basic usability practices, which practically none of the programming tutorials, books and online articles teach. Also, a lot of practice helps as you&#8217;ll learn how to fix non-obvious compiler and runtime errors, how to structure your code, and pitfalls of each language you use.</p>
<h3>Objective-C: The Language of Choice</h3>
<p>After you understand the basic principle of MVC, you can then move on to learning the language of choice, Objective-C. Why Objective-C? Why didn&#8217;t Apple just choose a common language like C++ or Java? The reasons will become obvious as you learn about the language, it&#8217;s power, and how it fits into the MVC methodology better than any other language you&#8217;ve likely seen before. So here&#8217;s a nice tutorial on <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html" target="_blank">how to program in Objective-C</a>.</p>
<p>If you already have a great understanding of programming and know a C-based langauge, skip the lengthy tutorial and <a href="http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/" target="_blank">take a look at this primer</a> to get a sense of the additions to C that Objective-C brings to the table. For most people who are familiar with a C-based language, this will be all you need to get started.</p>
<p><span id="more-813"></span></p>
<h3>The Cocoa Framework: Because Reinventing the Wheel is for Marketing Types</h3>
<p>Ok, great. Now you speak the language, but you need to know the framework and how the language leverages this technology so you don&#8217;t have to reinvent the wheel. Take a look at <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html" target="_blank">Cocoa Fundamentals</a> to learn how the fundamentals of Cocoa work. The Cocoa framework is a superset of the Cocoa Touch framework that is specialized to fit the paradigm and requirements of the iPhone and iPod Touch devices. Knowledge of the Cocoa framework is essential to understanding how to use such things as data structures and containers, user interface (UI) elements and the rich built-in functionality that all these components offer you for free. Many of the Cocoa framework elements carry over into Cocoa Touch, such as strings, CoreData, CoreAudio, CoreAnimation and concepts such as delegation, protocols, interfaces and application bundles.</p>
<h3>XCode Tools: The Real Workers</h3>
<p>Once you know how to speak the language and use the technology, you need to know how to use the tools to make it all happen. Here&#8217;s another Apple document that explains <a href="http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/A_Tour_of_Xcode/000-Introduction/qt_intro.html" target="_blank">what XCode is and how to use it</a>. Even if you&#8217;ve worked in XCode before, it&#8217;s worth checking out as XCode is a powerful code editor, debugger and provides a host of other tools and features. If you&#8217;re interested in what else XCode has to offer, there&#8217;s a more detailed explanation of <a href="http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/000-Introduction/Introduction.html" target="_blank">the other tools that come with XCode</a>.</p>
<h3>Your First iPhone Application</h3>
<p>You need to keep in mind that even though this is a lot of information to process, if you&#8217;re unfamiliar with the platform, it&#8217;s all very intuitive and will become second nature with a little practice and time. Just keep looking at a little bit every day and re-read some of the things you read early on. No one can learn a foreign language in a week, so don&#8217;t expect to be fluent in everything Objective-C and Cocoa in less than that. Spend a little time with it every day, go through sample code that interests you, read developer forums to see what other people are having trouble with and come up with a few of your own app ideas just to put all this theory to work. You never truly learn something until you have to use it in the real world.</p>
<p>So now that you have made all the theory, concepts and language familiar to you, you can cement it in your brain with <a href="http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone101/Articles/01_Overview.html" target="_blank">your first iPhone application</a>. This tutorial will cover most of the concepts you already learned (repetition is the key to really cementing that knowledge) but it will also show how they all tie together, which is the most important part. Don&#8217;t just copy and paste the code. You don&#8217;t have to memorize it, but typing it out really helps you recognize the code later when you have to use it on your own.</p>
<h2>Beyond the Basics</h2>
<p>This is where most tutorials stop. They pat you on the back, call you an iPhone developer and send you on your way to flail your way through haphazard development. All you really know however is how to put a few buttons on the screen and make them do something stupid (which admittedly is most of the apps that are available on the app store). There&#8217;s a lot more you need to know to be truly dangerous.</p>
<h3>Usabilty on the iPhone: The Apple Human Interface Guidelines (HIG)</h3>
<p>Take an afternoon and sit down and read <a href="http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/Introduction/Introduction.html" target="_blank">the Apple Human Interface Guidelines</a>. This will give you a slew of iPhone capabilities, but more importantly, it&#8217;ll give you a sense of how to properly use the interface elements of the iPhone. Nothing shows an amateur or know-nothing developer than an improper use of an interface element or using a certain interface element because it looks or acts cool (anyone remember the drawer?).</p>
<h2>Developing iPhone Apps:</h2>
<h2>Gather those Building Blocks</h2>
<h3>Create an iPhone Utility Application</h3>
<p>iPhone utility applications are the easiest to understand. They have two views, a front side and a back side. This type of app is great for learning how two views can communicate without worrying about complex data structures, implementing confusing protocols or dealing with the frustration when your code compiles, but doesn&#8217;t work. You can also experiment with new technologies such as CoreData, CoreAnimation, CoreAudio, WebViews, playing music or anything else you&#8217;re interested in without the overhead of managing views and other complexities like that. Keep it simple and think of a few ideas that fit the paradigm of an iPhone utility application.</p>
<h3>Navigation Controller: Controlling Presentation</h3>
<p>After you&#8217;ve successfully completed a working iPhone utility application, the next and most common interface technology is <a href="http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/CombiningViewControllers/CombiningViewControllers.html" target="_blank">the Navigation Controller</a>. This is the controller that allows you to hit a button and have a new view slide in from the right. I love this controller because it adds a lot of functionality to your app without a lot of work. Simply allocate a new view, push it onto the navigation controller and release it because the navigation controller does the rest.</p>
<h3>Tab Bar Controller: Multiple Sections in One App</h3>
<p><a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITabBarController_Class/Reference/Reference.html" target="_blank">The Tab Bar Controller</a> is a great way to segment your applications into features. It&#8217;s a bit trickier to set up, but once you know how it works it&#8217;s intuitive and flexible. For example, you can use the views that Interface Builder gives you to drop your UI elements onto, or you can tell Interface Builder to load a particular View Controller of your choice. It greatly extends your application&#8217;s functionality with minimal work.</p>
<h3>Tableview Controller Class: Displaying Your Data</h3>
<p><a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewController_Class/Reference/Reference.html" target="_blank">Table views</a> are a bit more complex and involve a bit more understanding but it&#8217;s hard to get far in iPhone development without using a tableview. <a href="http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007451-CH1-SW1" target="_blank">Invest the time to understand these creatures</a> and it&#8217;ll give you a nice return on your investment. Take a look at the <a href="http://developer.apple.com/iphone/library/samplecode/TableViewSuite/index.html" target="_blank">TableViewSuite sample code</a> to learn how to do everything you ever wanted to with table views.</p>
<h3>Tool Bar: Making Your App Accomplish Something Useful</h3>
<p>Applications need to do something useful besides editing data. Sometimes you may want to do something a little bit different and adding <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIToolbar_Class/Reference/Reference.html" target="_blank">a tool bar</a> to your application is a great way to add a different kind of functionality. It&#8217;s simple to use and gives you a standard way to manipulate data in your application.</p>
<h3>Core Data: Handling Lots of Data Without Too Much Real Work</h3>
<p>A lot of your app ideas are going to be based on having lots of useful data that needs to be displayed and manipulated and <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/CoreData_ObjC/" target="_blank">Core Data</a> is the easiest, most flexible and optimized way to do that. So spend some time and read <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html" target="_blank">the Core Data Programming Guide</a>. Secondly, it&#8217;s important to learn how to use a <a href="http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/Reference/Reference.html" target="_blank">fetch results controller</a> as it will allow you to display your data in a tableview in a simpler way than straight out of the fetch request. It will also give you the functionality of indexed tableviews for free (as seen in the Contacts application).</p>
<p><a href="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iPhone_font_back.png?utm_campaign=feed&utm_medium=feed&utm_source=blog"><img class="aligncenter size-full wp-image-865" title="iPhone font and back" src="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iPhone_font_back.png" alt="" width="309" height="360" /></a></p>
<h2>More Programming for iPhone</h2>
<p>This is the one of the best paths to picking up programming for the iPhone. It starts you off with the most useful items without overwhelming you. However, it does take a lot of time to read and a little more than that to understand, but take it slowly, look at it over a course of several weeks and soon you&#8217;ll be able to add anything to your application by only reading the API references.</p>
<p>There are a ton of things that you&#8217;ll need to learn in order to be an effective developer, so in future articles I&#8217;ll take what I&#8217;ve learned, some pitfalls and neat tricks and show you some of the basic stuff that you can&#8217;t find in the Apple developer documentation such as handling common problems, design patterns and usability pitfalls.</p>
<p>After all of this, you&#8217;re still not done. You&#8217;ve only grasped the basics, but at this point, the real challenge is not understanding the technologies, it&#8217;s figuring out where to go from here: what should you learn next, how can you get started programming a real app, and most importantly how you can make a lot of money. All of this will be covered in future articles as this one is too long already, but I will leave you with some of my favorite links. In the mean time, subscribe via RSS or email to get future articles on iPhone and everything else I do. Finally, frequent the Apple developer forums and try to answer some of the questions there. You won&#8217;t always know the answer, but a little research and you will be able to figure it out and learn in the process.</p>
<h2>Additional Links</h2>
<p><a href="http://stackoverflow.com/questions/tagged/iphone" target="_blank">Stack Overflow</a> &#8211; A great site for asking programming questions of all kinds.</p>
<p><a href="http://www.iphonedevsdk.com/" target="_blank">iPhone Dev SDK</a> &#8211; Forum-based site for asking and answering everything iPhone related.</p>
<p><a href="http://www.iphonesdkarticles.com/" target="_blank">iPhone SDK Articles</a> &#8211; Blog-based site with lots of useful and informative examples on iPhone development.</p>
<h2>My Apple Background</h2>
<p>I&#8217;ve been developing Mac applications since 2003, attending WWDC 2004 and 2005. I&#8217;m the author of several small Mac applications including a Terminal GUI tool that&#8217;s been quite popular in the past. I have taught Code Kitchens at Virginia Tech on Cocoa through the Mac Users Group and I use to sell, support, demonstrate and evangelize Apple products to college kids as an Apple Campus Rep. I&#8217;m now an independent developer, working in partnership with LTZ, a company started to give independent developers and free thinkers a platform and voice from which to launch their apps or app ideas.</p>
<p><a href="http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/">How to Program for the iPhone &#8211; A Plan</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2010/04/10/iphone-pricing/' rel='bookmark' title='Permanent Link: iPhone Pricing'>iPhone Pricing</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2010/04/14/barninja-an-iphone-app-postmortem/' rel='bookmark' title='Permanent Link: BarNinja &#8211; An iPhone App Postmortem'>BarNinja &#8211; An iPhone App Postmortem</a></li>
<li><a href='http://www.mikeziray.com/2010/06/27/corona-game-template-in-lua/' rel='bookmark' title='Permanent Link: Corona Game Template in Lua'>Corona Game Template in Lua</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iphone_views-150x150.png" />
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iphone_views.png" medium="image">
			<media:title type="html">iphone</media:title>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iphone_views-150x150.png" />
		</media:content>
		<media:content url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iPhone_font_back.png" medium="image">
			<media:title type="html">iPhone font and back</media:title>
			<media:thumbnail url="http://www.mikeziray.com/blog/wp-content/uploads/2010/01/iPhone_font_back-150x150.png" />
		</media:content>
	</item>
		<item>
		<title>Dynamic Textfields in Flash AS3 and Their Bugs</title>
		<link>http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 13:47:42 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[advice]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[Action Script]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[foreign]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[localize]]></category>
		<category><![CDATA[macromedia]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[text field]]></category>
		<category><![CDATA[translate]]></category>
		<category><![CDATA[translation]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=716</guid>
		<description><![CDATA[I do a ton of localizations for web-based media (over 30 countries) and have run across several bugs in Flash where the characters don&#8217;t show up for certain fonts, especially for translations with non-latin characters (such as Russian, Chinese, Japanese and even Hungarian and German). I always complain about them, and instead of just complaining, [...]<p><a href="http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/">Dynamic Textfields in Flash AS3 and Their Bugs</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2008/03/10/tech-update/' rel='bookmark' title='Permanent Link: Tech Update'>Tech Update</a></li>
<li><a href='http://www.mikeziray.com/2009/01/29/fixing-whats-not-broken-a-user-centric-perspective/' rel='bookmark' title='Permanent Link: Fixing What&#8217;s Not Broken: A User-Centric Perspective'>Fixing What&#8217;s Not Broken: A User-Centric Perspective</a></li>
<li><a href='http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/' rel='bookmark' title='Permanent Link: Custom Mouse Cursor for ActionScript 3'>Custom Mouse Cursor for ActionScript 3</a></li>
<li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I do a ton of localizations for web-based media (over 30 countries) and have run across several bugs in Flash where the characters don&#8217;t show up for certain fonts, especially for translations with non-latin characters (such as Russian, Chinese, Japanese and even Hungarian and German). I always complain about them, and instead of just complaining, I figured I&#8217;d share some of the solutions since they&#8217;re bugs in Flash and may not have obvious solutions.</p>
<h2>Obvious Solutions</h2>
<p>The obvious solution when you have a dynamic text field, that is either driven from XML, RSS, or other external document or data source, is to embed the font&#8217;s character sets. Obviously if you are going to be using Korean in your Flash application, you want to embed the Korean character sets so the Korean characters show up in the font you have chosen. However, with so much emphasis on branding these days, not all specialized fonts have characters in Korean.</p>
<p>For example, the Star Wars Jedi font that I once worked with has absolutely no other characters beyond A-Z, a-z and numerals. Forget Spanish and it&#8217;s tildes, forget German and its umlauts, but Russian?! Ha! Everyone knows there were no Russian Jedis! So, what to do?<br />
<span id="more-716"></span></p>
<h2>Dynamic Font Swap!</h2>
<p>There are several nuances to Flash character embedding (ok, there&#8217;s probably a few hundred as I find at least one new one a week). Unfortunately Star Wars applications with the Jedi font won&#8217;t work in any language but High Schooler English since they don&#8217;t use punctuation either. So we have to change the font to something more appropriate if we want to use another language. Here&#8217;s the ActionScript 3 (AS3) code, which I&#8217;ll explain a bit after:</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Get previous text format from text field to save other settings</span><br />
<span class="kw2">var</span> newTextFormat:<span class="kw3">TextFormat</span> = theTextField_txt.<span class="kw3">getTextFormat</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Change font size if applicable</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span> fontSize_num <span class="sy0">!</span>= 0 <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
newTextFormat.<span class="kw3">size</span> = fontSize_num;<br />
<span class="br0">&#125;</span><br />
<span class="co1">// If we need to replace the font, do that here</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span> isSpecialCharacterSet_bool <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
newTextFormat.<span class="kw3">font</span> = defaultFont_str; <span class="co1">// set font</span><br />
newTextFormat.<span class="kw3">bold</span> = <span class="kw2">false</span>;<br />
newTextFormat.<span class="me1">kerning</span> = <span class="kw2">false</span>;</p>
<p>theTextField_txt.<span class="kw3">embedFonts</span> = <span class="kw2">false</span>; &nbsp;<span class="co1">// kill the embedded font</span><br />
theTextField_txt.<span class="me1">rotation</span> = <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">theTextField_txt.<span class="kw3">setTextFormat</span><span class="br0">&#40;</span> newTextFormat <span class="br0">&#41;</span>; <span class="co1">// Apply altered text format</span></div>
</div>
<p>Ok, so what does that all mean? We take the textfield&#8217;s font properties (say, Jedi Font, size 14pt, embedded characters, etc) and alter some of the properties we need to so that our application can speak to our Russian Jedi friends. This particular code will accept a fontsize_num variable in case you want to alter the font size of the text so that it will fit within the given textbox because we all know Russian Jedi Masters are sometimes really wordy!</p>
<p>Next, we have a variable isSpecialCharacterSet_bool, that if true, will execute the font swap that we need. You can set this variable via XML to control, on a per language basis (having a separate XML file per language), if you want to swap fonts. After that is the defaultFont_str variable, which we can also set via XML to determine which font we want to use as a replacement of our Star Wars Jedi font. Recommended fonts are _sans, _serif, Arial and Helvetica as those are fonts that include almost every character known to modern languages.</p>
<p>Then, we turn off bold because, believe it or not, a bolded font is an entirely different character set and causes all sorts of other problems. We also turn off kerning because it&#8217;s a ridiculous feature that doesn&#8217;t work and I guarantee will mess up your dynamic text fields, foreign or otherwise.</p>
<p>The next two lines are super important:</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">theTextField_txt.<span class="kw3">embedFonts</span> = <span class="kw2">false</span>; &nbsp;<span class="co1">// kill the embedded font</span><br />
theTextField_txt.<span class="me1">rotation</span> = <span class="nu0">0</span>;</div>
</div>
<p>Unless you embed ALL the font characters that Flash will let you, you won&#8217;t have your foreign language characters, which I don&#8217;t recommend anyway due to a huge increase in file size and also for the fact that it still won&#8217;t work (I&#8217;ve tried, trust me on this one). So, even though everyone has _sans, _serif, Arial and Helvetica on their computer (which allows us to display the characters without embedding them), if you DO embed some character sets, it won&#8217;t show up. Go ahead, create a dynamic text field in Arial, embed A-Z, a-z, numerals and punctuation and watch it NOT show up when you assign a string in Russian to it. Go back, click &#8220;Don&#8217;t Embed&#8221; on the textfield and watch as your Russian characters show up again. So, we programmatically disallow fonts from being embedded.</p>
<p>Another thing you&#8217;d think Adobe could have gotten right but doesn&#8217;t seem to care about the non-English speaking community, a.k.a. the 6 billion other people on this planet, is the bug that if a foreign character exists in a textfield that is even slightly rotated, the entire textfield disappears. So, thank you Adobe for wasting my time with your poorly programmed textfields. The trick though, is to simply make the textfield&#8217;s rotation 0. Strangest thing ever, but give it a try if you don&#8217;t believe me. And if the textfield&#8217;s parent movieclip is rotated, not your child textfield within said MC, try:</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">theTextField_txt.<span class="me1">parent</span>.<span class="me1">rotation</span> = <span class="nu0">0</span>;</div>
</div>
<p>You can also programatically set the multiline property if translated text seems to be a bit wordy. But you should probably have set up your textfields to handle the expanded text anyway.</p>
<p>Put that code into a function and throw the textfield at it and additionally the font size (which I usually get from the XML node&#8217;s property). Unfortunately, you have to do this for every textfield and every time you set the text, but if you plan and build your Flash applications correctly, this should be a simple addition to your application framework. If it&#8217;s not obvious, let me know and I&#8217;ll write another article on best practices for localizing your Flash applications.</p>
<h2>Flash Textfields Are Still Broken!</h2>
<p>What isn&#8217;t broken in Flash? If you&#8217;ve delved deeper into Flash than simple animations, you&#8217;ve more than likely run into several on your own. Most of the time, restarting Flash will work, but other times, some detective work must be done in order to sort out the workarounds.</p>
<h4>Masked fonts</h4>
<p>If your dynamic textfield is masked, in anyway, fonts won&#8217;t show up if you disallow font embedding. Why? Well isn&#8217;t it obvious? Flash is broken. So, don&#8217;t mask your fonts if you want them to work internationally.</p>
<p>So many thanks to Adobe, we enjoy overpaying for your products so that they&#8217;re full of bugs. I&#8217;ve worked in Flash since the late 90s and I can&#8217;t remember them EVER releasing a patch for it. Just a new version that you have to pay several hundred dollars for. So my advice is to use an open standard for your applications and websites since those always support international languages.</p>
<h2>Did you find this useful?</h2>
<p>If you found this page useful, please consider helping us out by checking out the links on the right. Thanks!</p>
<p><a href="http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/">Dynamic Textfields in Flash AS3 and Their Bugs</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2008/03/10/tech-update/' rel='bookmark' title='Permanent Link: Tech Update'>Tech Update</a></li>
<li><a href='http://www.mikeziray.com/2009/01/29/fixing-whats-not-broken-a-user-centric-perspective/' rel='bookmark' title='Permanent Link: Fixing What&#8217;s Not Broken: A User-Centric Perspective'>Fixing What&#8217;s Not Broken: A User-Centric Perspective</a></li>
<li><a href='http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/' rel='bookmark' title='Permanent Link: Custom Mouse Cursor for ActionScript 3'>Custom Mouse Cursor for ActionScript 3</a></li>
<li><a href='http://www.mikeziray.com/2010/01/16/how-to-program-for-the-iphone-a-plan/' rel='bookmark' title='Permanent Link: How to Program for the iPhone &#8211; A Plan'>How to Program for the iPhone &#8211; A Plan</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
	</item>
		<item>
		<title>Custom Mouse Cursor for ActionScript 3</title>
		<link>http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/?utm_campaign=feed&#038;utm_medium=feed&#038;utm_source=blog&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/#comments</comments>
		<pubDate>Sat, 30 May 2009 20:54:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[advice]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[comment]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[listener]]></category>
		<category><![CDATA[macromedia]]></category>
		<category><![CDATA[mouse]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mikeziray.com/?p=623</guid>
		<description><![CDATA[I threw together an FLA for someone to show them the different ways to handle custom mouse cursors in ActionScript 3 (AS3). I figured I should start posting more of my code for people since I use other people&#8217;s blogs all the time to figure out my own problems. I&#8217;m mostly coding up stuff for [...]<p><a href="http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/">Custom Mouse Cursor for ActionScript 3</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>



Related posts:<ol><li><a href='http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/' rel='bookmark' title='Permanent Link: Dynamic Textfields in Flash AS3 and Their Bugs'>Dynamic Textfields in Flash AS3 and Their Bugs</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2009/01/29/fixing-whats-not-broken-a-user-centric-perspective/' rel='bookmark' title='Permanent Link: Fixing What&#8217;s Not Broken: A User-Centric Perspective'>Fixing What&#8217;s Not Broken: A User-Centric Perspective</a></li>
<li><a href='http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/' rel='bookmark' title='Permanent Link: Why Your Code Sucks &#8211; Naming Conventions'>Why Your Code Sucks &#8211; Naming Conventions</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I threw together an FLA for someone to show them the different ways to handle custom mouse cursors in ActionScript 3 (AS3). I figured I should start posting more of my code for people since I use other people&#8217;s blogs all the time to figure out my own problems.</p>
<p>I&#8217;m mostly coding up stuff for Flash, Flex, Augmented Reality, iPhone, PHP, and CSS/HTML right now so hopefully I&#8217;ll have the time to post more snippets of things I&#8217;ve figured out trying to get all this technology to work.</p>
<p>
<object width="550" height="400">
<param name="movie" value="http://www.mikeziray.com/blog/wp-content/uploads/2009/05/customcursor_cs3.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.mikeziray.com/blog/wp-content/uploads/2009/05/customcursor_cs3.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
</p>
<h2>Direct Access to the X and Y Mouse Coordinates</h2>
<p>The first method allows us a finer control over the movement of the cursor by directly changing the X and Y coordinates of the cursor movieclip with the X and Y coordinates of the mouse. If we wanted to restrict the movement to say, a specific Y coordinate (as we&#8217;ll see in the next example), we can do that just as easily by keeping the target_mc.y a constant when we update it&#8217;s X coordinate.</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Set up our listeners for each of our mouse actions</span><br />
boundary_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_OVER</span>,<br />
onMouseOver <span class="br0">&#41;</span>;<br />
boundary_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_OUT</span>,<br />
onMouseOut &nbsp;<span class="br0">&#41;</span>;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Ignores all sub movie clips and treats them as the parent</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &#8212; otherwise events will be triggered on the child</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; movieclips and not our main clip</span><br />
target_mc.<span class="me1">mouseChildren</span> &nbsp; = <span class="kw2">false</span>;<br />
boundary_mc.<span class="me1">mouseChildren</span> = <span class="kw2">false</span>;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">&nbsp;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Ignore all the mouse events for the cursor</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &#8212; otherwise MOUSE_OUT will trigger since mouse is</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; now on target_mc instead of boundary_mc</span><br />
target_mc.<span class="me1">mouseEnabled</span> = <span class="kw2">false</span>;</p>
<p><span class="co1">///////////////////////////////////////////////////////////////</span><br />
<span class="co1">// ON MOUSE OVER /////////////////////////////////////////////</span><br />
<span class="co1">/////////////////////////////////////////////////////////////</span><br />
<span class="co1">// When the mouse rolls over onto our red boundary, hide</span><br />
<span class="co1">// &nbsp; mouse/show cursor</span><br />
<span class="co1">//</span><br />
<span class="kw2">function</span> onMouseOver<span class="br0">&#40;</span> theEvent:MouseEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
<span class="co1">// Swap the mouse for the cursor</span><br />
<span class="kw3">Mouse</span>.<span class="kw3">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// hide mouse cursor</span><br />
target_mc.<span class="kw3">visible</span> = <span class="kw2">true</span>;</p>
<p><span class="co1">// Add a listener to handle the moving of the mouse</span><br />
<span class="co1">// We add the listener here because otherwise the onMouseMove</span><br />
<span class="co1">// &nbsp; &nbsp;would execute whenever the mouse moved, whether it was</span><br />
<span class="co1">// &nbsp; &nbsp;on our boundary or not. Not a big deal, but better for</span><br />
<span class="co1">// &nbsp; &nbsp;efficiency</span><br />
boundary_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_MOVE</span>,<br />
onMouseMotion <span class="br0">&#41;</span>;</p>
<p><span class="co1">// Handle the movement now so we don&#8217;t have to wait for a</span><br />
<span class="co1">// &nbsp; mouse movement to reposition the cursor</span><br />
onMouseMotion<span class="br0">&#40;</span><span class="kw2">null</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">///////////////////////////////////////////////////////////////</span><br />
<span class="co1">// ON MOUSE MOTION ///////////////////////////////////////////</span><br />
<span class="co1">/////////////////////////////////////////////////////////////</span><br />
<span class="co1">// When the mouse moves, we also want to move our target to follow</span><br />
<span class="co1">// &nbsp; the mouse</span><br />
<span class="co1">//</span><br />
<span class="kw2">function</span> onMouseMotion<span class="br0">&#40;</span> theEvent:MouseEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
<span class="co1">// Reposition our custom cursor</span><br />
target_mc.<span class="me1">y</span> = <span class="kw3">stage</span>.<span class="me1">mouseY</span>;<br />
target_mc.<span class="me1">x</span> = <span class="kw3">stage</span>.<span class="me1">mouseX</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">///////////////////////////////////////////////////////////////</span><br />
<span class="co1">// ON MOUSE OUT //////////////////////////////////////////////</span><br />
<span class="co1">/////////////////////////////////////////////////////////////</span><br />
<span class="co1">// When mouse goes outside our red boundary, show default mouse,</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; and hide our custom one</span><br />
<span class="co1">//</span><br />
<span class="kw2">function</span> onMouseOut<span class="br0">&#40;</span> theEvent:MouseEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
<span class="co1">// Swap the cursor for the mouse</span><br />
<span class="kw3">Mouse</span>.<span class="kw3">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
target_mc.<span class="kw3">visible</span> = <span class="kw2">false</span>;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Stop listening for mouse movement</span><br />
boundary_mc.<span class="me1">removeEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_MOVE</span>,<br />
onMouseMotion <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<h2>The Drag and Drop Method</h2>
<p>Our next method uses the drag function built into Flash. With this function you can drag a movieclip around the stage with the mouse automatically. This restricts some of our freedom, but gives us some very easy coding to control the mouse cursor to specific regions. In this case we simply restrict the motion of the cursor along a rectangle of 0 height (so it won&#8217;t move along the Y axis) and with a width as wide as our boundary box. This feature has its advantages, and I merely include it to demonstrate how to use it in the context of a custom mouse cursor.</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Set up our listeners for each of our mouse actions</span><br />
boundary2_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_OVER</span>,<br />
onMouseOver2 <span class="br0">&#41;</span>;<br />
boundary2_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_OUT</span>,<br />
onMouseOut2 &nbsp;<span class="br0">&#41;</span>;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Ingores all sub movie clips and treats them as the parent</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &#8212; otherwise events will be triggered on the child</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; movieclips and not our main clip</span><br />
target_mc.<span class="me1">mouseChildren</span> &nbsp; &nbsp;= <span class="kw2">false</span>;<br />
boundary2_mc.<span class="me1">mouseChildren</span> = <span class="kw2">false</span>;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">&nbsp;</div>
</div>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="co1">// Ignore all the mouse events for the cursor</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &#8212; otherwise MOUSE_OUT will trigger since mouse is</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; now on target_mc instead of boundary_mc</span><br />
target_mc.<span class="me1">mouseEnabled</span> = <span class="kw2">false</span>;</p>
<p><span class="co1">// Calculate the the vertical center of our boundary</span><br />
<span class="co1">// &nbsp; so that the cursor can move along that line</span><br />
<span class="kw2">var</span> verticalCenter_num = boundary2_mc.<span class="me1">y</span> + <span class="br0">&#40;</span>boundary2_mc.<span class="kw3">height</span> <span class="sy0">/</span> 2<span class="br0">&#41;</span>;</p>
<p><span class="co1">///////////////////////////////////////////////////////////////</span><br />
<span class="co1">// ON MOUSE OVER 2 ///////////////////////////////////////////</span><br />
<span class="co1">/////////////////////////////////////////////////////////////</span><br />
<span class="co1">// Same as our previous one, but set for another boundary</span><br />
<span class="co1">//</span><br />
<span class="kw2">function</span> onMouseOver2<span class="br0">&#40;</span> theEvent:MouseEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
<span class="co1">// Swap the mouse for the cursor</span><br />
<span class="kw3">Mouse</span>.<span class="kw3">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
target_mc.<span class="kw3">visible</span> = <span class="kw2">true</span>;</p>
<p><span class="co1">// Add a listener to handle the moving of the mouse</span><br />
<span class="co1">// We add the listener here because otherwise the</span><br />
<span class="co1">// &nbsp; &nbsp;onMouseMove would execute whenever the</span><br />
<span class="co1">// &nbsp; &nbsp;mouse moved, whether it was on our boundary</span><br />
<span class="co1">// &nbsp; &nbsp;or not. Not a big deal, but better for efficiency</span><br />
boundary2_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_MOVE</span>,<br />
onMouseMotion2 <span class="br0">&#41;</span>;</p>
<p><span class="co1">// Handle the movement now so we don&#8217;t have to wait</span><br />
<span class="co1">// for a mouse movement to reposition the cursor</span><br />
target_mc.<span class="me1">y</span> = verticalCenter_num;<br />
target_mc.<span class="me1">x</span> = <span class="kw3">stage</span>.<span class="me1">mouseX</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">///////////////////////////////////////////////////////////////</span><br />
<span class="co1">// ON MOUSE MOTION 2 /////////////////////////////////////////</span><br />
<span class="co1">/////////////////////////////////////////////////////////////</span><br />
<span class="co1">// Restricts the movement of our custom cursor along the X axis</span><br />
<span class="co1">//</span><br />
<span class="kw2">function</span> onMouseMotion2<span class="br0">&#40;</span> theEvent:MouseEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
<span class="co1">// Calculate the the vertical center of our boundary</span><br />
<span class="co1">// &nbsp; so that the cursor can move along that line</span><br />
<span class="kw2">var</span> verticalCenter_num = boundary2_mc.<span class="me1">y</span> +<br />
<span class="br0">&#40;</span>boundary2_mc.<span class="kw3">height</span> <span class="sy0">/</span> 2<span class="br0">&#41;</span>;</p>
<p><span class="co1">// We use startDrag() here since it&#8217;s easier to restrict</span><br />
<span class="co1">// &nbsp; the cursor&#8217;s boundaryusing a Rectangle object.</span><br />
<span class="co1">// boundary2_mc.x: Start from the x position of our</span><br />
<span class="co1">// &nbsp; boundary verticalCenter_num: Start from the</span><br />
<span class="co1">// &nbsp; vertical center as our y coordinate</span><br />
<span class="co1">// boundary2_mc.width: Allow the cursor to move</span><br />
<span class="co1">// &nbsp; the length of the boundary&#8217;s width (starting from x)</span><br />
<span class="co1">// 0: Don&#8217;t allow the cursor to move up or down</span><br />
target_mc.<span class="kw3">startDrag</span><span class="br0">&#40;</span> <span class="kw2">true</span>, <span class="kw2">new</span> Rectangle<span class="br0">&#40;</span><br />
boundary2_mc.<span class="me1">x</span>,<br />
verticalCenter_num,<br />
boundary2_mc.<span class="kw3">width</span>,<br />
0<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">///////////////////////////////////////////////////////////////</span><br />
<span class="co1">// ON MOUSE OUT 2 ////////////////////////////////////////////</span><br />
<span class="co1">/////////////////////////////////////////////////////////////</span><br />
<span class="co1">// When mouse goes outside our gray boundary, show default mouse,</span><br />
<span class="co1">// &nbsp; hide custom mouse and stop drag</span><br />
<span class="co1">//</span><br />
<span class="kw2">function</span> onMouseOut2<span class="br0">&#40;</span> theEvent:MouseEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
<span class="co1">// Swap the cursor for the mouse</span><br />
<span class="kw3">Mouse</span>.<span class="kw3">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
target_mc.<span class="kw3">visible</span> = <span class="kw2">false</span>;</p>
<p><span class="co1">// Stop our drag</span><br />
target_mc.<span class="kw3">stopDrag</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// Stop listening for mouse movement</span><br />
boundary2_mc.<span class="me1">removeEventListener</span><span class="br0">&#40;</span> MouseEvent.<span class="me1">MOUSE_MOVE</span>,<br />
onMouseMotion2 <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>I hope the code is commented well enough for you to understand what&#8217;s going on. It should be fairly self explanatory, but if not, feel free to ask.</p>
<h2>Sample Code</h2>
<p>Below is the source code for the SWF used at the top of the post:</p>
<p><a href="http://www.mikeziray.com/blog/wp-content/uploads/2009/05/2009-05-28_Custom_Cursors.zip?utm_campaign=feed&utm_medium=feed&utm_source=blog">Download the Custom Cursors sample code.</a></p>
<h2>Did you find this useful?</h2>
<p>If you found this page useful, please consider helping us out by checking out the links on the right. Thanks!</p>
<p><a href="http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/">Custom Mouse Cursor for ActionScript 3</a> is a post from: <a href="http://www.mikeziray.com">Ziray Studio Labs</a></p>


<p>Related posts:<ol><li><a href='http://www.mikeziray.com/2009/10/19/dynamic-textfields-in-flash-as3-and-their-bugs/' rel='bookmark' title='Permanent Link: Dynamic Textfields in Flash AS3 and Their Bugs'>Dynamic Textfields in Flash AS3 and Their Bugs</a></li>
<li><a href='http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/' rel='bookmark' title='Permanent Link: Handling Your Initial View Controllers for iPhone'>Handling Your Initial View Controllers for iPhone</a></li>
<li><a href='http://www.mikeziray.com/2009/01/29/fixing-whats-not-broken-a-user-centric-perspective/' rel='bookmark' title='Permanent Link: Fixing What&#8217;s Not Broken: A User-Centric Perspective'>Fixing What&#8217;s Not Broken: A User-Centric Perspective</a></li>
<li><a href='http://www.mikeziray.com/2010/07/01/why-your-code-sucks-naming-conventions/' rel='bookmark' title='Permanent Link: Why Your Code Sucks &#8211; Naming Conventions'>Why Your Code Sucks &#8211; Naming Conventions</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.mikeziray.com/2009/05/30/custom-mouse-cursor-for-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
	</item>
	</channel>
</rss>
