<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>untoldentertainment.com &#187; Tutorial</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.untoldentertainment.com/blog</link>
	<description>We Make Flash Games</description>
	<lastBuildDate>Wed, 01 Feb 2012 15:18:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<copyright>Copyright &#xA9; untoldentertainment.com 2011 </copyright>
	<managingEditor>ryan@untoldentertainment.com (untoldentertainment.com)</managingEditor>
	<webMaster>ryan@untoldentertainment.com (untoldentertainment.com)</webMaster>
	<image>
		<url>http://www.untoldentertainment.com/blog/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
		<title>untoldentertainment.com</title>
		<link>http://www.untoldentertainment.com/blog</link>
		<width>144</width>
		<height>144</height>
	</image>
	<itunes:subtitle></itunes:subtitle>
	<itunes:summary>We Make Flash Games</itunes:summary>
	<itunes:keywords></itunes:keywords>
	<itunes:category text="Society &#38; Culture" />
	<itunes:author>untoldentertainment.com</itunes:author>
	<itunes:owner>
		<itunes:name>untoldentertainment.com</itunes:name>
		<itunes:email>ryan@untoldentertainment.com</itunes:email>
	</itunes:owner>
	<itunes:block>no</itunes:block>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.untoldentertainment.com/blog/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<item>
		<title>Flash to iOS: A Step-by-Step Tutorial (Part 5)</title>
		<link>http://www.untoldentertainment.com/blog/2011/12/21/flash-to-ios-a-step-by-step-tutorial-part-5/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/12/21/flash-to-ios-a-step-by-step-tutorial-part-5/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 16:54:38 +0000</pubDate>
		<dc:creator>Sina Kashanizadeh</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPod]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=4272</guid>
		<description><![CDATA[This is the fifth part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch. Jump to other parts in the series: Part 1 &#8211; Sign up [...]]]></description>
			<content:encoded><![CDATA[<p><center></p>
<div class="displayed">
<img src="http://www.untoldentertainment.com/blog/img/2011_12_07/iPadAIR.png" alt="Flash to iOS: A Step-by-Step Tutorial" />
</div>
<p></center></p>
<p>This is the fifth part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch.</p>
<p>Jump to other parts in the series:</p>
<ul>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/" title="Flash to iOS: A Step-by-Step Tutorial (Part 1)"><b>Part 1</b> &#8211; Sign up for an Apple iOS Developer Account</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 2)"><b>Part 2</b> &#8211; Obtain your Signing Certificate &#038; Mobile Provisioning Profile, and create your App ID</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 3)"><b>Part 3</b> &#8211; Use FlashDevelop to build your mobile AIR app</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/" title="Flash to iOS: A Step-by-Step Tutorial (Part 4)"><b>Part 4</b> &#8211; Modify your project settings and test your app on an Apple device</a>
</ul>
<h2>Distribution Certificate</h2>
<p>Now that you&#8217;ve tested your app and it&#8217;s to your liking, guess you want to put it up on the App Store? Never fear, my friend! I will now show you how to get your app uploaded on the App Store so the whole world can ignore it and buy <b>Angry Birds</b> instead.</p>
<p>This process of distribution is a bittersweet one. It&#8217;s sweet because it&#8217;s <em>extremely similar</em> to the development process that you just went through, so you should be familiar with the convoluted certificates and hoops you have to jump through at Apple&#8217;s pleasure. It&#8217;s bitter because it&#8217;s <em>extremely similar</em> to the development process that you just went through.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/groundhogDay.jpg" alt="Groundhog Day" /></p>
<p>Something about this seems awfully familiar &#8230;
</p></div>
<p>Enough talk. Let’s get cracking!</p>
<h2>Generate a Signing Certificate Request</h2>
<p>Remember that OpenSSL program that you installed in <a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Guide Part 2 by Untold Entertainment">Part 2 of this tutorial</a>? You have to open that bad boy up again to create another <b>Certificate Signing Request</b>. Now, it <em>is</em> possible to just use your old Signing Certificate &#8211; however, just to be super clear and to avoid any confusion or difficulty, we&#8217;re going to create another one.</p>
<p><b>Note:</b> We&#8217;ll use this new Signing Certificate Request to get a <b>Distribution Certificate</b> rather than a <b>Development Certificate</b>.</p>
<ol>
<li>Open a command prompt window. You can do this by clicking the Windows <b>Start</b> button and typing in <b>cmd</b> in the search field. Alternately, you can also hold the Windows key on your keyboard and hit the &#8220;R&#8221; key (for &#8220;Run&#8221;), then type <b>cmd</b> and hit Enter.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/cmd.jpg" alt="Windows CLI" /></p>
</div>
<li>Once you are in the command prompt, navigate to your Open SSL <b>bin</b> folder. Depending on where you installed it, you will have to navigate to a different path than in this example. (i hope you installed Open SSL in a location that you can remember!)  Check <a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Guide Part 2 by Untold Entertainment">Part 2</a> if you need a refresher on Windows CLI (Command Line Interpreter) commands.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/cd.jpg" alt="Open SSL folder" /></p>
</div>
<li>Punch this command into the CLI and hit the ENTER key when you’re finished:

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl genrsa -out mykey.key 2048</pre></div></div>

<p>You should see this response:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/response.jpg" alt="Response" /></p>
</div>
<li>Next, type (or highlight the line, right-click, choose &#8220;Copy&#8221;, and right-click/&#8221;Paste&#8221; in the CLI):

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl req -new -key mykey.key -out CertificateSigningRequest.certSigningRequest  -subj &quot;/emailAddress=yourAddress<span style="color: #33cc33;">@</span>example.com, CN=John Doe, C=US&quot;</pre></div></div>

<p>Now, before you hit ENTER you’ll want to edit a couple of things. First, replace &#8220;John Doe&#8221; with your own name or company name. Then replace the &#8220;yourAddress@example.com&#8221; email with your own email. Press ENTER.</p>
<p>You should get a message similar to this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/response2.jpg" alt="Response 2" /></p>
</div>
<p>You just generated the <b>Signing Certificate Request file</b> that you&#8217;ll use to ask Apple for your <b>Distribution Signing Certificate</b>. The Signing Certificate Request file is located in the <b>bin</b> folder of your Open SSL install. It has a <b>.certSigningRequest</b> file extension and should look like this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/signingRequest.jpg" alt="Signing Certificate" /></p>
</div>
</ol>
<h2>Obtain a Signing Certificate for Distribution</h2>
<p>You have to upload your <b>Signing Certificate Request file</b> to the <b>Apple Provisioning Portal</b> to get your <b>Distribution Signing Certificate</b>.  Onward.</p>
<p><b>Note:</b> There have been instances where this upload does not work with Google Chrome. Just to be safe, use another browser like Firefox.</p>
<ol>
<li>Navigate to the <a href="http://developer.apple.com/devcenter/ios/index.action" title="Apple iOS Provisioning Portal">Apple iOS Provisioning Portal</a> within the Dev Center.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/portal.jpg" alt="Apple Provisioning Portal" /></p>
</div>
<li>Log in with your developer account and click on <b>Certificates</b>.
<li>Click on the <b>Distribution</b> tab.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/distribution.jpg" alt="Apple Provisioning Portal" /></p>
</div>
<p><b>Note:</b> In our previous tutorials, you requested a certificate from the <b>Development</b> tab.  An app signed with a Development certificate cannot be successfully submitted to the Apple App Store.</p>
<p>If you have an old <b>Distribution Certificate</b> because you&#8217;ve made a prior app, and you&#8217;d still like to follow along, you can go ahead and <b>Revoke</b> it. Revoking the Certificate will not affect your apps that are already on the App Store, because the Certificate is primarily used during the upload process so that Apple can identify you.</p>
<p><b>Note:</b> I recommend revoking an existing Certificate because Apple seems to issue only one Certificate per Team Agent. You must revoke any existing Certificate to be able to request another. Why is it set up like this? I have no idea.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/revoke.jpg" alt="Revoke your Apple Signing Certificate" /></p>
</div>
<li>If you don&#8217;t have a pre-existing <b>Distribution Signing Certificate</b>, you can just go ahead and click <b>Request Certificate</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/requestCertificate.jpg" alt="Request your Apple Signing Certificate" /></p>
</div>
<p>On the next screen, you will upload the <b>Signing Certificate Request file</b> that you generated in the previous section.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/request.jpg" alt="Request" /></p>
</div>
<li>Click the <b>Browse</b> button and navigate to the OpenSSL <b>bin</b> folder.
<li>Select the <b>Signing Certificate Request file</b> and click <b>Submit</b>.  When you are finished, you&#8217;ll see your <b>Distribution Signing Certificate</b> ready to download from the web page.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/yourCertificate.jpg" alt="Your Apple Signing Certificate" /></p>
</div>
</ol>
<p>Don&#8217;t download your <b>Distribution Certificate</b> quite yet. You still need to create an <b>App ID</b> and a <b>Mobile Provisioning Profile</b>.</p>
<h2>Create a New App ID</h2>
<p>When deciding what you want your App ID to be, you have two choices:</p>
<ul>
<li>Create an entirely new App ID
<li>Use the Existing App ID that you create in <a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial by Untold Entertainment">Part 2</a>
</ul>
<p>If you want to use your existing App ID, then just skip to the next section. If for some reason you don’t like the name of your App ID, go ahead and make a new App ID, following the steps outlined in <a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial Part 2 by Untold Entertainment">Part 2</a>.</p>
<h2>Create a Mobile Provisioning Profile for Distribution</h2>
<p>Now that you have an App ID, you can create your <b>Mobile Provisioning Profile</b>.</p>
<ol>
<li> Within the Provisioning Portal website, click <b>Provisioning</b> in the sidebar.
<li>Click the <b>Distribution</b> tab.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/newProfile.jpg" alt="New Provisioning Profile" /></p>
</div>
<p>In previous tutorials, you created a <b>Development</b> profile for testing.  You won&#8217;t be able to upload your app to the Apple App Store unless you create a <b>Distribution</b> profile, so make sure you select the correct tab.</p>
<p>You can either create a new <b>Provisioning Profile</b>, or modify an existing one.  Modifying an existing profile if you want to switch between <b>App Store</b> and <b>Ad Hoc</b> distribution. If you&#8217;ve never created a Distribution profile, read on. Instructions for modifying a profile are in the section following.</p>
<li>Click on <b>New Profile</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/newProfileButton.jpg" alt="New Provisioning Profile Button" /></p>
</div>
<p><b>Note:</b> You&#8217;ll only see this option if you are the fee-paying Team Admin.</p>
<li>In the next section, you&#8217;ll have to fill out a bunch of options. The first option is the <b>Distribution Method</b>. As you can see, you can choose between <b>App Store</b> and <b>Ad Hoc</b>.
<ul>
<li>Choose App Store if you are completely satisfied with your app and you want to submit it to Apple for review. If all goes well, then your app will be released whenever Apple deems it worthy.
<li>The Ad Hoc option can be thought of as a closed beta for your app. When you choose Ad Hoc, you are able to release your app to a maximum of 100 people, and only they will have access to it. Your App will NOT appear in the public App Store if you choose Ad Hoc.
</ul>
<p><b>Note:</b> Notice when you choose <b>App Store</b>, the &#8220;Devices&#8221; option is greyed out. When you choose <b>Ad Hoc</b>, it allows you to choose a number of devices that you have registered. The <b>App Store option</b> releases your app on the public App Store, so it doesn&#8217;t make sense to target specific devices.</p>
<li>Your <b>Profile Name</b> is the name that you want for your <b>Provisioning Profile</b>. The <b>Distribution Certificate</b> that you created should be visible here.
<li>Finally, you have to choose which <b>App ID</b> with which you want to register your <b>Provisioning Profile</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/profileOptions.jpg" alt="Profile Options" /></p>
</div>
<li>Click <b>Submit</b>.
<li>You should ee a list of the <b>Provisioning Profiles</b> that you&#8217;ve created. Click <b>download</b> on the <b>Provisioning Profile</b> that you just created, and save it to you computer.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/saveIt.jpg" alt="Save Your Provisioning Profile" /></p>
</div>
</ol>
<h2>Modify your Provisioning Profile</h2>
<p><b>Note:</b> If you&#8217;re not modifying an existing <b>Provisioning Profile</b> (say, from <b>Ad Hoc</b> to <b>App Store</b>), skip over this section.</p>
<ol>
<li>Within the <b>Provisioning Portal</b> website, click <b>Provisioning</b> in the sidebar.
<li>Click the <b>Distribution</b> tab.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/newProfile.jpg" alt="New Provisioning Profile" /></p>
</div>
<li>Click <b>Modify</b> on the <b>Provisioning Profile</b> that you want to reuse.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/modifyProfile.jpg" alt="Modify Provisioning Profile" /></p>
</div>
<li>Switch to either <b>Ad Hoc</b> or <b>App Store</b>. The distinction between these two options is explained in the preceding section.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/edit.jpg" alt="Edit Provisioning Profile" /></p>
</div>
<li>Click <b>Submit</b>.
<li>You should see a list of the <b>Provisioning Profiles</b> that you&#8217;ve created. Click <b>download</b> on the <b>Provisioning Profile</b> that you just modified, and save it to your computer.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/saveIt.jpg" alt="Save Your Provisioning Profile" /></p>
</div>
</ol>
<h2>Download your Distribution Certificate</h2>
<p>You just need to download the <b>Distribution Signing Certificate</b> that you created at the top of this tutorial,  and then you are ready to bundle your <b>.ipa</b> file to submit to the App Store.</p>
<ol>
<li>Click on the <b>Certificates</b> section in the sidebar.
<li>Click the <b>Distribution</b> tab.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/distribution.jpg" alt="Apple Provisioning Portal" /></p>
</div>
<li>You&#8217;ll see that your <b>Distribution Signing Certificate</b> is ready to download. You can also see that it contains the <b>Distribution Provisioning Profile</b> that you just made.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/ready.jpg" alt="Your Certificate is Ready" /></p>
</div>
</ol>
<li>Click download and save the file to your computer. Just to make things easier, you can save that certificate in the Open SSL <b>bin</b> folder, where your <b>Certificate Signing Request</b> file is sitting.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/hooray.jpg" alt="Hooray!" /></p>
</div>
</ol>
<p><b>Note:</b> Make sure not to get confused between your <em>Development</em> Certificate and Profile, and your <em>Distribution</em> Certificate and Profile.  You won&#8217;t be able to upload your app to the App Store if you accidentally use the Distribution set of certs in these next steps.</p>
<h2>Convert the Signing Certificate to a .p12 File</h2>
<p>You&#8217;ll have to perform the .p12 conversion on your Certificate again, as you did in an earlier tutorial.  Here are the steps:</p>
<ol>
<li>Open up your command prompt.
<li>Navigate to your Open SSL bin folder.
<li>Copy and paste this command:

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl x509 -in distribution_identity.cer -inform DER -out distribution_identity.pem -outform PEM</pre></div></div>

<p>After you enter that command, you&#8217;ll see a .pem file show up in your OpenSSL <b>bin</b> folder.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/pem.jpg" alt=".pem file" /></p>
</div>
<li>Copy and paste this command in the CLI:

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl pkcs12 -export -inkey mykey.key -in distribution_identity.pem -out iphone_dev.p12</pre></div></div>

<p><b>Note:</b> These are the exact same commands that you used for our Development Certificate in an earlier tutorial, except that the file name has been changed from &#8220;development&#8221; to &#8220;distribution&#8221;.</p>
<li>After you punch in the command, enter a password and then verify that password. Make sure the password is something that you will remember. (Save it in a .txt file! – Ed.)
<p><b>Note:</b> Remember that you may get an error mentioning a &#8220;random state&#8221;. Just type in the command set <b>RANDFILE=.rnd</b> and it should fix the problem for you.</p>
<li>Navigate to the <b>bin</b> folder in your Open SSL directory and you should see your <b>.p12</b> file. Hooray!
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_20/p12.jpg" alt=".p12 file" /></p>
</div>
</ol>
<h2>Hello, Planet</h2>
<p>As we mentioned off the top, much of this is familiar territory.  With all of your Distribution certs sorted out, you&#8217;re ready to bundle up your final <b>.ipa</b> file and foist it onto an unsuspecting public.  In the next tutorial, you&#8217;ll follow familiar instructions to bind your certs to your <b>.ipa</b>, and experience that magical moment of uploading your app for Apple&#8217;s approval (followed shortly by that equally magical moment of getting your app rejected cuz BEWBZ.)  Let&#8217;s do it!</p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F12%2F21%2Fflash-to-ios-a-step-by-step-tutorial-part-5%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F12%2F21%2Fflash-to-ios-a-step-by-step-tutorial-part-5%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/12/21/flash-to-ios-a-step-by-step-tutorial-part-5/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=4272&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/12/21/flash-to-ios-a-step-by-step-tutorial-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash to iOS: A Step-by-Step Tutorial (Part 4)</title>
		<link>http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 21:43:56 +0000</pubDate>
		<dc:creator>Sina Kashanizadeh</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=4171</guid>
		<description><![CDATA[This is the fourth part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch. Jump to other parts in the series: Part 1 &#8211; Sign up [...]]]></description>
			<content:encoded><![CDATA[<p><center></p>
<div class="displayed">
<img src="http://www.untoldentertainment.com/blog/img/2011_12_07/iPadAIR.png" alt="Flash to iOS: A Step-by-Step Tutorial" />
</div>
<p></center></p>
<p>This is the fourth part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch.</p>
<p>Jump to other parts in the series:</p>
<ul>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/" title="Flash to iOS: A Step-by-Step Tutorial (Part 1)"><b>Part 1</b> &#8211; Sign up for an Apple iOS Developer Account</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 2)"><b>Part 2</b> &#8211; Obtain your Signing Certificate &#038; Mobile Provisioning Profile, and create your App ID</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 3)"><b>Part 3</b> -Use FlashDevelop to build your mobile AIR app</a>
</ul>
<p>In this section of the tutorial, the rubber meets the road.  You&#8217;ll make all the finicky adjustments to FlashDevelop and the AIR bundling files to get your Hello Planet app up and running on your testing device.</p>
<h2>Point FlashDevelop to the Merged SDK Folder</h2>
<p>Now that your (cr)app is successfully up and running, you have to make a few tweaks and adjustments to FlashDevelop. You&#8217;ll start by telling FlashDevelop all about that merged SDK folder you created in Part 3. </p>
<ol>
<li>In the Project panel on the right, you&#8217;ll see a file called AIR_iOS_readme.txt. Double click that file to open it.
<p>This txt file lists a number of settings needed to get your ipa file working.  The ipa file is the Holy Grail- the native app file that you&#8217;ll send to your testing device to see everything working properly. A window pops up with a bunch of options.</p>
<li>Click the Project Properties button at the top of the Properties Panel (alternately, you can use the menus to navigate to Project>Properties).
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/projectProperties.jpg" alt="FlashDevelop Project Properties"></p>
</div>
<li>Change the version of your AIR SDK from 2.7 to <b>3.0</b> because (at the time of this writing), that is the latest version.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/projectPropertiesOptions.jpg" alt="FlashDevelop Project Properties Options"></p>
</div>
<li>Click on the SDK tab, and click <b>Manage</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/manage.jpg" alt="FlashDevelop Project Properties Options - Manage"></p>
</div>
<li>On the next screen you will see list of installed Flex SDKs. Click the ellipsis (&#8230;) button at the end of that line.  An <b>InstalledSDK Collection Editor</b> window pops up.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/ellipsis.jpg" alt="FlashDevelop Project Properties Options - Flex SDK Location"></p>
</div>
<li>You&#8217;re going to point FlashDevelop to the location of our recently-downloaded Flex SDK (the same folder that contains both the Flex SDK and AIR SDK  that you created earlier in this tutorial).  First click the <b>Add</b> button, and then click on the <b>Path</b> line to get another ellipsis. Click on THAT ellipsis to browse your computer.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/add.jpg" alt="Add"></p>
</div>
<li>Once you have clicked the ellipsis button, a Windows File Browser appears. Navigate to the merged SDK directory. Select that folder and click <b>OK</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/navigate.jpg" alt="Navigate to your merged SDK folder"></p>
</div>
<li>Click <b>OK</b> again to exit the <b>InstalledSDK Collection Editor</b> window.
<li>Click <b>Close</b>.
<li>In the drop-down menu, you should see the Default option. Click on the drop-down list and choose the SDK location you just told FlashDevelop about.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/dropDown.jpg" alt="Choose your Merged SDK profile from the drop-down menu."></p>
</div>
<li>Click <b>Apply</b>.
<li>Click <b>OK</b>. Voila! Your SDK is now setup for your FlashDevelop Project.
<p>Let’s just test it out quickly to see if it works. The code you pasted in the previous tutorial prints out the words &#8220;Hello Planet&#8221; within your app. Well, I&#8217;m going to change mine to say &#8220;Sina is Cool&#8221;, just because I can, and also because I AM COOL. (You may be tempted to replace &#8220;Sina&#8221; with your own name.  Please don&#8217;t, because uh &#8230; your code won&#8217;t work.)</p>
<p>Your resulting app should look like this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/sinaIsCool.jpg" alt="Breaking: Sina is cool."></p>
</div>
<p>A beautiful sight, isn&#8217;t it?</p>
<p>(At this point, Sina actually is pretty damned cool. &#8211; Ed.)</p>
</ol>
<h2>Delete the Extra Icons Folder</h2>
<p>Now you have FlashDevelop set up to create mobile-formatted AIR apps. Your app compiles, and you have your <b>Mobile Provisioning Profile</b> and your <b>.p12 Signing Certificate</b> waiting in the wings. All you need to do is combine all these amazing files together to create the Holy Grail .ipa file.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/holyGrail.jpg" alt="The .ipa file is the Holy Grail of this tutorial series"></p>
<p>Ah &#8211; this is truly the .ipa of a FlashDeveloper.
</p></div>
<p>An <b>.ipa</b> file is like the program file &#8211; the Windows executable (.exe) &#8211; that you need to test your iPhone/iPad App on a device, and ultimately upload it to the Apple App Store.</p>
<p>The creators of FlashDevelop 4 made a minor error when they created the Mobile App template: the template generates a duplicate <b>icons</b> folder that will cause a conflict when you try to bundle your <b>.ipa</b> file. You need to get rid of this extra folder to make sure your .ipa file to builds properly. </p>
<ol>
<li>Look in the right sidebar that lists your files and folders. You&#8217;ll see a folder called <b>bin</b>.  (This is one of the folders that FlashDevelop created for you automatically when you created your project.)
<li>Click on the little plus sign beside the <b>bin</b> folder to expand it. You will see a folder called <b>icons</b> tucked inside. Right-click the <b>icons</b> folder and click <b>Delete</b>. Problem solved.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/delete.jpg" alt="Delete the icons folder to avoid a conflict later"></p>
</div>
</ol>
<h2>Integrate Your Cert Files</h2>
</ol>
<li>Copy your <b>Mobile Provisioning Profile</b> and your <b>.p12 Signing Certificate</b> into the <b>certs</b> folder of your project. If you&#8217;ve been following along with this tutorial from the beginning, you probably saved these two files in the <b>bin</b> folder of your OpenSSL install.
<p>Once the files are in the right place, they should show up in your Project sidebar:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/certs.jpg" alt="You put yer WEED in there."></p>
</div>
<p>To create the <b>.ipa</b>, you have to edit a couple of lines in a <b>batch file</b> so that the app compiles properly. Don’t be scared: a batch file (.bat) is simply a text file that runs a script when you double-click it in Windows. You can open up a batch file by using a text editor of any kind.  Since you&#8217;ve already got FlashDevelop open, it makes a lot of sense to use it to edit the batch files.</p>
<p>In the following screenshot, I&#8217;ve opened up the ReadMe file that describes the iOS compilation steps. Step 5 talks about editing a batch file. Take a read if you&#8217;re nervous.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/readme.jpg" alt="iOS compilation readme"></p>
</div>
<li>In the sidebar, find the <b>bat</b> folder and expand it.
<li>Double-click the <b>SetupApplication.bat</b> file. The file should open in FlashDevelop.
<li>In the batch file, you should see some green text that says <b>ios packaging</b>, and a bunch of <b>set</b> options beneath it. This is where you have to add or adjust some text. In the IOS_DEV_CERT_FILE line, type the path to your .p12 <b>Signing Certificate</b> file (the one you just copied into the <b>certs</b> folder). You only have to use a <em>relative path</em> (one that describes where the file is in relation to the compilation target), so just copy what I have and you should be fine:

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">cert\iphone_dev.p12</pre></div></div>

<li>In the IOS_DEV_CERT_PASS line, you have to enter password that you set when you made your <b>Signing Certificate</b>. (Don&#8217;t come crying to me if you don&#8217;t remember it &#8211; i told you to write it down &#8211; Ed.)
<li>In the IOS_PROVISION line, type the name of your <b>Mobile Provisioning Profile</b>.
<p>All in all your SetupApplication.bat should look like this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/setupApplication.jpg" alt="SetupApplication.bat"></p>
</div>
<p><b>Note:</b> Make sure you have the path properly setup as well as the same exact filename. If you have any sort of typo, it&#8217;s not going to work.
</ol>
<h2>Get Your Ducks in a Row</h2>
<p>You&#8217;re almost there! All you have to do is edit the <b>SetupSDK.bat</b> file.</p>
<ol>
<li>Double-click the <b>SetupSDK.bat</b> file to open it in FlashDevelop.
<li>At the top of the file, you&#8217;ll see a line that says <b>path to Flex SDK</b> in green. Right under that line is the path to the SDK. However, it&#8217;s not pointing to the merged SDK folder that we created earlier. Replace the default path with the path to your merged SDK folder. It might look similar to this:
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/setupSDK.jpg" alt="SetupSDK.bat"></p>
</div>
<p><b>Note:</b> Remember to save often!</p>
<li>Open the <b>application.xml</b> file. This xml file is the descriptor file for your application. You have to double-check to ensure everything in there is correct. Everything from your App name to the version of your App is located in this file.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/applicationXML.jpg" alt="Application.xml"></p>
</div>
<li>Change the version number at the end of the <b>xmlns</b> parameter of the root <b>application</b> node to the version of AIR you&#8217;re running. In this case (and at the time of this writing), it&#8217;s <b>3.0</b>.
<li>In the <b>id</b> node, enter the package name that you created for your <b>Mobile Provisioning Profile</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/applicationXMLChanges.jpg" alt="Application.xml changes"></p>
</div>
<p>If you don’t remember what your Bundle ID was, go to the Provisioning Portal on the <a href="http://developer.apple.com" title="Apple Developer Site">iOS developer website</a> and click on <b>Provisioning</b>.</p>
</ol>
<h2>Compile Time</h2>
<p>After all that, you&#8217;re finally ready to compile your <b>.ipa</b> file to test on an iOS device.</p>
<ol>
<li>In the sidebar, right-click the <b>PackageApp.bat</b> file and select <b>Execute</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/execute.jpg" alt="Execute!"></p>
</div>
<li>You&#8217;ll see a bunch of options. Since this is only a test, you can pick the <b>fast test</b> option. This option will quickly create an <b>.ipa</b> file for you to throw onto your iWhatever. Once you choose <b>fast test</b>, sit back and relax.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/choices.jpg" alt="Choices"></p>
</div>
<li>Look in the <b>dist</b> folder in the sidebar. You now have an <b>.ipa</b> file!
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/success.jpg" alt="Success!"></p>
</div>
</ol>
<h2>Test Your New App</h2>
<p>Take a moment and soak in what you&#8217;ve accomplished. You&#8217;re in the final steps of your epic journey of iEnlightenment. All you have to do now is load the hard-fought <b>.ipa</b> file up on your iWhatever.</p>
<p>I made a copy of my <b>Mobile Provisioning Profile</b> and my <b>.ipa</b> file and put them in a separate folder. I suggest you do the same as a backup measure &#8230; better safe than sorry.  Now let&#8217;s get that sucker onto your device.</p>
<ol>
<li>Plug in your iPhone/iPad/iPod Touch/iEtCetera and let it sync in iTunes.
<li>After it&#8217;s synced, go to <b>Library -> Apps</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/iTunes.jpg" alt="iTunes"></p>
</div>
<li>To add your <b>Mobile Provisioning Profile</b>, click and drag it from your folder into the <b>iTunes Library</b>.
<li>Follow the same process to add your <b>.ipa</b> file.
<p><b>Note:</b> You have to add your <b>Mobile Provisioning Profile</b> before you add your <b>.ipa</b> file! If you add these files in the wrong order, you may get errors!</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/dragAndDrop.jpg" alt="Drag and drop your files into iTunes"></p>
</div>
<p>After dragging your <b>.ipa</b> file into iTunes, you&#8217;ll see a generic App icon that FlashDevelop/AIR generated for you.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/genericIcon.jpg" alt="Generic Apple App Icon"></p>
</div>
<li>In the left sidebar, click on <b>Devices -> Your iPad/iPod/iPhone</b>.
<li>Click on the little <b>Apps</b> button at the top of iTunes. You&#8217;ll see a screen that displays all your Apps. Within that screen, you should be able to find your App (you may have to scroll around a little to find it).
<li>Click the <b>Apply</b> button to transfer everything to your device.
<p>iTunes will go through the syncing process, and will add your App to your device. You can click sync a few more times just to make sure everything is good &#8211; iTunes doesn&#8217;t always behave perfectly on a PC.</p>
<p><b>Note:</b> If you get an error saying that the App failed to install, your <b>Bundle ID</b> or something within your <b>Application.xml</b> may be wrong. Double-check everything! There is no harm in deleting your App and from iTunes and transferring it again. You can also overwrite your <b>Mobile Provision Profile</b> with a corrected one.</p>
<p><b>DOUBLE NOTE:</b> If you change anything in your project, you must compile your <b>.ipa</b> file again, because it contains all the new information necessary for iTunes to run your App.</p>
<li>Fire up your iPad/iPhone and BAM!
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/beautiful.jpg" alt="AIR on iOS"></p>
</div>
<p>It’s so &#8230; beautiful. *sniff*
</ol>
<h2>To the App Store &#8230; and Beyond!</h2>
<p>In this section of the tutorial, you:</p>
<ul>
<li>Pointed FlashDevelop to your merged AIR/Flex SDK folder
<li>Corrected an error with the Mobile AIR template by deleting an extraneous icons folder
<li>Copied your <b>Mobile Provisioning Profile</b> and your <b>.p12 Signing Certificate</b> into your project and pointed FlashDevelop to them
<li>Pointed the SetupSDK.bat file to the merged SDK folder
<li>Modified the application.xml descriptor file to match your project settings
<li>Compiled your project and created an <b>.ipa</b> file
<li>Copied your <b>.ipa</b> file and <b>Mobile Provisioning Profile</b> into iTunes
<li>Synced your app and ran it on your testing device!
</ul>
<p>With your project set up, you can continue developing your application until you have an amazing <em>thing</em> that you&#8217;re ready to foist on the app-buying public. There are still a few more things you need to know to deploy your app to the live iTunes store.  What about that generic AIR icon?  Wouldn&#8217;t you like to see something nicer in its place?  And how do you get your <b>.ipa</b> file in front of the app-approvers at Apple to get it to show up in iTunes?  And are we finished with certificates and profiles, or is there more to do?</p>
<p>Short answer? <em>There&#8217;s more to do.</em>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F12%2F14%2Fflash-to-ios-a-step-by-step-tutorial-part-4%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F12%2F14%2Fflash-to-ios-a-step-by-step-tutorial-part-4%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=4171&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Flash to iOS: A Step-by-Step Tutorial (Part 3)</title>
		<link>http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 16:25:16 +0000</pubDate>
		<dc:creator>Sina Kashanizadeh</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=4193</guid>
		<description><![CDATA[This is the third part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch. Jump to other parts in the series: Part 1 &#8211; Sign up [...]]]></description>
			<content:encoded><![CDATA[<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_07/iPadAIR.png" alt="Flash to iOS: A Step-by-Step Tutorial"></p>
</div>
<p>This is the third part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch.</p>
<p>Jump to other parts in the series:</p>
<ul>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/" title="Flash to iOS: A Step-by-Step Tutorial (Part 1)"><b>Part 1</b> &#8211; Sign up for an Apple iOS Developer Account</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 2)"><b>Part 2</b> &#8211; Obtain your Signing Certificate &#038; Mobile Provisioning Profile, and create your App ID</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/" title="Flash to iOS: A Step-by-Step Tutorial (Part 4)"><b>Part 4</b> &#8211; Modify your project settings and test your app on an Apple device</a>
</ul>
<h2>Luke Loses His Hand</h2>
<p><a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 2)">Part 2</a> of this tutorial was like <b>The Empire Strikes Back</b>.  It was the crisis moment in the story of deploying a Flash project to the Apple iOS platform.  It was dark, it was difficult, and Han Solo wound up frozen in carbonite.  Part 3 is redemptive: with all that command line and certificate red tape out of the way, you can finally build our AS3 app using FlashDevelop. Watch closely, friends: you may spot some ewoks.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/iPhone.jpg" alt="It's a trap."></p>
<p>It&#8217;s a trap.
</p></div>
<h2>Install Flash Develop</h2>
<p>As I mentioned earlier in the series, FlashDevelop is a FREE, PC-only coding environment that is used throughout the industry and loved by many. If you haven&#8217;t installed it, <a href="http://www.flashdevelop.org/community/viewforum.php?f=11">head here to download the latest version.</a></p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/fd4.jpg" alt="FlashDevelop 4"></p>
</div>
<p>FlashDevelop is an IDE (Integrated Development Environment) where you can create many different types of applications from scratch.  The most amazing thing about FlashDevelop is that you don’t actually need to buy Adobe&#8217;s Flash Professional software to create Flash (swf) content. FlashDevelop has amazing, time-saving features like importing code libraries on the fly, and numerous awesome templates. At the time of this writing, the latest edition of FlashDevelop is 4.0.0. This version of FlashDevelop contains a template for Mobile AIR applications which you can use for iOS and Android development. In this tutorial, you&#8217;ll use the iOS version of the template.</p>
<h2>Create a Merged SDK Folder</h2>
<p>There are two SDKs (Software Development Kits) you&#8217;ll need to deploy Flash content to the iOS platform: Flex and AIR. Depending on the version of FlashDevelop you install, these two SDKs may already be included in the download.  You&#8217;re going to download them both anew and put them in a single folder. That way, if you mess anything up, you can take off and nuke the site from orbit instead of messing up your main FD install.  It&#8217;s the only way.</p>
<ol>
<li><a href="http://www.adobe.com/special/products/air/sdk/thankyou.html?download=win" title="Download Adobe AIR SDK">Download the latest AIR SDK</a> (which, at the time of this writing, was v3.0)
<li><a href="http://opensource.adobe.com/wiki/display/flexsdk/Downloads" title="Download the latest Flex SDK">Download the Flex SDK</a>.
<li>These two packages download as <b>.zip</b> files.  Extract them wherever you like on your computer &#8211; just make sure you know where you put them.
<li>Once you have both SDK&#8217;s extracted to separate folders, copy everything in the AIR SDK folder into the Flex SDK folder. Windows will ask you if you want to overwrite certain files. Say &#8220;yes&#8221; to everything.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/merge.jpg" alt="Merged SDK folder"></p>
</div>
<p>You now have a folder with the <b>Flex SDK</b> and the <b>AIR SDK</b> merged into one. This is a Good Thing™.</p>
</ol>
<h2>Point FlashDevelop to the Flex SDK</h2>
<p>In order to compile (build and execute) your app, you have to point FlashDevelop to the merged SDK folder that you just created.</p>
<ol>
<li>Open up FlashDevelop and hit <b>F10</b> or go to <b>Tools->Program Settings</b> in the menus.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/programSettings.jpg" alt="Program Settings"></p>
</div>
<li>In the Program Settings menu, click on <b>AS3 Context</b>.
<li>Find the <b>Language</b> section, and the <b>Flex SDK Location</b> line within it.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/as3Context.jpg" alt="AS3 Context"></p>
</div>
<li>Click the ellipsis button at the end of that line, and navigate to the merged SDK folder.
<li>Click <b>Close</b> to commit the change.
</ol>
<h2>Set Up Your Mobile AIR Application</h2>
<p>Once you have downloaded and installed FlashDevelop, open it up and you should see it in all its glory:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/fdInterface.jpg" alt="FlashDevelop 4 Interace"></p>
</div>
<p>To begin with, you&#8217;ll need to create a new FlashDevelop project, which is a group of folders governed by a file filled with metadata about your project.</p>
<ol>
<li>Create a new project by clicking <b>Project -> New Project</b> in the menu.
<li>Now you will see a screen with a multitude of options. These are all project starter templates that FlashDevelop has for you to use. The one you want is the <b>AIR Mobile AS3 App</b>. Select this template, choose a location on your hard drive to save it, give a name to your project, and hit OK.
<p>Note: A package name is not required but the option is there if you&#8217;d like to use it.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/fdTemplates.jpg" alt="FlashDevelop 4 Project Templates"></p>
</div>
<p>After you hit OK, FlashDevelop creates a number of folders and files for you, including:</p>
<ul>
<li>The .as3proj file, which is a FlashDevelop project file filled with metadata that keeps track of various aspects of your project.
<li>The <em>bin</em> folder. This is where your finished files end up.
<li>The <em>lib</em> folder, where you can put your &#8220;construction&#8221; files and artwork.
<li>The <em>src</em> folder, which contains your .as AS3 code files.
</ul>
<li>Once you&#8217;ve created your project file, FlashDevelop starts you off with a blank screen. To open up your code, click on the <b>src</b> folder in the Project sidebar on the right. (The Project sidebar lists your computer&#8217;s actual directory structure so that you can access your Actionscript files quickly and easily.)
<li>Inside the <b>src</b> folder, double click the <b>Main.as</b> file to open it. This is your project&#8217;s point of entry, where you can start writing the code for your app.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/main.jpg" alt="FlashDevelop 4 Main.as"></p>
</div>
<li>Push the F5 key on your keyboard, or the click the little play button at the top-middle of FlashDevelop to compile and run your project.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/compile.jpg" alt="FlashDevelop 4 Compile Button"></p>
</div>
<p>The first compile will take little bit of time, but any compile after that should be quick, because the compiler builds on an as-needed basis, compiling only your most recent changes (as opposed to Flash Professional, which compiles your ENTIRE project every single time. Bleh!). Once you run your new project, the FlashPlayer should pop up and you should see a blank screen. Luckily, this is exactly what we want. As of right now, this is your App.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/blankApp.jpg" alt="FlashDevelop 4 Blank App"></p>
</div>
</ol>
<h2>Hello, Planet</h2>
<p>This is nice and all, but let&#8217;s try to the write a simple message on screen so that when we are testing, we know it actually works. Let&#8217;s write &#8220;Hello Planet&#8221; on-screen.</p>
<ol>
<li>Just below the green commented line where it says //entry point in your <b>Main.as</b> file, copy and paste these lines into your code:

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> tf:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;  
<span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;  
format.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;_sans&quot;</span>;  
format.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">48</span>;  
tf.<span style="color: #006600;">defaultTextFormat</span> = format;  
tf.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Hello Planet&quot;</span>;  
tf.<span style="color: #0066CC;">width</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - <span style="color: #cc66cc;">20</span>;  
tf.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>;  
tf.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;  
addChild<span style="color: #66cc66;">&#40;</span>tf<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/entryPoint.jpg" alt="Code Entry Point"></p>
</div>
<li>Click the compile button (or press F5). Did it screw up? Good. There are a couple more things you have to do before you are ready to compile without errors.
<li>In your code, click to place your cursor carat on the word <b>Textfield</b> and hit Ctrl+Shift+1 on your keyboard. The word turns blue. If you scroll to the top of the class, you&#8217;ll notice that there is an extra import line at the top of your code. CTRL+SHIFT+1 is a special feature of FlashDevelop that does code completion. In this instance, CTRL+SHIFT+1 automatically handles your import statements for you.  You need to import the TextField code with this statement if you want your TextField to work.
<li>Use CTRL+SHIFT+1 on the <b>TextFormat</b> word and you should see this:
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/imports.jpg" alt="Import Statements"></p>
</div>
<li>Now click Compile (or press F5).
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_03/helloPlanet.jpg" alt="Hello Planet"></p>
<p>Thar she blows!
</p></div>
<p>(It works, but it&#8217;s no Angry Birds. &#8211; Ed.)
</ol>
<h2>Bust Out the Hardware</h2>
<p>This is moving along nicely.  You:</p>
<ul>
<li>Installed FlashDevelop, a free and open-source IDE for creating Flash and AIR content
<li>Downloaded and sunk the latest AIR SDK and Flex SDK into a single, merged folder
<li>Pointed FlashDevelop to the Flex SDK to use its superior compiler
<li>Created a new FlashDevelop project using the Mobile AIR application template
<li>Wrote and compiled a new Hello Planet AIR app that you can deploy to a testing device
</ul>
<p>In the next part of the tutorial, you&#8217;ll fine-tune FlashDevelop and modify a number of settings and batch files to create the magical, mystical <b>.ipa</b> file, and you&#8217;ll see that file running on your testing device.<br />
</a></p>
<p><a href="http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/" title="Flash to iOS: A Step-by-Step Tutorial (Part 4)">Continue to <b>Part 4</b></a>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F12%2F07%2Fflash-to-ios-a-step-by-step-tutorial-part-3-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F12%2F07%2Fflash-to-ios-a-step-by-step-tutorial-part-3-2%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=4193&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Flash to iOS: A Step-by-Step Tutorial (Part 2)</title>
		<link>http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 15:09:49 +0000</pubDate>
		<dc:creator>Sina Kashanizadeh</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=4097</guid>
		<description><![CDATA[This is the second part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch. Jump to other parts in the series: Part 1 &#8211; Sign up [...]]]></description>
			<content:encoded><![CDATA[<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_07/iPadAIR.png" alt="Flash to iOS: A Step-by-Step Tutorial"></p>
</div>
<p>This is the second part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch.</p>
<p>Jump to other parts in the series:</p>
<ul>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/" title="Flash to iOS: A Step-by-Step Tutorial (Part 1)"><b>Part 1</b> &#8211; Sign up for an Apple iOS Developer Account</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 3)"><b>Part 3</b> -Use FlashDevelop to build your mobile AIR app</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/" title="Flash to iOS: A Step-by-Step Tutorial (Part 4)"><b>Part 4</b> &#8211; Modify your project settings and test your app on an Apple device</a>
</ul>
<h2>Drudgery</h2>
<p>Without a doubt, this section of our tutorial series on bundling your Adobe AIR App for Apple&#8217;s iOS platform is the pits.  You have to use a complicated command line interpreter, you have to juggle what may feel like a million different certificates and profiles, and you have to jump through a dizzying array of hoops just to get everything set up &#8230; and you&#8217;re not even going to <em>start</em> building your app at all!  But trust me: once you&#8217;ve cleared these hurdles, the rest of the process is gravy. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/gravy.jpg" alt="Gravy"></p>
<p>Thick, delicious gravy.
</p></div>
<p>The first goal of this tutorial is to create a <em>signing certificate</em>, which is used very much like a hot brand on cattle. It&#8217;s the digital signature of you or your company that identifies an app as your own. We need to use a program called OpenSSL to create this signing certificate.</p>
<h2>Install Open SSL</h2>
<p>In order to turn your future AIR project into a native app, you need to create two important files: a Signing Certificate and a Mobile Provisioning Profile. If you are working on a Mac, you actually use a program called Keychain to do some fancy voodoo magic and get the certificate you need.  We are working on a PC and we do not have that luxury, so there are a couple of confusing steps that we must go through. </p>
<p>The first thing you have to do is go to <a href="http://www.slproweb.com/products/Win32OpenSSL.html">http://www.slproweb.com/products/Win32OpenSSL.html</a>. Scroll down, and then download the Visual C++ 2008 Redistributables and install the resulting file. Next, download the <b>Win32 OpenSSL v1.0a Light</b> file and install that after you have installed the first package. These two files will help you create the Signing Certificate Request you so richly deserve.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/openSSL.jpg" alt="OpenSSL"></p>
</div>
<p>Be sure to keep track of where you installed those last two elements. Next, visit this site:</p>
<p><a href="http://help.adobe.com/en_US/as3/iphone/index.html">http://help.adobe.com/en_US/as3/iphone/index.html</a>.</p>
<p>These are Adobe&#8217;s help files for generating your signing certificate and then converting that certificate into a .P12 file. If you get lost in this tutorial, that page will be your safety net.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/adobeHelpPage.jpg" alt="Adobe Help Page"></p>
</div>
<p>On that Adobe support page, you will see some instructions for Mac machines. Ignore them.  Scroll down to the Windows instructions and give them a once-over.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/instructions.jpg" alt="Instructions"></p>
</div>
<p>The instructions say that you have to install Open SSL. You&#8217;ve already done that. Open a command prompt window. You can do this by going to your <b>Start</b> button and typing in cmd in the search field.  (You can also hold the Windows key on your keyboard and hit the &#8220;R&#8221; key (for &#8220;Run&#8221;), then type <b>cmd</b> and hit Enter.)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/cmd.jpg" alt="CMD"></p>
</div>
<p>Once you are in the command prompt, travel your way to your Open SSL bin folder. Depending on where you installed it you will have to travel to a different path than in this example.  (For your sake, i hope you installed Open SSL in a location that you can remember!) </p>
<p>If this is your first time using the command prompt, here&#8217;s a quick primer on the commands that will help you get around:</p>
<ul>
<li><b>cd</b> This command means &#8220;Change Directory&#8221;. If you are in a directory and you want to go into one of its sub-directories, use this command plus the directory name (example: cd directoryName)
<li><b>cd..</b> This command will pull you up one level in your directory structure
<li><b>dir</b> Type this command to see a list of all of the files and folders within your current directory
<li><b>dir /w</b> The /w (wide) switch spreads a directory listing across multiple columns.  This is useful for seeing all the folder names at a glance, instead of in one big long scrolling list.
</ul>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/cd.jpg" alt="CLI Commands"></p>
</div>
<p>You may have some problems getting some of the following commands to work. A fine commenter (and Ontario resident!) named Dan Zen mentioned on the Adobe guide&#8217;s comments section that you may need to input <b>set RANDFILE=.rnd</b> in your command prompt before the following commands will work properly.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/randfile.jpg" alt="RANDFILE command"></p>
</div>
<h2>Generate a Signing Certificate Request File</h2>
<p>With all of that taken care of, you can start inputting some commands to create your Signing Certificate Request file.  Once you have that file, you&#8217;ll use it to ask Apple for your proper Signing Certificate file that you&#8217;ll use to identify your app. </p>
<ol>
<li> Punch this command into your command line interpreter and hit the ENTER key when you&#8217;re finished:

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl genrsa -out mykey.key 2048</pre></div></div>

<p>You should see this response:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/response.jpg" alt="response"></p>
</div>
<li>Next, type (or highlight the line, right-click, choose &#8220;Copy&#8221;, and right-click/&#8221;Paste&#8221; in the command line interpreter):

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl req -new -key mykey.key -out CertificateSigningRequest.certSigningRequest  -subj &quot;/emailAddress=yourAddress<span style="color: #33cc33;">@</span>example.com, CN=John Doe, C=US&quot;</pre></div></div>

<li>Now, before you hit ENTER you&#8217;ll want to edit a couple of things. First, replace &#8220;John Doe&#8221; with your own name or company name. Then replace the &#8220;yourAddress@example.com&#8221; email with your own email. Then press ENTER.
<p>You should get a message similar to this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/response2.jpg" alt="response"></p>
</div>
<p>You just generated the Signing Certificate Request file that you&#8217;ll use to ask Apple for your Signing Certificate. The  Signing Certificate Request file is located in the <em>bin</em> folder of your Open SSL install.  It has a <em>.certSigningRequest</em> file extension and should look like this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/signingRequest.jpg" alt="Certificate Signing Request file"></p>
</div>
</ol>
<h2>Obtain a Signing Certificate for Development</h2>
<p>You have to upload your Signing Certificate Request file to the Apple Provisioning Portal to get your signing certificate.</p>
<p>Note: There have been instances where this upload does not work with Google Chrome. Just to be safe, use another browser like Firefox.</p>
<ol>
<li>Navigate to the iOS Provisioning Portal after logging in with your <a href="http://developer.apple.com">developer account</a> and click on <b>Certificates</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/certificates.jpg" alt="Certificates"></p>
</div>
<li>Click <b>Request Certificate</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/requestCertificate.jpg" alt="Request Certificate"></p>
</div>
<p>Note: If you are a team member and/or you have not paid the annual developer fee of $99 dollars, then you cannot request a Signing Certificate. Only the Team Admin &#8211; the person who has paid the development fee &#8211; will be able make this request.</p>
<p>On the next screen, you will upload the Signing Certificate Request file that you generated in the previous section.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/generateCertificate.jpg" alt="Generate Certificate"></p>
</div>
<li>Click the <b>Browse</b> button and navigate to the <b>bin</b> folder located within your OpenSSL folder.
<li>Once you have selected the Signing Certificate Request file, click Submit. Your next screen should show this:
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/pending.jpg" alt="Certificate Pending"></p>
</div>
<li>Just hit <em>Refresh</em> a couple of times on your browser and you should eventually see this:
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/certificateReady.jpg" alt="Certificate Ready"></p>
</div>
<li>It&#8217;s your Signing Certificate! Go ahead and download it. Just to make things easier, you can save that certificate in the <b>bin</b> folder in the Open SSL folder, where your Certificate Signing Request file is sitting.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/saveCertificate.jpg" alt="Save Certificate"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/saveCertificate2.jpg" alt="Save Certificate 2"></p>
</div>
<h2>Convert the Signing Certificate to a .p12 File</h2>
<p>Are you having fun yet? Yeah, me neither. We are making good progress though. The next step is converting that Signing Certificate to a p12 certificate, because <em>Apple loves certificates</em>.</p>
<ol>
<li>From the Adobe help page, visit the link describing the process of <a href="http://help.adobe.com/en_US/as3/iphone/WS144092a96ffef7cc-371badff126abc17b1f-7fff.html">converting a developer certificate to a p12 file</a>. You get a list of more commands that you&#8217;ll have to punch into your trusty old command prompt.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/helpPage2.jpg" alt="Adobe Help Page 2"></p>
</div>
<li>Copy the first command (highlight the text, right-click and select &#8220;Copy&#8221; from the context menu):

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM</pre></div></div>

<li>Open up your command prompt as I showed earlier, and navigate to the <b>bin</b> folder of your Open SSL directory.
<li>Once you&#8217;re there, paste in the first command (right-click and select &#8220;Paste&#8221; from the context menu), and press ENTER.
<li>Skip ahead to Step 3 of the Adobe help guide and copy that command. (The command in Step 2 is for Macs only.)

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">openssl pkcs12 -export -inkey mykey.key -in developer_identity.pem -out iphone_dev.p12</pre></div></div>

<li>Once you punch in the command, enter a password and then verify that password. Make sure the password is something that you will remember.  (Save it in a .txt file in that folder if you&#8217;ve got a lousy memory like me &#8211; Ed.) When you are typing in your password, the command prompt does not give you the luxury of seeing dots or asterisks to indicate how many letters you are typing in. Rest assured that the command is accepting your input.
<p>Note: You <em>may</em> get an error mentioning a &#8220;random state&#8221;. Just type in the command <b>set RANDFILE=.rnd</b> and it should fix the problem for you.</p>
<li>Go into the <b>bin</b> folder in your Open SSL directory and you should see your .p12 file.  Hooray!
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/p12file.jpg" alt="p12 Signing Certificate"></p>
</div>
</ol>
<h2>Create an App ID</h2>
<p>Now that you have the precious file you need, it&#8217;s finally time to create your Mobile Provisioning Profile.  You&#8217;ll start by setting up a unique ID for the app you&#8217;re creating.</p>
<ol>
<li>Navigate back to the <b>Provisioning Portal</b> in your web browser to create an App ID for your app. <a href="http://developer.apple.com" title="Apple Developer Portal">Log in</a>, click on <b>Provisioning Portal</b>, and click on <b>App IDs</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/appIDs.jpg" alt="App IDs"></p>
</div>
<li>Click <b>New App ID</b>.
<p>Note: If you have not paid the developer fee, or if you are just a team member of the Team Admin, you will not see this option. Ask your Team Admin to complete this step.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/newAppID.jpg" alt="New App ID"></p>
</div>
<li>On the next screen you&#8217;ll start by writing a Description for your app, which can be anything you want. (This is what the player will see when he sees your app in the App Store. Obviously, there&#8217;s a whole marketing concern here that&#8217;s outside the scope of this tutorial.  Since we&#8217;re just building a test app, i recommend you punch SOME CRAZY NONSENSE in there for your description.  Take THAT, marketing.  -Ed)
<p>Within the <b>Bundle Seed ID (App ID Prefix)</b> you will have various options, depending on your account status. If you are the Team Admin, you&#8217;ll have a &#8220;Generate New&#8221; option. Team Members will only see a &#8220;Use Team ID&#8221; option.  (If this is not your first App, you could also choose the Bundle Seed IDs for previous project). Either way, leave it at the default.</p>
<p>The <b>Bundle Identifier</b> lets you create a specific package for your App. The standard way to make this package is to reverse the name of your website domain. I used com.sina.ipadtest. (Our team uses com.untoldentertainment.whatever &#8211; Ed.)</p>
<p>Alternately, you can use a generic Bundle Identifier by just putting an &#8220;*&#8221; in the field.</p>
<p><b>Note:</b> There have been cases where the asterisk does not work for some people. It is a safe bet you make a specific namespace like the examples above.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/appDescription.jpg" alt="App Description"></p>
</div>
<li>Click <b>Submit</b>.
<p>Voila &#8211; you have now created your App ID! On the next screen you will see all of the app profiles you&#8217;ve created. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/appIDResults.jpg" alt="App ID Results"></p>
</div>
</ol>
<h2>Create a Mobile Provisioning Profile</h2>
<p>Now that you have an <b>App ID</b>, you can now create your <b>Mobile Provisioning Profile</b>.  This is the crowning touch to everything we&#8217;ve done in this tutorial so far. The Mobile Provisioning Profile glues your developer(s), your Signing Certificate, your app ID and your testing device together.  Once you have this file, you&#8217;ll be able to test an app on your device.  </p>
<ol>
<li>Within the Provisioning Portal website, click <b>Provisioning</b> in the sidebar.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/provisioningPortal.jpg" alt="Apple Provisioning Portal"></p>
</div>
<li>Click on <b>New Profile.</b>
<p>Note: You only see this option if you are the fee-paying Team Admin.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/newProfile.jpg" alt="New Apple Provisioning Portal"></p>
</div>
<li>Give your provisioning profile a <b>Profile Name</b>.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/nameProfile.jpg" alt="Apple Name Your Provisioning Profile"></p>
</div>
<li>Choose the <b>Signing Certificate(s)</b> that you want to use with this profile.
<li>Use the drop-down list to indicate the app for which you&#8217;re creating this profile.
<li>Finally, indicate the testing device you want to test it on.
<li>Click Submit.
</ol>
<p>You&#8217;ll be taken to a screen that says your Mobile Provisioning Profile is pending. Just click your browser&#8217;s refresh button a couple of times until the <b>Download</b>button appears, and download your precious, hard-fought profile. Make sure to save it in a location you&#8217;re likely to remember.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_29/profilePending.jpg" alt="Apple Provisioning Profile"></p>
</div>
<h2>Are We There Yet?</h2>
<p>That was a big pile of hoops to jump through just to test an app on your device, and we haven&#8217;t even begun building the app yet!  Here&#8217;s what you just accomplished:</p>
<ol>
<li>You used the command line and OpenSSL to create a Certificate Signing Request file.
<li>You sent that file to Apple. Apple responded by enabling you to download your Signing Certificate file. This file identifies you as the developer of your app.
<li>You converted your Signing Certificate to the .p12 format.
<li>You created an ID for your app.
<li>You created your Mobile Provisioning Profile, the glue that holds all these elements together and enables you to test your app on an iOS device.
</ol>
<p>The good news is that you DO NOT need to repeat the steps to create your Signing Certificate for additional apps. Now that you have that file, you&#8217;ll use it to sign all of your iOS apps from here on in. Similarly, you won&#8217;t have to re-enter your developer ID or device ID for future projects (unless, of course, you gain a new team member or device). The App ID and Mobile Provisioning Profile are the two pieces that are unique to your app &#8211; you WILL need to repeat those steps for additional projects.  </p>
<p>The next part of this tutorial series will cover the process of creating your actual Adobe AIR app using FlashDevelop, bundling it up with this mess of certificates and profiles you just built, and deploying the app to your testing device.  </p>
<p><a href="http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 3)">Continue to Part 3</a></p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F11%2F30%2Fflash-to-ios-a-step-by-step-tutorial-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F11%2F30%2Fflash-to-ios-a-step-by-step-tutorial-part-2%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=4097&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Flash to iOS: A Step-by-Step Tutorial (Part 1)</title>
		<link>http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 15:33:33 +0000</pubDate>
		<dc:creator>Sina Kashanizadeh</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPod Touch]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=4087</guid>
		<description><![CDATA[This is the first part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch. Jump to other parts in the series: Part 2 &#8211; Obtain your [...]]]></description>
			<content:encoded><![CDATA[<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_12_07/iPadAIR.png" alt="Flash to iOS: A Step-by-Step Tutorial" /></p>
</div>
<p>This is the first part of our tutorial series by Intern Sina on creating an AIR application for free on a PC using FlashDevelop, and deploying it as a native app on an iOS device like the Apple iPad, iPhone or iPod Touch.</p>
<p>Jump to other parts in the series:</p>
<ul>
<li><a href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 2)"><b>Part 2</b> &#8211; Obtain your Signing Certificate &#038; Mobile Provisioning Profile, and create your App ID</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/07/flash-to-ios-a-step-by-step-tutorial-part-3-2/" title="Flash to iOS: A Step-by-Step Tutorial (Part 3)"><b>Part 3</b> -Use FlashDevelop to build your mobile AIR app</a>
<li><a href="http://www.untoldentertainment.com/blog/2011/12/14/flash-to-ios-a-step-by-step-tutorial-part-4/" title="Flash to iOS: A Step-by-Step Tutorial (Part 4)"><b>Part 4</b> &#8211; Modify your project settings and test your app on an Apple device</a>
</ul>
<h2>Dead Wrong</h2>
<p>There&#8217;s a lot of <a href="http://en.wikipedia.org/wiki/Fear,_uncertainty_and_doubt">FUD</a> floating around about Flash these days. Ever since Steve Jobs took to the mic and sounded the death knell of at least the <em>perception</em> of Flash, there&#8217;s been heaping gobs of misinformation about what you can and can&#8217;t, should and shouldn&#8217;t do with Flash. i chalk it up to the fact that death is news. It&#8217;s BIG news when Michael Jackson dies, BIG news when Steve Jobs dies, and equally big news if you can be among the first to report on the death of a technology or company &#8211; RIM, Flash, Palm, HP &#8211; take your pick. If it bleeds, it leads.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/tombstone.jpg" alt="Saying stuff is dead is dead" /></p>
<p>Saying stuff is dead, is dead. You heard it here first.</p>
</div>
<p>This hyperbolic and sensational misreporting is particularly damaging for those of us who have made our living developing with Flash and Actionscript. Back in 2000, when i first got into Flash, i chose it because the alternative was HTML. HTML appeared and performed <em>completely differently</em> depending on a number of different factors:</p>
<ol>
<li>The platform (Mac, PC or Linux)</li>
<li>The screen resolution (640&#215;480, 800&#215;600, 1024&#215;768, etc)</li>
<li>The browser (Netscape, Internet Explorer, Safari)</li>
<li>The browser <em>version</em> (HTML could render completely differently from IE6 to IE7)</li>
</ol>
<p>Meanwhile with Flash, i could build something inside the little Flash Player box, and it would look and behave reasonably identically across platforms, resolutions, browsers, and browser versions. (Subsequent versions of the Flash player complicated things a smidge, but we were still WORLDS away from the pain of HTML). What&#8217;s more, as a visual person, i could actually lay things out within the tool, instead of coding them abstractly and waiting to see how the browser would render them. If i wanted something to appear <em>over there</em>, i picked it up and put it <em>over there</em>. No futzing around with padding or align tags for hours.</p>
<p>The push towards HTML5 doesn&#8217;t scare me &#8211; more accurately, it makes me feel ill. It&#8217;s a step backwards. Without proper tooling, i see myself banging my head against the wall picturing absolutely everything in <em>codespace</em> (rather than concretely laid out in front of me in <em>designspace</em>), and programming all kinds of exception cases so that my content performs properly depending on platform, browser and version. You know what? No thanks.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/butterChurn.jpg" alt="Butter Churn" /></p>
<p>Thank God Flash is dead! Now we can finally move forward.</p>
</div>
<h2>Prying Flash from my Cold, Dead Hands</h2>
<p>As long as the tools for other technologies stink, and as long as i can keep making and monetizing projects in Flash, i&#8217;m going to stay the course until there&#8217;s a compelling technological /creative/workflow reason to make a jump. Untold Entertainment has deployed two games <em>written in Actionscript</em> (<strong><a title="Sissy's Magical Ponycorn Adventure on the Apple iPad" href="http://click.linksynergy.com/fs-bin/click?id=S5athXGL5Y4&amp;subid=&amp;offerid=146261.1&amp;type=10&amp;tmpid=3909&amp;RD_PARM1=http%3A%2F%2Fitunes.apple.com%2Fca%2Fapp%2Fsissys-magical-ponycorn-adventure%2Fid445696590%3Fmt%3D8">Sissy&#8217;s Magical Ponycorn Adventure</a></strong> and <strong><a title="Untold Entertainment's Heads on the BlackBerry App Store" href="https://appworld.blackberry.com/webstore/content/reviews/38777?lang=en">Heads</a></strong>) across two different mobile platforms (Apple iPad and the BlackBerry Playbook), and we&#8217;re just getting warmed up.</p>
<p>Here are the facts, at the time of this writing:</p>
<ol>
<li>No one really wants to play a game in a browser on a phone. Native apps are where it&#8217;s at.</li>
<li>Yes, you CAN put Flash- and Actionscript-created content on an iPhone, in native app format.</li>
<li>You can even do it without having to buy Flash Professional or Creative Suite.</li>
<li>You can develop entirely on a PC until the very last step (uploading content to the App Store).</li>
<li>Untold Entertainment is about to show you how.</li>
</ol>
<h2>What You&#8217;ll Need</h2>
<p>While buying Adobe&#8217;s tools is optional, you&#8217;ll still need to spend a bit of money (or mug the right people) to pull this off. Here&#8217;s what you&#8217;ll need:</p>
<ol>
<li>An Apple Developer account, which is $99 USD/year. Once you start down this path, you&#8217;re in it for the long haul &#8230; if you let your developer account lapse, they pull all your products off the store. For realsies.</li>
<li>A Mac (not free) running the latest version of Xcode (free).</li>
<li>An iOS device (optional, but obviously recommended).</li>
<li>FlashDevelop v4 or better (free, PC-only)</li>
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/mac.jpg" alt="Macs are not free" /></p>
<p>A Mac: not free.</p>
</div>
<h2>Introducing Sina</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/pop.jpg" alt="Sina looks like Prince of Persia" /></p>
<p>He&#8217;s the spitting image of this guy &#8211; i swear.</p>
</div>
<p><a href="http://twitter.com/#!/sinakash">Sina Kashanizadeh</a> is a Sheridan College computer programming student and an intern here at Untold Entertainment. He&#8217;s done all the legwork in this process, and has written this step-by-step guiding on porting your Actionscript projects to iOS. Take it away, Sina!</p>
<h2>Flash on iOS? So You Want to Be a Hero&#8230;</h2>
<p>With the mobile world expanding at a crazy rate you may have had the crazy idea to try making an App yourself and putting it on the iPhone. Well, you&#8217;re a brave soul, because it can be a confusing task. This is why I have scoured the internet and composed a step by step tutorial of this process. This tutorial would not have been possible without the fantastic people that maintain <a href="http://www.flashdevelop.org">FlashDevelop</a> and the wonderful community behind it. I would also like to thank <a href="http://www.gotoandlearn.com">Lee Brimelow</a> and <a href="http://seaships.in">Adobe</a> for putting out some great tutorial videos that helped me out a lot. Last but not least, I would like to thank <a href="http://www.CodeandVisual.com">CodeandVisual.com</a> for putting up a fantastic comprehensive guide that inspired me to move forward whenever I had trouble writing this tutorial.</p>
<p>I just want to be clear that this tutorial is not the &#8220;be-all, end-all&#8221; of this process. There are many different ways of transferring your Flash App to iOS, and the problems I cover are specific to my method. Also, the process I cover is PC-specific.</p>
<h2>Getting Started</h2>
<p>Before we begin, I would like to recommend a <a href="http://www.gotoandlearn.com/play.php?id=133">very good video resource</a> by Adobe Evangelist Lee Brimelow, in which he explains the basics of adding an app to the App Store when using Flash. If you do not know how this process works, I highly recommend this video, as it breaks down the somewhat painful process of adding an AIR App to the App Store when using a PC (thanks again, Lee!). The good news is that if you go through this once, you&#8217;ll understand the ins and outs of the process. You only have to do the most painful parts once.</p>
<p>The video covers a myriad of stuff but the basics boil down to this: Your end goal is to create an .ipa file, which is what you&#8217;ll upload to the App Store. To do this you will require:</p>
<ul>
<li>An App</li>
<li>An iOS developer ID from Apple ($)</li>
<li>An iPad or iPhone ($)</li>
<li>A Mac ($, but required for one step only &#8230; borrow one from a friend!)</li>
<li>A Signing Certificate</li>
<li>A Mobile Provisioning Profile</li>
<li>YOUR SOUL</li>
</ul>
<p>Sounds like fun? Well it isn’t, but let’s get cracking anyway!</p>
<h2>Any Club that would Have Me as a Member</h2>
<p>As I mentioned above, you&#8217;ll need a developer ID from Apple. Head over to the <a href="http://developer.apple.com/devcenter/ios/index.action">iOS Dev Center</a> and register for &#8220;free&#8221;.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/lie.jpg" alt="It ain't free" /></p>
<p>&#8220;Free&#8221; as in &#8220;Free Beer &#8230; that you have to pay for.&#8221;</p>
</div>
<p>Go through the steps of signing up and the email verification and you will acquire an Apple ID. Now, the unfortunate thing is that you get the Apple ID for free, but if you want to develop anything with it on a device &#8211; even a silly test app that you will never release &#8211; you will have to pay $99 a year. You&#8217;d better be positive that your <strong>Angry Birds</strong> clone will be worth at least a hundred bucks a year in revenue. You can sign up for a free student account, but again you can’t really do anything with it unless you drop some cash. If you are helping someone develop an App for the iPhone/iPad and they are paying the developer fee, they can add you as a partner to their project.</p>
<h2>Adding a Device to Your Developer Account</h2>
<p>If you want to test and ultimately launch this App, you will need an Apple device such as an iPhone, iPod Touch or iPad. You will also need to register your Apple device with your Apple ID. Follow these steps:</p>
<ol>
<li>Go into the iOS Developer website and log in with your new account info. Here, you will see the iOS Provisioning Portal. Click on it to see further options.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/provisioningPortal.jpg" alt="iOS Provisioning Portal" /></p>
</div>
</li>
<li>click Devices in the sidebar.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/devices.jpg" alt="iOS Devices menu" /></p>
</div>
</li>
<li>Click Add Devices.Note: if you are a partner developer with a team member profile, you will not be able to add a device. The option will only appear for the master account holder.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/addDevices.jpg" alt="iOS Add Devices" /></p>
</div>
</li>
<li>On this screen, you can add up to 100 devices to your account. Start off by adding your Device Name. Nothing fancy here &#8211; just enter something that will help you identify it &#8211; ie &#8220;Sina&#8217;s iPad&#8221;, or &#8220;iPhone what i stole from that guy that one time&#8221;.</li>
<li>Enter the Device ID (40 hex characters). This is the serial number that is associated with the device that you are trying to register. To find this Device ID, open iTunes while your device is connected. On the main screen, you will see all the information about your device, including a field called Serial Number. It doesn&#8217;t look clickable, but click on that field anyway and the number changes to a hex code. That&#8217;s your device ID.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_11_21/deviceID.jpg" alt="iOS Device ID" /></p>
</div>
</li>
<li>Once you&#8217;ve filled in those details, click Submit and the device will be added to your Developer account. Now you can build an Adobe AIR application and test it on your device.The next tutorial will take you through the process of requesting and creating a Signing Certificate, converting that certificate to the p12 format that Apple requires, and generating a Mobile Provisioning Profile for your new app.
<p><a title="Flash to iOS: A Step-by-Step Tutorial (Part 2)" href="http://www.untoldentertainment.com/blog/2011/11/30/flash-to-ios-a-step-by-step-tutorial-part-2/">Continue to Part 2</a></li>
</ol>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F11%2F23%2Fflash-to-ios-a-step-by-step-tutorial-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F11%2F23%2Fflash-to-ios-a-step-by-step-tutorial-part-1%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=4087&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/11/23/flash-to-ios-a-step-by-step-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Unity Game Development by Example gets an Update!</title>
		<link>http://www.untoldentertainment.com/blog/2011/10/12/unity-game-development-by-example-gets-an-update/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/10/12/unity-game-development-by-example-gets-an-update/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 14:49:11 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3976</guid>
		<description><![CDATA[Last week, Packt Publishing released the second edition of my seminal and important work, Unity 3.x Game Development by Example: A Beginner&#8217;s Guide, which helps fledgling game developers learn Unity through a serious of bad puns and dick jokes. What&#8217;s New in Version 2? Absolutely nothing! The Unity 3.x version is identical to the first [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, Packt Publishing released the second edition of my seminal and important work, <a href="http://www.amazon.com/gp/product/1849691843/ref=as_li_qf_sp_asin_tl?ie=UTF8&#038;tag=untoldentert-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399373&#038;creativeASIN=1849691843"><b>Unity 3.x Game Development by Example: A Beginner&#8217;s Guide</b></a><img src="http://www.assoc-amazon.com/e/ir?t=untoldentert-20&#038;l=as2&#038;o=1&#038;a=1849691843&#038;camp=217145&#038;creative=399373" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, which helps fledgling game developers learn Unity through a serious of bad puns and dick jokes.</p>
<div class="displayed">
<p><a href="http://www.amazon.com/gp/product/1849691843/ref=as_li_qf_sp_asin_tl?ie=UTF8&#038;tag=untoldentert-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399373&#038;creativeASIN=1849691843"><img src="http://www.untoldentertainment.com/blog/img/projects/unity3DByExample/unity3XByExample.jpg" alt="Unity 3.x Game Development by Example"></a><img src="http://www.assoc-amazon.com/e/ir?t=untoldentert-20&#038;l=as2&#038;o=1&#038;a=1849691843&#038;camp=217145&#038;creative=399373" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
</div>
<h2>What&#8217;s New in Version 2?</h2>
<p>Absolutely nothing!  The Unity 3.x version is identical to the first edition, save for the fancy new cover by UbiSoft Toronto game artist <a href="http://dancoxart.com/" title="Dan Cox Art">Dan Cox</a>.  The occasional errata from the first edition have been corrected, and the screenshots have been updated so that they match Unity&#8217;s subtle interface changes.  i&#8217;ve included two new spaceship models by Dan in Chapter 11 (the same ones you see on the cover), which i&#8217;ll make available to owners of all versions.</p>
<p>The first chapter was updated to discuss the BootCamp demo instead of the original Island Demo, which stopped working reliably as of version 3.  Then, just as we were ready to release the book, Unity launched v3.4, which included <em>yet another</em> demo and more subtle UI changes. So Unity 3.x Game Development by Example has been lovingly (?) re-written TWICE to bring it in-line with what those nutty funsters at Unity have been up to.</p>
<h2>Should i Buy It?</h2>
<p>Yes you should.</p>
<p>i made the rather bold claim that the first edition of the book may very well cure cancer.  i realize now that this was entirely irresponsible of me, and i regret my error in judgment.  i can say now with all certainty that version two is <em>guaranteed</em> to cure cancer, or your money back.  Please contact Pack Publishing for all refund inquiries, including a post mortem certificate from your next of kin.</p>
<h2>Praise for Unity 3D Game Development by Example (from actual people)</h2>
<blockquote><p>&#8220;A great resource for learning Unity.&#8221; &#8211; <a href="http://spyeart.com/">Michael Todd</a>, indie game developer</p></blockquote>
<blockquote><p>&#8220;Creighton&#8217;s book teaches both Unity and good design practices while staying hip, funny, and even controversial. A must read!&#8221; &#8211; <a href="http://www.linkedin.com/pub/ryon-levitt/4/544/534">Ryon Levitt</a>, Game Designer</p></blockquote>
<blockquote><p>&#8220;I went from unemployed to working full-time in California!&#8221; &#8211; <a href="http://mohdsahaf.blogspot.com/">Mohammed Al-Sahaf</a>, game school graduate</p></blockquote>
<blockquote><p>&#8220;I didn&#8217;t loose brain cells when I read this book.&#8221; &#8211; <a href="http://tutorialsbyibrent.com">Brent Arnold</a>, Mobile Developer</p></blockquote>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F10%2F12%2Funity-game-development-by-example-gets-an-update%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F10%2F12%2Funity-game-development-by-example-gets-an-update%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/10/12/unity-game-development-by-example-gets-an-update/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3976&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/10/12/unity-game-development-by-example-gets-an-update/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How Little Things Escalate</title>
		<link>http://www.untoldentertainment.com/blog/2011/08/22/how-little-things-escalate/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/08/22/how-little-things-escalate/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 14:09:44 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3910</guid>
		<description><![CDATA[&#8220;Escalation&#8221; is the term game developers use to describe the ways in which a video game gets progressively more challenging. There are many different ways to escalate a game. Here are a few: decrease the amount of time the player has to complete a goal at the climax of Super Metroid, the player has to [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Escalation&#8221; is the term game developers use to describe the ways in which a video game gets progressively more challenging.  There are many different ways to escalate a game.  Here are a few:</p>
<ol>
<li>decrease the amount of time the player has to complete a goal
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/superMetroid.png" alt="Super Metroid"></p>
<p>at the climax of <b>Super Metroid</b>, the player has to escape the ship before it explodes
</div>
<li>increase the speed or power of obstacles
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/pacMan.jpg" alt="Pac Man"></p>
<p>Pac Man&#8217;s ghost enemies speed up as the game escalates
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/bubbleBobble.jpg" alt="Bubble Bobble"></p>
<p>Bubble Bobble introduces the invincible Baron von Blubba when the player dawdles
</p></div>
<li>decrease the number of &#8220;chances&#8221; or &#8220;tries&#8221; the player has to complete a goal
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/donkeyKong.jpg" alt="Donkey Kong"></p>
<p>Donkey Kong gives the player a set number of tries (&#8220;lives&#8221;) as the game escalates before the machine requires another quarter and the player has to start from the beginning
</p></div>
<li>increase complexity
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/bejeweled.jpg" alt="Bejeweled"></p>
<p>as Bejewelled escalates, it introduces extra gem colours, which makes finding matches more challenging
</p></div>
<li>increase the grind &#8211; an action the player must perform repeatedly to be successful
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/granTurismo.jpg" alt="Gran Turismo"></p>
<p>Gran Turismo introduces endurance races where the player must race for a set number of real-time hours to win
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/worldOfWarcraft.jpg" alt="World of Warcraft"></p>
<p>World of Warcraft confronts the player with increasingly more difficult monsters, which forces the player to repeatedly kill weaker monsters to improve his stats
</p></div>
<li>decrease the amount of visual information available to the player
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/legendOfZelda.jpg" alt="The Legend of Zelda"></p>
<p>The Legend of Zelda periodically turns out the lights, and requires the player to doggedly re-ignite lamps
</p></div>
<li>increase the quota &#8211; the number of small goals the player must complete within the time limit.  This is really just a variation on decreasing the time limit.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/dinerDash.jpg" alt="Diner Dash"></p>
<p>As each level progesses, Diner Dash increases the number of customers you have to serve
</p></div>
</ol>
<h2>Lightning Bolt!  Lightning Bolt!</h2>
<p>i learned an interesting tip about escalation. i wish i could credit my source, but i have a memory like a sieve .  (Mother Nature escalates my life by increasingly turning me into a doddering old fool.)  The tip is to escalate your game with a lightning bolt graph, rather than a smooth incline.  Regard:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/graphs.png" alt="Game Escalation Graphs"></p>
</div>
<p>In an old-school arcade game, the levels get progressively more insane, until you lose the game and have to feed the machine more quarters.  This makes sense for a coin-op that exists to devour your hard-earned pocket change, but it doesn&#8217;t make sense if you&#8217;re designing a more modern game where you <em>want</em> your player to be engaged for as long as possible.  A freemium game, for example, lives and dies by retaining players; a linear escalation curve increases the likelihood that the player will quit out of fear or exhaustion.  If he just <em>barely</em> makes it out of level 10 by the skin of his teeth, he may not even <em>want</em> to attempt level 11.  He&#8217;d sooner quit while he&#8217;s ahead.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/ballmer.jpg" alt="Steve Ballmer"></p>
<p>Is that all you got??  BRING ON THE WATER TEMPLE!!
</p></div>
<p>A modern game that strives for retention might use a lightning bolt or stair-step graph.  Here, level 10 was insanely difficult, but level 11 gives the player a bit of a breather.  One of the early examples of this approach that i can remember is the coin-op beat-em-up <b>Final Fight</b>.  After brawling your way through a preposterously crime-ridden city as the roid-raging mayor or one of his two ninja buddies, you get to smash a car.  No bad guys, no chance of death, no risk of losing &#8211; just a time-limited opportunity to smash a car.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/carSmash.jpg" alt="Final Fight Car Smash"></p>
<p>Because nothing says &#8220;tough on crime&#8221; like the mayor engaging in a little senseless vandalism.
</p></div>
<p>A more modern example is <b>Little Things</b>, a hidden object game on the iPad that i&#8217;ve been playing this week.  The levels in Little Things are, refreshingly, NOT rooms in a Victorian mansion in which a child ghost has murdered a wealthy detective.  Instead, they&#8217;re little pictures composed of even smaller pictures.  The player has to complete his quota by finding the items on the list.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/littleThings.jpg" alt="Little Things"></p>
<p>i was trying to keep this article classy, but they had to go mentioning shuttlecocks.
</p></div>
<p>Some of these pictures make it easier to find the little things within.  The umbrella&#8217;s striped design makes it easier to compartmentalize sections of the image and limit your focus.  In contrast, the hideously jam-packed cupcake is a real challenge to sift through.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/beagle.jpg" alt="Little Things Beagle"></p>
<p>Little Things beagle: easy. Little things hamburger: not-so-easy. (This is somewhat of a reversal of real life.)
</p></div>
<p>If Little Things were to escalate linearly, you&#8217;d unlock the easy pictures at the beginning of the game, and the denser pictures towards the end.  But Little Things has a staggered escalation; the densely-populated hamburger is introduced before the cinchy electric guitar.  If the escalation was linear, you&#8217;d complete 10 missions in the beagle picture before moving on to complete 10 further missions in the wheelbarrow picture.  Instead, Little Things bounces around from picture to picture, shuffling in the easier images to give the player a break.  Even the missions themselves are staggered: in one instance, you&#8217;ll find yourself scouring the level for 15 objects under distinct time pressure; the very next level, you&#8217;ll need to point at four pairs of sunglasses in one of the easier pictures.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_08_22/waldo.jpg" alt="Where's Waldo"></p>
<p>Serously &#8211; there he is.  He&#8217;s RIGHT THERE.  Just click on him.  50 points.
</p></div>
<p>The net effect is that Little Things held my attention much longer than it would have if its escalation were progressive.  Give consideration to staggered escalation if you count player retention among your next game&#8217;s goals.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F08%2F22%2Fhow-little-things-escalate%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F08%2F22%2Fhow-little-things-escalate%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/08/22/how-little-things-escalate/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3910&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/08/22/how-little-things-escalate/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to Convert Artwork from Flash to Illustrator</title>
		<link>http://www.untoldentertainment.com/blog/2011/01/26/how-to-convert-artwork-from-flash-to-illustrator/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/01/26/how-to-convert-artwork-from-flash-to-illustrator/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 15:42:59 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3385</guid>
		<description><![CDATA[A client recently asked me if i could supply him with artwork from a game so that another team could produce a video. The video team, of course, couldn&#8217;t/wouldn&#8217;t do anything with the native .fla (Flash) files i had on hand. They wanted Illustrator files. It took a lot of back and forth and futzing [...]]]></description>
			<content:encoded><![CDATA[<p>A client recently asked me if i could supply him with artwork from a game so that another team could produce a video.  The video team, of course, couldn&#8217;t/wouldn&#8217;t do anything with the native .fla (Flash) files i had on hand.  They wanted Illustrator files.</p>
<p>It took a lot of back and forth and futzing to finally convert the artwork properly, and when i finally found the solution, i realized i had known it all along and had merely forgotten it (because i don&#8217;t do this type of thing often).  So i&#8217;m recording the solution here for posterity; the next time i have to do this, a Google search will turn up my own blog :)  It actually wouldn&#8217;t be the first time that&#8217;s happened.</p>
<p>i&#8217;ll keep you on tenterhooks and take you through the frustrating conversion process, just so that you can swear along with me and nod your head and say &#8220;yep &#8211; that&#8217;s happened to me.&#8221;</p>
<h2>Export Image</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/characters1.jpg" alt="Characters"></p>
<p>These are the Flash vector characters the client needed me to convert.
</p></div>
<p>The ability to export artwork to .ai format has actually been removed from Flash Professional as of version CS5, and has been replaced with the Adobe .fxg format, which works as expected. The .fxg format even converts gradients properly, which have been a longstanding asspain for the past 10 years.  If you&#8217;re running CS5 or better, export to .fxg and you&#8217;re done.  If you&#8217;re trapped in CS4 or below, here&#8217;s what you may have tried:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/export.jpg" alt="Export Image">
</div>
<ol>
<li>File > Export > Export Image
<li>Choose Adobe Illustrator (*.ai) from the drop-down filetype list
<li>Punch in a filename, and click &#8220;Save&#8221;
<li>Open the file in Illustrator
</ol>
<p>The first roadblock you encounter may be this message:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/error.jpg" alt="Can't open the illustration.">
</div>
<blockquote><p>Can&#8217;t open the illustration.  The illustration contains an incomplete or garbed object description.</p>
<p>Offending operator: &#8220;&#8221;</p>
<p>Adobe_Illustrator_AI5 / initialize get exec<br />
%%BeginEncoding: _pixelmic &#8230; feh.  i can&#8217;t be arsed to look up the accented characters to type the rest.</p></blockquote>
<p>i&#8217;ve ranted at length before about the error messages eggheads put into their software products.  How on Earth is joe average user &#8211; and an <em>artist</em>, at that &#8211; supposed to react to and troubleshoot a message like this?</p>
<p>Obviously (?), all the egghead wanted the artist to do was this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/characters2.jpg" alt="Characters">
</div>
<p>Select the single movieclip and press CTRL+B to break apart the object into separate objects.  Now, when i go through the export process and bring the file into Illustrator, i get this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/desaturated.jpg" alt="Characters"></p>
<p>Um &#8230; yuck?  Who sucked all the color out?
</p></div>
<p>The reason why the colors are desaturated and grey-looking apparently has to do with the Flash .ai export routine deciding to convert RGB artwork to CMYK artwork.  i don&#8217;t know why it does this, but it&#8217;s not helpful.  But at least the artwork is in Illustrator in vector format, right?</p>
<p>At this point, you <em>could</em> pick through every piece of vector art and tweak the color by hand &#8230; if you&#8217;re a sucker.  Instead, let&#8217;s get on to the reason you&#8217;re here: the actual solution to this non-working nonsense.</p>
<h2>WMF FTW!</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/wmf.jpg" alt="WMF"></p>
<p>The mighty (and seldom-understood) wmf
</p></div>
<p>The secret is to export as a Windows Metafile.  Mac people, i don&#8217;t know if you have this option.  If not, i&#8217;m not sure how to help you &#8230; but once you counter-intuitively export as a .wmf file instead of an .ai file and bring THAT into Illustrator, voila: your artwork is beautifully preserved with its RGB colors intact.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/characters3.jpg" alt="Characters"></p>
</div>
<p>As i mentioned, the one drawback is that gradients don&#8217;t survive the crossing.  This character&#8217;s cell phone casts an alpha gradient glow across her face, which shows up as a flat white polygon in Illustrator:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_01_26/gradient.jpg" alt="Gradient">
</div>
<p>.fxg format will fix that. If you&#8217;re not ready to upgrade your software, this might entail an ugly hand-conversion process of all your gradients &#8230; unless someone else has discovered the esoteric conversion secret to preserving gradients?  Anyone?</p>
<p>Big thanks to Gavin Friesen, my Art Director back at Corus, for originally cluing me in to this solution.</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F01%2F26%2Fhow-to-convert-artwork-from-flash-to-illustrator%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F01%2F26%2Fhow-to-convert-artwork-from-flash-to-illustrator%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/01/26/how-to-convert-artwork-from-flash-to-illustrator/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3385&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/01/26/how-to-convert-artwork-from-flash-to-illustrator/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Unity3D iOS Porting Tips</title>
		<link>http://www.untoldentertainment.com/blog/2011/01/03/unity3d-ios-porting-tips/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/01/03/unity3d-ios-porting-tips/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 15:40:29 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3360</guid>
		<description><![CDATA[Indie game developer Itay Keren of Rope Racket shares a number of tips for porting Unity 3D projects to iOS: Unity3D iOS Porting Tips The article contains a pointers on trimming your Unity pc project to succeed under the more restrictive memory and processor constraints of the iPod/iPad/iPhonograph.]]></description>
			<content:encoded><![CDATA[<p>Indie game developer Itay Keren of <b>Rope Racket</b> shares a number of tips for porting Unity 3D projects to iOS:</p>
<p><a href="http://roperacket.tumblr.com/post/2544450508/unity-ios-porting">Unity3D iOS Porting Tips</a></p>
<p>The article contains a pointers on trimming your Unity pc project to succeed under the more restrictive memory and processor constraints of the iPod/iPad/iPhonograph.</p>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F01%2F03%2Funity3d-ios-porting-tips%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F01%2F03%2Funity3d-ios-porting-tips%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2011/01/03/unity3d-ios-porting-tips/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3360&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/01/03/unity3d-ios-porting-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically Add a GameObject to the Scene in Unity 3D</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/23/dynamically-add-a-gameobject-to-the-scene-in-unity-3d/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/23/dynamically-add-a-gameobject-to-the-scene-in-unity-3d/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 22:43:07 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3335</guid>
		<description><![CDATA[If you work a lot in Flash, you&#8217;re used to the process of adding a linkage name to a MovieClip in the library, and then using the new keyword and addChild method to dynamically pull the clip out of the library and display it on the stage. You can do something very similar in Unity [...]]]></description>
			<content:encoded><![CDATA[<p>If you work a lot in Flash, you&#8217;re used to the process of adding a linkage name to a MovieClip in the library, and then using the <b>new</b> keyword and <b>addChild</b> method to dynamically pull the clip out of the library and display it on the stage.</p>
<p>You can do something very similar in Unity 3D, but the process is slightly different.  Be sure to read our <a href="http://www.untoldentertainment.com/blog/2010/12/21/flash-to-unity-3d-glossary/">Flash to Unity 3D Glossary</a> if you&#8217;re coming over to Unity 3D from Flash.</p>
<h2>1. Create the GameObject</h2>
<p>We&#8217;ll make it real simple.  Click GameObject > Create Other > Cube in the menus to add a cube primitive to the Scene.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/createCube.jpg" alt="Create a Cube Primitive in Unity 3D"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/cube.jpg" alt="Cube Primitive in Unity 3D"></p>
<p>A cube appears somewhere in your Scene&#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/hierarchy.jpg" alt="Cube Primitive in the Hierarchy panel in Unity 3D"></p>
<p>&#8230; and the Cube GameObject is listed in the Hierarchy.
</p></div>
<h2>2. Create an Empty Prefab</h2>
<p>A Prefab is a lot like a Flash MovieClip.  It&#8217;s going to contain our Cube GameObject.  To create the Prefab, click the Create button in the Project panel, and choose Prefab from the resulting menu.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/createButton.jpg" alt="Unity 3D Project Panel Create Button"></p>
<p>Click here &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/prefabMenu.jpg" alt="Unity 3D Project Panel Prefab Menu"></p>
<p>and then here &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/emptyPrefab.jpg" alt="Unity 3D Project Panel Empty Prefab"></p>
<p>&#8230; to get this.
</p></div>
<p>Right/alternate-click on the Prefab, or click and hold (Mac)/press F2 (PC) to rename the Prefab.  I called mine &#8220;Cube&#8221;.  This won&#8217;t create a naming conflict or anything.</p>
<h2>3. Click/drag the Cube into the Prefab</h2>
<p>Click and drag the Cube GameObject from the Hierarchy panel into the empty grey Prefab you just created in the Project panel.  The Prefab lights up blue to indicate that it&#8217;s got junk in the trunk.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/bluePrefab.jpg" alt="Unity 3D Project Panel Full Prefab"></p>
</div>
<p>It&#8217;s now safe to delete the Cube GameObject from the Scene (via the Hierarchy panel) by highlighting it and pressing Delete (PC) or CMD+Delete (Mac).  The Cube is safe and sound inside the Prefab.</p>
<h2>4. Create a new Javascript</h2>
<p>Our Cube Prefab is ready to be instantiated (instantiate == make an instance [copy] of).  Let&#8217;s create a javascript to do the instantiating.  In the Project panel, click Create > Javascript to make a new script.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/createButton.jpg" alt="Unity 3D Project Panel Create Button"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/newJavascript.jpg" alt="Unity 3D Project Panel New Javascript"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/newBehaviourScript.jpg" alt="Unity 3D Project Panel New Javascript"></p>
</div>
<p>Name the javascript something sensible.  <b>Then, click/drag the script onto the Main Camera.</b>  (We do this because our script won&#8217;t execute unless it&#8217;s hooked up to a GameObject in the Scene.)</p>
<h2>5. Store a Reference to the Prefab</h2>
<p>This is the fanciest/weirdest thing we&#8217;ll do in the entire tutorial.</p>
<p>Double-click the script to open it up in Unitron or Uniscite (depending on whether you have a Mac or a PC).</p>
<p>Declare a variable at the top of the script called myCube:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myCube <span style="color: #339933;">:</span> GameObject<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Save the script and close the editor.</p>
<p>In order to store a reference to the cube Prefab, we need to click and drag it into the variable we just created.</p>
<p>In the Hierarchy panel, click on the Main Camera, which is the GameObject to which we attached our script.</p>
<p>In the Inspector panel, look for the script component.  You&#8217;ll see the myCube variable we created, and next to it is a piece of text that says None (Game Object)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/none.jpg" alt="Unity 3D Variable"></p>
<p>What in the Hells?
</p></div>
<p>myCube is the name of the variable (bucket), and the None (Game Object) is where the value goes.</p>
<p>Now, you need to <em>click and drag</em> the Cube Prefab from the Project panel into that value area that i&#8217;ve highlighted. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/clickDrag.jpg" alt="Unity 3D Click n' Drag"></p>
</div>
<p>This is the result:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/result.jpg" alt="Unity 3D Variable"></p>
<p>This is going to take some getting used to.
</p></div>
<p>It&#8217;s weird.  i know.</p>
<p><b>NOTE:</b> You called the variable &#8220;myCube&#8221;, but it gets displayed in the IDE as &#8220;My Cube&#8221;.  Why is that?  Chalk it up to a Unity idiosyncrasy that i&#8217;m not a huge fan of.  Check the script.  Your variable is still called &#8220;myCube&#8221; where it counts. </p>
<h2>6. Instantiate the Prefab</h2>
<p>Now, with a reference to that Prefab, you&#8217;re all ready to go.  Double-click to edit your script again, and punch in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myCube <span style="color: #339933;">:</span> GameObject<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> pos <span style="color: #339933;">:</span> Vector3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this is where the Cube will appear when it's instantiated</span>
     <span style="color: #003366; font-weight: bold;">var</span> rot <span style="color: #339933;">:</span> Quaternion <span style="color: #339933;">=</span> Quaternion.<span style="color: #660066;">identity</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Quaternion.identity essentially means &quot;no rotation&quot;</span>
     Instantiate<span style="color: #009900;">&#40;</span>myCube<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> rot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The Instantiate command takes a GameObject, a Vector3 for position and a Quaternion for rotation.</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Gettin&#8217; Fancy</h2>
<p>If you&#8217;ve read our article <a href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/">Understanding Loops with Arrays</a>, you might have a notion of how to use Loops and Arrays to instantiate an armada of Prefabs into your scene.  Here&#8217;s some sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myCube <span style="color: #339933;">:</span> GameObject<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// Store a bunch of different positions in an Array:</span>
     <span style="color: #003366; font-weight: bold;">var</span> aPositions <span style="color: #339933;">:</span> Array <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">new</span> Vector3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                      <span style="color: #003366; font-weight: bold;">new</span> Vector3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                      <span style="color: #003366; font-weight: bold;">new</span> Vector3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                      <span style="color: #003366; font-weight: bold;">new</span> Vector3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                      <span style="color: #003366; font-weight: bold;">new</span> Vector3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #003366; font-weight: bold;">var</span> rot <span style="color: #339933;">:</span> Quaternion <span style="color: #339933;">=</span> Quaternion.<span style="color: #660066;">identity</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #006600; font-style: italic;">// Loop as many times as there are elements in the aPositions Array:</span>
     <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>aPositions.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          Instantiate<span style="color: #009900;">&#40;</span>myCube<span style="color: #339933;">,</span> aPositions<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> rot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Put a new cube on the screen using the i'th position   </span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/armada.jpg" alt="Unity 3D Instantiation"></p>
<p>Here&#8217;s what it looks like.
</p></div>
<h2>One More for the Road</h2>
<p>Just for kicks, here&#8217;s a script that&#8217;ll throw the cubes on the screen any old place, using Random.Range():</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myCube <span style="color: #339933;">:</span> GameObject<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// Let's store some minimum and maximum possible x,y and z values</span>
     <span style="color: #006600; font-style: italic;">// to position our cubes:</span>
     <span style="color: #003366; font-weight: bold;">var</span> minX <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> maxX <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> minY <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> maxY <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> minZ <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> maxZ <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
     <span style="color: #006600; font-style: italic;">// (you can fiddle with these numbers to change the range of possible spawn positions)</span>
&nbsp;
     <span style="color: #003366; font-weight: bold;">var</span> totalCubes <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Change this to whatever number of cubes you'd like to have on-screen</span>
&nbsp;
     <span style="color: #003366; font-weight: bold;">var</span> rot <span style="color: #339933;">:</span> Quaternion <span style="color: #339933;">=</span> Quaternion.<span style="color: #660066;">identity</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>totalCubes<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">// Use Random.Range to grab randomized x,y and z values within our min/max ranges:</span>
          <span style="color: #003366; font-weight: bold;">var</span> randomX <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> Random.<span style="color: #660066;">Range</span><span style="color: #009900;">&#40;</span>minX<span style="color: #339933;">,</span> maxX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> randomY <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> Random.<span style="color: #660066;">Range</span><span style="color: #009900;">&#40;</span>minY<span style="color: #339933;">,</span> maxY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> randomZ <span style="color: #339933;">:</span> int <span style="color: #339933;">=</span> Random.<span style="color: #660066;">Range</span><span style="color: #009900;">&#40;</span>minZ<span style="color: #339933;">,</span> maxZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #003366; font-weight: bold;">var</span> pos <span style="color: #339933;">:</span> Vector3 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Vector3<span style="color: #009900;">&#40;</span>randomX<span style="color: #339933;">,</span> randomY<span style="color: #339933;">,</span> randomZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          Instantiate<span style="color: #009900;">&#40;</span>myCube<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> rot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Put a new cube on the screen using a randomized position   </span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_23/randomCubes.jpg" alt="Unity 3D Random Instantiation"></p>
<p>Awesomazing.
</p></div>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F23%2Fdynamically-add-a-gameobject-to-the-scene-in-unity-3d%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F23%2Fdynamically-add-a-gameobject-to-the-scene-in-unity-3d%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/23/dynamically-add-a-gameobject-to-the-scene-in-unity-3d/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3335&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/23/dynamically-add-a-gameobject-to-the-scene-in-unity-3d/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Make Something Clickable in Unity 3D</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/22/make-something-clickable-in-unity-3d/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/22/make-something-clickable-in-unity-3d/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 05:20:51 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3332</guid>
		<description><![CDATA[Making a GameObject clickable in Unity 3D is a piece of cake! Just write an OnMouseDown function in a script, and stick it to your GameObject. Voila &#8211; instant interactivity! // Unity javascript function OnMouseDown&#40;&#41; &#123; // Do something &#125;]]></description>
			<content:encoded><![CDATA[<p>Making a GameObject clickable in Unity 3D is a piece of cake!  Just write an OnMouseDown function in a script, and stick it to your GameObject.  Voila &#8211; instant interactivity!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Unity javascript</span>
<span style="color: #003366; font-weight: bold;">function</span> OnMouseDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F22%2Fmake-something-clickable-in-unity-3d%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F22%2Fmake-something-clickable-in-unity-3d%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/22/make-something-clickable-in-unity-3d/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3332&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/22/make-something-clickable-in-unity-3d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turn Something Invisible in Unity 3D</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/22/turn-something-invisible-in-unity-3d/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/22/turn-something-invisible-in-unity-3d/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 05:17:03 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3329</guid>
		<description><![CDATA[thing.renderer.enabled = true; // or false &#8230; where &#8220;thing&#8221; is a reference to a GameObject. If you&#8217;re doing this in a script that&#8217;s attached to the GameObject you want to hide, try: gameObject.renderer.enabled = false; Note that the GameObject&#8217;s collider is still active, so the GameObject can still be clicked on, run into, etc. Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">thing.<span style="color: #660066;">renderer</span>.<span style="color: #660066;">enabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// or false</span></pre></div></div>

<p>&#8230; where &#8220;thing&#8221; is a reference to a GameObject.</p>
<p>If you&#8217;re doing this in a script that&#8217;s attached to the GameObject you want to hide, try:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">gameObject.<span style="color: #660066;">renderer</span>.<span style="color: #660066;">enabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that the GameObject&#8217;s collider is still active, so the GameObject can still be clicked on, run into, etc.  Here&#8217;s a fast way to make the GameObject&#8217;s collider inactive:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">gameObject.<span style="color: #660066;">collider</span>.<span style="color: #660066;">isTrigger</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>To disable the GameObject&#8217;s clickability, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">gameObject.<span style="color: #660066;">active</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F22%2Fturn-something-invisible-in-unity-3d%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F22%2Fturn-something-invisible-in-unity-3d%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/22/turn-something-invisible-in-unity-3d/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3329&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/22/turn-something-invisible-in-unity-3d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash to Unity 3D Glossary</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/21/flash-to-unity-3d-glossary/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/21/flash-to-unity-3d-glossary/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 04:54:47 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3321</guid>
		<description><![CDATA[If you&#8217;re thinking of transitioning from Flash to Unity 3D, you&#8217;re in luck: the two programs are very similar, and it shouldn&#8217;t take you very long to feel right at home. The biggest difference that i can see between the two is that Unity is a proper game engine, while Flash is a program that [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re thinking of transitioning from Flash to Unity 3D, you&#8217;re in luck: the two programs are very similar, and it shouldn&#8217;t take you very long to feel right at home.  The biggest difference that i can see between the two is that Unity is a proper game engine, while Flash is a program that you bully, beg and cajole into doing game-like things.  Want collisions, 3D depth-swapping, particle systems, physics, shaders, and a whole lot more right out of the box (instead of implementing Box 2D, Away 3D, Collision Detection Kit, and God knows what else)?  Unity is a real treat!</p>
<p>Here are a few glossary terms that kinda sorta carry over from Flash to Unity: </p>
<p>MovieClip == GameObject (but more accurately, Symbol == Prefab.  Much like a MovieClip/Graphic/Button in Flash, whatever changes you make to a Prefab in Unity will replicate to all instances of that Prefab)</p>
<p>Scene == Scene (but Scenes in Flash are generally useless and glitchy &#8230; Scenes in Unity are the bee&#8217;s knees)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/scenesUnity.jpg" alt="Unity 3D Scenes"></p>
<p>Unity.
</p></div>
<p>Library == Project panel</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/libraryFlash.jpg" alt="Flash Library"></p>
<p>Flash.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/projectPanelUnity.jpg" alt="Unity 3D Project Panel"></p>
<p>Unity.
</p></div>
<p>Properties panel == Inspector panel</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/propertiesFlash.jpg" alt="Properties panel in Flash"></p>
<p>Flash.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/inspectorUnity.jpg" alt="Inspector panel in Unity"></p>
<p>Unity.
</p></div>
<p>Timeline == A-NOTHING!!  (except that there IS a timeline that allows you to animate GameObjects.  The resulting animation is stored in an .anim file inside your Project panel.  You can apply an animation to any GameObject in your Scene, so animations are curiously reusable)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/timelineFlash.jpg" alt="Timeline in Flash"></p>
<p>Flash.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/timelineUnity.jpg" alt="Timeline in Unity 3D"></p>
<p>Unity. (warning: the UX [user experience] on this thing kind of blows ass.)
</p></div>
<p>Instance names == tags &#8230; sorta (but unlike in Flash, you can have multiple GameObject instances with the same tag)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/instanceNamesFlash.jpg"></p>
<p>Flash.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/tagsUnity.jpg"></p>
<p>Unity.
</p></div>
<p>Layers == layers (but Unity does not display its layers in anything nearly as nice as the Flash timeline)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/layersFlash.jpg"></p>
<p>Flash.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/layersUnity.jpg"></p>
<p>Unity.
</p></div>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F21%2Fflash-to-unity-3d-glossary%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F21%2Fflash-to-unity-3d-glossary%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/21/flash-to-unity-3d-glossary/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3321&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/21/flash-to-unity-3d-glossary/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Start a Game Project in Unity 3D</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/21/how-to-start-a-game-project-in-unity/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/21/how-to-start-a-game-project-in-unity/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 04:18:24 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3312</guid>
		<description><![CDATA[By default, a new Unity install opens with a demo project. Unity 2.6 had an island, while Unity 3.0 has a G.I. Joe-style first-person shooter game. Follow these steps to start your own game project from scratch in Unity; 1. Start a New Project Click File > New Project &#8230; Every Unity sits in its [...]]]></description>
			<content:encoded><![CDATA[<p>By default, a new Unity install opens with a demo project.  Unity 2.6 had an island, while Unity 3.0 has a G.I. Joe-style first-person shooter game.  Follow these steps to start your own game project from scratch in Unity;</p>
<h2>1. Start a New Project</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/newProject.jpg" alt="New project in Unity"></p>
<p>Click File > New Project &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/projectWizard.jpg" alt="Project Wizard in Unity"></p>
</div>
<p>Every Unity sits in its own folder.  In this dialogue, browse to or create a new folder where your project will live, and then click the Create button.</p>
<p>What you&#8217;re left with is this:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/blankUnityProject.jpg" alt="Blank Project in Unity"></p>
<p>Ok &#8211; where&#8217;s the &#8220;Make Game&#8221; button at?
</p></div>
<p>Here&#8217;s what&#8217;s going on behind the scenes:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/projectFolder.jpg" alt="Unity Project Folder"></p>
</div>
<p>Unity automatically creates three sub-folders in your Project folder.  You won&#8217;t have to touch these folders &#8211; in fact, you really shouldn&#8217;t, because there&#8217;s metadata in there that might get botched if you start poking around.  Most of the time, you can import assets directly through the Unity IDE (program).</p>
<h2>2. Make a Scene</h2>
<p>Unity Projects are comprised of Scenes.  In Flash, it wasn&#8217;t really a good idea to use the Scenes, but in Unity, it&#8217;s darn near a requirement.  What you&#8217;re looking at now is actually an unnamed Scenes.  Scenes must be saved individually &#8230; whenever you try to leave one Scene to look at another, you&#8217;ll get a Save Confirmation prompt.</p>
<p>The smartest thing to do right off the bat is to save this Scene.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/saveSceneAs.jpg" alt="Unity Save Scene As"></p>
<p>Go to File > Save Scene as &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/sceneName.jpg" alt="Unity Scene Name"></p>
</div>
<p>Choose a name for your Scene.  Some really simple examples are &#8220;Title&#8221;, &#8220;Credits&#8221;, &#8220;Win&#8221;, &#8220;Lose&#8221;, and &#8220;Game&#8221; for an extremely basic project.  i&#8217;m calling this one &#8220;Game&#8221;, which is where all my main game action will take place.</p>
<p>Once you hit &#8220;Save&#8221;, two wonderful things will happen:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/titleBar.jpg" alt="Unity Title Bar"></p>
<p>The name of your new Scene is reflected in the Title Bar of your project &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/projectPanel.jpg" alt="Unity Project Panel"></p>
<p>.. and a new Scene shows up in your Project panel.
</p></div>
<p>The Project panel is where all your <em>stuff</em> goes.  It&#8217;s very much like the Library in Flash.  Scenes have a little Unity 3D icon next to them.  </p>
<h2>Put Your Scene in a Folder</h2>
<p>The final thing you&#8217;ll want to do is to create a folder for your Scenes, and dump your Scene inside it. </p>
<p>At the top of the Project panel, click the Create button and choose &#8220;Folder&#8221;:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/createButton.jpg" alt="Unity Project Panel Create Button"></p>
<p>This button sees a lot of use in Unity.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/createFolder.jpg" alt="Unity Project Panel Create Folder"></p>
<p>While you&#8217;re here, check out all the other cook junk you can build.
</p></div>
<p>You can also right click or alternate click on the Project panel to bring up this menu.  Mac users can probably splat-click or some nonsense.</p>
<p>And then:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/projectPanelScenesFolder.jpg" alt="Unity Project Panel Scenes Folder"></p>
<p>Rename the folder &#8220;Scenes&#8221; by right/alternate-clicking, slow double-clicking (Mac) or pressing F2 (PC)
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_21/sceneInFolder.jpg" alt="Unity Project Panel Game Scene in Folder"></p>
<p>Drag and drop the Game Scene inside the folder. Click the grey arrow to see what&#8217;s inside the folder.
</p></div>
<h2>What Now?</h2>
<p><b>How do i learn Unity 3D?</b></p>
<p>To learn more about developing games for Unity 3d and to learn the basic controls for getting around the program, read the first chapter of my book <a href="http://www.packtpub.com/article/introduction-to-game-development-using-unity-3d">Unity 3D Game Development by Example</a>.  Next, buy four copies of the book (in case you accidentally drop three of them in the mud).</p>
<p><b>How do i build a timer in Unity?</b></p>
<p>When you&#8217;re a little further along, read this sample of Chapter 7 of my book, <a href="http://www.packtpub.com/article/unity-3d-game-development-dont-be-clock-blocker">Don&#8217;t be a Clock Blocker</a>.    That&#8217;s how.</p>
<p><b>How do i program in Unity?</b></p>
<p>If you need a little help with programming in Unity using javascript, read our <a href="http://www.untoldentertainment.com/blog/unity-nuub/">Understanding Programming in Unity Javascript</a> series.</p>
<p><b>How do i transition from Flash to Unity?</b></p>
<p>Check out our article on Five Common AS3 to UnityScript Translations.</p>
<p><b>Alright &#8211; what else you got?</b></p>
<p>Like our style?  For more free Unity 3D tutorials, sign up for our <a href="http://www.untoldentertainment.com/blog/unity-3d-game-development-by-example/"><b>Unity 3D Game Development by Example</b> newsletter</a>, and we&#8217;ll send out a link whenever we post a new tutorial or article on Unity 3D.</p>
<p>Go forth and game!</p>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F21%2Fhow-to-start-a-game-project-in-unity%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F21%2Fhow-to-start-a-game-project-in-unity%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/21/how-to-start-a-game-project-in-unity/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3312&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/21/how-to-start-a-game-project-in-unity/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Understanding Functions &#8211; Return Values</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/03/understanding-functions-return-values/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/03/understanding-functions-return-values/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 15:27:20 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3259</guid>
		<description><![CDATA[There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Functions are one of them. In Understanding Functions, we learned that Functions are a way to bundle code statements together into a tight little unit that we can invoke (or “call”) any time [...]]]></description>
			<content:encoded><![CDATA[<p>There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Functions are one of them.</p>
<p>In <a href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-functions/">Understanding Functions</a>, we learned that Functions are a way to bundle code statements together into a tight little unit that we can invoke (or “call”) any time we like, and as often as we like. There are two very important features to learn about Functions: <a href="http://www.untoldentertainment.com/blog/2010/12/02/understanding-functions-arguments/">Arguments</a> and Return Values. We’ll figure out Return Values in this article.</p>
<h2>One Fanta, Please</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/vending.jpg" alt="Vending Machine">
</div>
<p>We talked about how Functions that accept arguments require exact change. If a Function asks for a String, you have to pass (&#8220;pay&#8221;) it a String.  If it wants an int, you must pass it an int.  If it wants two Arrays, a String, and four ints, you have to pass it those seven datatypes in that exact order.  If you pass the right datatypes in the wrong order, if you pass the wrong datatypes altogether, or if you pass the wrong number of arguments, you&#8217;ll throw a (possibly cryptic) error.</p>
<p>So let&#8217;s imagine that Functions are kinda like vending machines.  They require exact change, even specifying the <em>types</em> of coins you need to pay (ie five nickels, NOT one quarter).  But what do we get for all our trouble?  A kick in the pants and we&#8217;re sent on our way, that&#8217;s what.  Wouldn&#8217;t it be great if Functions could actually <em>give back</em> to society, instead of being a drain on our charitable resources?</p>
<p>Thanks to <b>return values</b>, they can.  A return value is the can of Coke that the Function vending machine spits out when we plug in our coins.   </p>
<p>Just as we type (specify a datatype for) our variables, we can also type our Functions.  When we do that, we&#8217;re saying &#8220;this is the type of thing the Function is going to return (give back/pay out/cough up).  Here&#8217;s how we write a Function that returns a String:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> giveMeGoodNewsOnly<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;good news&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>There are a few moving parts involved here.  Let&#8217;s take it step by step.</p>
<p>The <em>type</em> of the return value goes at the end of the Function declaration.  We split it out using a colon (two dots), just like when we type a variable.  Here&#8217;s a punctuation refresher:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/punctuation.jpg" alt="Punctuation refresher">
</div>
<p>And here&#8217;s a representation of the Function using colour blocks, because colours are pretty:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/functionReturnValues.jpg" alt="Return Values">
</div>
<p>By adding that datatype to the end of our Function declaration, we&#8217;re making a promise: the Function will return (pay out/cough up) something that has that datatype.  To live up to that promise, we use the <b>return</b> keyword.  Return is the command that pays/spits out/coughs up a value.  In the code example above, the giveMeGoodNewsOnly Function is typed <b>String</b>, which means it must return a String.  In the statement block (between the curly braces), we use the <b>return</b> keyword to return a String.  And all was right with the world.</p>
<h2>Where Does the Return Type Go?</h2>
<p>So, fine &#8211; we <b>return</b> something from our Function.  But what happens to that value?  Remember when our Pac Man-like code interpreter chewed through the Function call, and packed his suitcase with the arguments?</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/step1.jpg" alt="Call a Function"></p>
<p>The code interpreter recognizes this as a Function call &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/step2.jpg" alt="Bring the Arguments"></p>
<p>&#8230; and takes the arguments with him in his suitcase.
</p></div>
<p>When the code interpreter finds the function, he unpacks the arguments into the parameters.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/unpack.jpg" alt="Unpack the arguments"></p>
</div>
<p>At this point, the code interpreter has an empty suitcase.  When he reaches a <b>return</b> statement, he packs the value into his suitcase.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/pack.jpg" alt="Pack the return value"></p>
<p>Get packing, Pac Man.
</p></div>
<p>As we saw earlier, the code interpreter returns to the next line after the original function call.  But this time, he&#8217;s got a suitcase packed with a return value:</p>
<p>
<object width="600" height="500">
<param name="movie" value="http://www.untoldentertainment.com/blog/img/2010_12_02/returnValueFlow.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<param name="allowScriptAccess" value="always"></param>
<embed type="application/x-shockwave-flash" width="600" height="500" src="http://www.untoldentertainment.com/blog/img/2010_12_02/returnValueFlow.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
</p>
<p>(this is an interactive demonstration!  Keep clicking the &#8220;Next&#8221; button to follow the code interpreter through the example)</p>
<h2>Unpacking the Return Value</h2>
<p>When the code interpreter arrives from his journey with the return value in tow, we need to somehow capture that return value.  Otherwise, the interpreter just throws it away and it disappears into the either.</p>
<p>Here&#8217;s a simple example to demonstrate that concept:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">6</span>+<span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>You know and i know that 6+5 is 11. Your computer knows that too.  When you type that into a line of code, the computer adds 6 and 5.  And then &#8230;?  Nothing.  We haven&#8217;t told the computer to <em>do</em> anything with that result, so it&#8217;s a wasted piece of code.</p>
<p>Here are a couple of ways we can make use of the 11 result.  We can store the result in a variable (bucket), or we can trace the result to the output/console window:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> result : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">6</span>+<span style="color: #cc66cc;">5</span>; <span style="color: #808080; font-style: italic;">// Store the value 11 in a variable called &quot;result&quot;</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span>+<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Print 11 to the output window</span></pre></div></div>

<p><b>Note:</b> Unity javascript users should use <b>print</b> or <b>Debug.Log</b> instead of <b>trace</b> to print to the <b>Console</b> window.</p>
<p>It&#8217;s the same issue with Functions and their return values.  If we call the sayHello Function and it returns a value, the value is going to disappear unless we capture it somehow.  Just like the 6+5 example, let&#8217;s store the return value in a variable, or print it to the output window:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> greeting : <span style="color: #0066CC;">String</span> = sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Jennifer&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Store the return value &quot;Hello, Jennifer&quot; in a variable called greeting</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Matthias&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Print Hello, Matthias to the output window</span></pre></div></div>

<p>The code interpreter needs some place to unpack his bags.  These are just two ways to capture the return value &#8211; there are many, many more.  For example, you can add the result to an Array using the Push method (see <a href="http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/">Understanding Arrays</a> for more info).</p>
<h2>You Get a-NOTHING!!!</h2>
<p>What do we do about a Function that doesn&#8217;t return any value at all?  Can we just leave that little section at the end of the declaration blank?   Well, you <em>can</em>, but it all depends on how picky your compiler is.  You can set the Flash compiler to <b>Strict</b> mode &#8230; certain other compilers are strict by default.  This means that every variable must be typed (given a datatype), and every Function must also be typed.</p>
<p>The keyword for &#8220;no type&#8221; is <b>void</b>.  If your Function doesn&#8217;t return a value, type it as void:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> thisReturnsNothing<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Return Stops the Show</h2>
<p>The only really important thing to know about the <b>return</b> keyword is that when the code interpreter chews its way down to that line, it&#8217;s like hitting the EJECT button.  The interpreter gets booted out of your Function, regardless of whatever statements you may have written beneath it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span>playerName : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Hello, &quot;</span> + playerName;
    <span style="color: #808080; font-style: italic;">// This line doesn't get executed.</span>
    <span style="color: #808080; font-style: italic;">// Neither does this one.</span>
    <span style="color: #808080; font-style: italic;">// Nor this one.</span>
    <span style="color: #808080; font-style: italic;">// In fact, anything you write after a return statement gets skipped.</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Breaking Stuff</h2>
<p>As with arguments, there are some great ways to break a Function with return values. In brief, here&#8217;s how to do it:</p>
<ol>
<li>Return a value even though your Function is typed <b>void</b> (or it&#8217;s untyped).
<li>Type your Function to return a value, and don&#8217;t return a value.
<li>Return a datatype that&#8217;s different than the return type declaration.
<li>Write some branching logic so that it&#8217;s possible your Function may not return a value.
</ol>
<p>Let&#8217;s look at each of these, and the error statements that pop up when we make each mistake.</p>
<h2>1. Return a Value When You Shouldn&#8217;t</h2>
<p>Here&#8217;s an example of a Function that is typed <b>void</b>, but it returns an int:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> whoops<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">57</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When you do this, here&#8217;s the error that the Flash compiler throws:</p>
<blockquote><p>
Scene 1, Layer &#8216;Layer 1&#8242;, Frame 1, Line 3	1051: Return value must be undefined.</p></blockquote>
<p>That&#8217;s a pretty clear error message.  Thank you, eggheads.</p>
<p>Here&#8217;s the error message you get when you use Unity javascript:</p>
<blockquote><p>Assets/NewBehaviourScript.js(3,12): BCE0022: Cannot convert &#8216;int&#8217; to &#8216;void&#8217;.</p></blockquote>
<p>Again, with a tiny bit of detective work, this error is not too tricky to sort out.  The compiler thinks you&#8217;re trying to make it force a value of type int to be a value of type void (which is what you&#8217;re returning), but this is impossible, <em>even for computers</em>.</p>
<h2>2. Promise a Return Value, and then Cop Out</h2>
<p>If we type a Function with anything other than <b>void</b>, we&#8217;re pledging to return a value.  If we don&#8217;t return a value of that type, the compiler shows up on our doorstep in the middle of the night with mascara streaking her cheeks, demanding to know why you never do the things you say you&#8217;re going to do.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/psychogf.jpg" alt="Psycho Girlfriend"></p>
<p>You said we would have BABIES!  And ints!!!
</p></div>
<p>Here&#8217;s an example of a promise a programmer didn&#8217;t follow up on with a return statement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> makeBabiesWithTanya<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">int</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> numberOfBabiesWeCanHave : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">17</span>;
    <span style="color: #808080; font-style: italic;">// Honest.</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So because the <b>return</b> statement is missing, this Function doesn&#8217;t actually give Tanya any babies.  And that&#8217;s when she notices something is up:</p>
<blockquote><p>Scene 1, Layer &#8216;Layer 1&#8242;, Frame 1, Line 5	1170: Function does not return a value.</p></blockquote>
<p>This is one of the clearest error messages you&#8217;ll ever receive when using Actionscript 3.  Let&#8217;s see how it looks in Unity javascript:</p>
<blockquote><p>(no error)</p></blockquote>
<p>Unity is clearly a lot more easy-going about this type of thing.  Remember that when you&#8217;re thinking of which one you should invite on a road trip to Vegas.</p>
<h2>3. Return the Wrong Datatype</h2>
<p>If the Function is typed <b>int</b>, we gotta return an int.  If it&#8217;s typed <b>Array</b>, we gotta return an array.  Here&#8217;s an obvious boob:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> iAmConfused<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">int</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;nyearrrghhh&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This Function is typed <b>int</b>, but we&#8217;re returning a String.  Here&#8217;s the Flash error:</p>
<blockquote><p>Scene 1, Layer &#8216;Layer 1&#8242;, Frame 1, Line 3	1067: Implicit coercion of a value of type String to an unrelated type int.</p></blockquote>
<p>Nice and clear.  And in Unity javascript:</p>
<blockquote><p>Assets/NewBehaviourScript.js(3,12): BCE0022: Cannot convert &#8216;String&#8217; to &#8216;int&#8217;.</p></blockquote>
<p>These are both very similar, and they do a pretty good job at getting the point across.</p>
<h2>4. Faulty Logic</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/labyrinth.jpg" alt="The Labyrinth">
</div>
<p>If you write a Function that should return a value, and you load it up with branching conditional logic, the compiler will actually step through that logic to make sure a value gets returned.  Here&#8217;s an example of a logic structure that may never return the value promised by the Function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> badLogic<span style="color: #66cc66;">&#40;</span>playerName : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>playerName == <span style="color: #ff0000;">&quot;Pete&quot;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Hi, Pete!&quot;</span>;
     <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>playerName == <span style="color: #ff0000;">&quot;Carl&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Hi, Carl!&quot;</span>;
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So if the playerName is Pete, we return &#8220;Hi, Pete&#8221;, and if the playerName is Carl, we return &#8220;Hi, Carl&#8221;.  But what if the playerName is Suzie, Jasmine, Turk, Paolo or Ferdinand?  In that case, neither of the statements will get executed, and so it&#8217;s possible that the Function will not return a value.</p>
<p>Here&#8217;s what the Actionscript 3 compiler has to say about that:</p>
<blockquote><p>Scene 1, Layer &#8216;Layer 1&#8242;, Frame 1, Line 9	1170: Function does not return a value.</p></blockquote>
<p>Clear as crystal!  And now, Unity javascript:</p>
<blockquote><p>(no error)</p></blockquote>
<p>Hmm.  Now we&#8217;re up against a situation where permissive coding isn&#8217;t always a good thing.  As a very new programmer, you might think &#8220;the fewer errors, the better&#8221;.  But if you&#8217;re expecting that Function to return a value, and it somehow doesn&#8217;t, wouldn&#8217;t it be <em>more</em> helpful to see an error telling you what went wrong?</p>
<p>Answer: yes.  Yes it would.</p>
<h2>Your Triumphant Return</h2>
<p>Those are the essentials of writing Functions that return values.  Together with <a href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-functions/">Understanding Functions</a> and <a href="http://www.untoldentertainment.com/blog/2010/12/02/understanding-functions-arguments/">Understanding Functions &#8211; Arguments</a>, you should have a complete picture of what Functions are and what they can do.  But this is the HOW, not then WHEN of it.  These articles just discuss the <em>building blocks</em> of Object-Oriented Programming.  Assembly instructions, and even freestyle building &#8211; are entirely different matters altogether.</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature. </p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F03%2Funderstanding-functions-return-values%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F03%2Funderstanding-functions-return-values%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/03/understanding-functions-return-values/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3259&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/03/understanding-functions-return-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Functions &#8211; Arguments</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/02/understanding-functions-arguments/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/02/understanding-functions-arguments/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 05:49:09 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3246</guid>
		<description><![CDATA[There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Functions are one of them. In Understanding Functions, we learned that Functions are a way to bundle code statements together into a tight little unit that we can invoke (or &#8220;call&#8221;) any time [...]]]></description>
			<content:encoded><![CDATA[<p>There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Functions are one of them.</p>
<p>In Understanding Functions, we learned that Functions are a way to bundle code statements together into a tight little unit that we can invoke (or &#8220;call&#8221;) any time we like, and as often as we like.  There are two very important features to learn about Functions: Arguments and Return Values.  We&#8217;ll figure out Arguments in this article.</p>
<h2>Til You&#8217;re Blue in the Face</h2>
<p>Arguments aren&#8217;t as unfriendly as they sound.  They&#8217;re just additional pieces of information we can send to a Function during a Function call.  You&#8217;ll remember that a Function call looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">doSomething<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Quite often, we need to pass additional information to Functions to make them super-useful.  Let&#8217;s imagine we have a text input field where the player can enter his name.  The player types in his name and clicks a button, and the game says &#8220;Hello, John&#8221; (or whatever the player&#8217;s name is).  The <em>dynamic</em>, or changing, part of that Function is the player&#8217;s name.  Here&#8217;s how that Function might look if it was &#8220;hard-coded&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hello, John.&quot;</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #808080; font-style: italic;">// ain't nothing dynamic about this</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>Note:</b> Unity javascript people will want to start the function name with a capital letter as a best practice, and use either <b>print</b> or <b>Debug.Log</b> in place of trace.</p>
<p>This is okay, but the problem is that every time we <b>call</b> (run, execute, do) that Function, it&#8217;s going to say &#8220;Hello, John.&#8221;  What if the player&#8217;s name isn&#8217;t John?  Well, we need to make that part dynamic, because it changes.</p>
<h2>Dynamic!</h2>
<p>Let&#8217;s set up our Function so that we say &#8220;Hello&#8221;, and then tack on a variable that holds the player&#8217;s name:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hello, &quot;</span> + playerName<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>(When working with Strings, the <b>+</b> operator glues two strings together.)</p>
<p>It&#8217;s getting better, but we have a new problem: the code interpreter is going to see playerName and think &#8220;playerName?  What the heck is that?&#8221;  Then it&#8217;s going to freak out and the Earth is going to crash into the Sun, and we don&#8217;t want that.</p>
<p>We need to declare and define the playerName variable somewhere.  But instead of just declaring playerName any old place, we&#8217;re going to <em>tell</em> the sayHello Function which playerName we&#8217;d like it to say.  We&#8217;re going to set our sayHello Function up so that it has a <b>parameter</b>.  We do that between the round brackets in the Function declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span>playerName : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hello, &quot;</span> + playerName<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>Parameters</b> go between the round brackets in a Function declaration. In the continuing tradition of seeing our blocks of code as visual elements, check this out:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/artistVision.jpg" alt="Parameters"></p>
<p>If you&#8217;re a visual person, learning to see code in blocks like this could help you out a lot.
</p></div>
<p>When we structure a Function this way, we say that the sayHello Function <em>accepts</em> one parameter, which is called playerName.  The <b>type</b> of playerName is a String (a list of letters and/or numbers). Notice that even though this is kind of like a variable declaration, we don&#8217;t need to use the <b>var</b> keyword.</p>
<h2>Exact Change Required</h2>
<p>i like to think of <b>parameters</b> as the <em>payment</em> that a Function accepts. The sayHello Function takes one <b>argument</b> and stores it in its first <b>parameter</b>. Since the first parameter is of type <b>string</b>, we need to &#8220;pay&#8221; it a string.  If we pay any other datatype (int, float, number, boolean, array), the compiler will throw an error. </p>
<p>Here&#8217;s how we &#8220;pay&#8221; a string to the sayHello Function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Murphy&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Freeze frame!  Let&#8217;s become the Pac Man-like code interpreter and follow the logic of this statement for a moment.</p>
<p>The interpreter gets to this line and recognizes it as a Function call, thanks to those round brackets at the end.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/step1.jpg" alt="Step 1"></p>
</div>
<p>It also sees &#8211; hey! &#8211; the programmer has passed an argument to the Function inside those round brackets.  So Pac Man puts that in his suitcase, hops in the DeLorean, and travels off in search of the sayHello Function.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/step2.jpg" alt="Step 2"></p>
</div>
<p>When he finds it, before he starts chewing through the statements in the statement block (between the curly braces), the Pac Man-like code interpreter drops off the argument into the waiting playerName parameter bucket, between the round brackets of the Function declaration.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/step3.jpg" alt="Step 3"></p>
</div>
<p>Now the parameter called playerName has a value of &#8220;Murphy&#8221;.  Since &#8220;Murphy&#8221; is a String, and since the sayHello Function demands exact change (one argument of type <b>String</b>), we&#8217;re good to go!</p>
<p>With payment resolved, the code interpreter chews through the statements in the statement block (between the curly braces), reaches the bottom of the Function, and hops back in the DeLorean to pick up where he left off, back at the end of the Function call.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_02/step4.jpg" alt="Step 4"></p>
</div>
<p>Because the value of the variable playerName is the String &#8220;Murphy&#8221;, the result is that the console/output window says <b>Hello, Murphy</b>.  It will say Hello to whatever name we pass in as an argument.</p>
<h2>Breakamage Pt.1 &#8211; Wrong Datatype</h2>
<p>It&#8217;s really important to break code and cause errors, so that when you&#8217;re in a jam on a project and an error comes up, you&#8217;ll know what it means and how to deal with it.</p>
<p>Try tweaking the sayHello Function call so that you pass an int (integer/whole number) instead of a String:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">42</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>42 is not a String.  Your first clue is that it&#8217;s missing those double-quotation marks around it.  Since the sayHello Function <em>DEMANDS</em> exactly one string as payment, the compiler throws an error.</p>
<p>This is what that error looks like when we use ActionScript 3:</p>
<blockquote><p>Scene 1, Layer &#8216;Layer 1&#8242;, Frame 1, Line 1	1067: Implicit coercion of a value of type int to an unrelated type String.</p></blockquote>
<p>i really take exception to the way eggheads write error messages.  They could stand to be a LOT more clearly written.  But here&#8217;s what this error is trying to say:  &#8220;implicit coercion&#8221; means you&#8217;re trying to force something to be something else, and ActionScript 3 can&#8217;t manage it.  In this case, you&#8217;re trying to force an int (42) to become a String when Pac Man delivers the goods to the sayHello Function, and the compiler is having a minor freak-out.  That&#8217;s your clue that you may have passed a mismatched type as an argument to a Function.</p>
<p>This is the error message that the same mistake will cause in Unity javascript;</p>
<blockquote><p>Assets/NewBehaviourScript.js(4,10): BCE0017: The best overload for the method &#8216;Tutorial.sayHello(String)&#8217; is not compatible with the argument list &#8216;(int)&#8217;.</p></blockquote>
<p>Ugh!  You eggheads are <em>killing me</em>.  Forget what an &#8220;overload&#8221; is and focus in on the part that (sorta) makes sense to you right now: <em>something</em> is not compatible with the argument list.  You can see quite clearly that one is a String, and the other is an int.  That&#8217;s your clue that you&#8217;ve botched the argument type.  Go back and patch this up, and you&#8217;ll be golden.</p>
<h2>Extremely Argumentative</h2>
<p>So is that it?  Can Functions only accept one argument?  No &#8230; they can actually accept multiple arguments.  Be sure to comma-separate them, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">makePizza<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;pepperoni&quot;</span>, <span style="color: #ff0000;">&quot;mustard&quot;</span>, <span style="color: #ff0000;">&quot;tree bark&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> makePizze<span style="color: #66cc66;">&#40;</span>topping1 : <span style="color: #0066CC;">String</span>, topping2 : <span style="color: #0066CC;">String</span>, topping3 : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>B..but &#8230; do they all have to be the same datatype (String)?   Heck no!  You can mix and match.  The only rules are that you need to pass the arguments into the Function in the same order as the parameter list, and you need to pass the same <em>number</em> of arguments the Function requires.  The Pac Man-like code interpreter unpacks these one at a time in order.  So let&#8217;s pretend there&#8217;s a function that takes a String, an int and an Array (in that order):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> initPatient<span style="color: #66cc66;">&#40;</span>patientName : <span style="color: #0066CC;">String</span>, age : <span style="color: #0066CC;">int</span>, aPhobias : <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As long as we pass in a String, an int and an Array, in that order, it works:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> theName : <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Shaggy&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> theAge : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">17</span>;
<span style="color: #000000; font-weight: bold;">var</span> aThePhobias : <span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;like, ghosts&quot;</span>, <span style="color: #ff0000;">&quot;like, zombies&quot;</span>, <span style="color: #ff0000;">&quot;like, creepy-crawly things&quot;</span><span style="color: #66cc66;">&#41;</span>;
initPatient<span style="color: #66cc66;">&#40;</span>theName, theAge, aThePhobias<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That&#8217;s fine.  But as soon as we mix up the order, there&#8217;s trouble:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> theName : <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Shaggy&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> theAge : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">17</span>;
<span style="color: #000000; font-weight: bold;">var</span> aThePhobias : <span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;like, ghosts&quot;</span>, <span style="color: #ff0000;">&quot;like, zombies&quot;</span>, <span style="color: #ff0000;">&quot;like, creepy-crawly things&quot;</span><span style="color: #66cc66;">&#41;</span>;
initPatient<span style="color: #66cc66;">&#40;</span>theAge, theName, aThePhobias<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// ERROR!  Totally long-winded explanation that could really be made a lot more clear, but eggheads aren't about making other people's lives easier (unfortunately).  In short, argument type mismatch.</span></pre></div></div>

<p>So far so good?  You may have noticed that i made the names of the variables i passed into the Function as arguments conspicuously different than the names of the parameters i accepted in the initPatient Function.  This is because i wanted you to see that there is a difference between these two things, and that there CAN be a difference.  When you call a Function and pass it some arguments, the variable names you pass in don&#8217;t matter &#8230; it&#8217;s the values <em>stored</em> in those variables that Pac Man takes with him on his journey.</p>
<h2>Breakamage Pt.2 &#8211; Wrong Number of Arguments</h2>
<p>Just as sending in the wrong <em>type</em> of argument will throw an error (ie paying a quarter when the Function wants a nickel), the compiler will throw an error if you pass in the wrong <em>number</em> of arguments.</p>
<p>In this example, we have a Function that accepts one argument, but we try to pass it two arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> eatBacon<span style="color: #66cc66;">&#40;</span>numberOfStrips : <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;OM NOM NOM!  I just ate &quot;</span> + numberOfStrips + <span style="color: #ff0000;">&quot; strips of delicious bacon.&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
eatBacon<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">57</span>, <span style="color: #ff0000;">&quot;i can haz heart attack&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// ERROR!!</span></pre></div></div>

<p>The function wants only one argument, but we&#8217;re sending it two. Here&#8217;s what the error looks like when we use Actionscript 3:</p>
<blockquote><p>Scene 1, Layer &#8216;Layer 1&#8242;, Frame 1, Line 6	1137: Incorrect number of arguments.  Expected no more than 1.</p></blockquote>
<p>Thankfully, this is one of the clearest error message i&#8217;ve ever seen an egghead write.</p>
<p>This is what the error looks like when we use Unity javascript (changing <b>trace</b> to either <b>print</b> or <b>Debug.Log</b> and using a capital letter to begin our Function name):</p>
<blockquote><p>Assets/NewBehaviourScript.js(8,9): BCE0017: The best overload for the method &#8216;NewBehaviourScript.eatBacon(int)&#8217; is not compatible with the argument list &#8216;(int, String)&#8217;.</p></blockquote>
<p>That&#8217;s the same error we get when there&#8217;s an argument type mismatch, so the Actionscript eggheads did a slightly better job of communicating the problem than the Unity eggheads did.  The important thing is that you learn to recognize why Unity or Flash are throwing the error so that you can fix it quickly and get on with your project.</p>
<p>Note that you&#8217;ll throw an error even if you &#8220;overpay&#8221; a Function.  If a Function wants exactly two arguments, and you give it nine, you&#8217;ll get one of these errors. </p>
<h2>But Wait &#8211; There&#8217;s More!</h2>
<p>There are two more advanced topics when it comes to passing and accepting arguments &#8211; optional arguments, and Rest arguments.  They&#8217;re useful, but not crucial.  We&#8217;ll be sure to cover them in future articles.</p>
<p>For now, get some practice passing arguments to your Functions, and writing parameters to store those articles.  <em>The fate of the free world may depend on it!</em>  Or whatever.</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature. </p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F02%2Funderstanding-functions-arguments%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F02%2Funderstanding-functions-arguments%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/02/understanding-functions-arguments/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3246&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/02/understanding-functions-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Functions</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/01/understanding-functions/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/01/understanding-functions/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 17:02:46 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>
		<category><![CDATA[Bizarre]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3243</guid>
		<description><![CDATA[There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Functions are one of them. Bang on De Drum All Day Here&#8217;s a secret about programmers: they are inherently lazy. The less typing a programmer can do, the better. Programming is all about [...]]]></description>
			<content:encoded><![CDATA[<p>There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages.  Functions are one of them.</p>
<h2>Bang on De Drum All Day</h2>
<p>Here&#8217;s a secret about programmers: they are inherently lazy.  The less typing a programmer can do, the better.  Programming is all about dreaming up complex tasks that programmers don&#8217;t want to do, and delegating those tasks to a computer so that the programmer can blow off and watch cartoons the rest of the day.</p>
<p>Many programming structures are designed to make a programmer&#8217;s life easier.  And a programming rule of thumb is <em>generally</em> that if you have to type something twice, there&#8217;s an opportunity to make your code more efficient.</p>
<p>Functions make your code more efficient.  Let&#8217;s imagine a game where we need to create a bunch of monsters.  If you&#8217;ve read <a href="http://www.untoldentertainment.com/blog/2010/11/24/understanding-loops/">Understanding Loops</a>, you know that you can set up a loop to run an action multiple times.  i&#8217;ll use a combination of pseudocode (in comments) and real code to demonstrate:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Make 50 monsters.</span>
<span style="color: #000000; font-weight: bold;">var</span> totalMonsters:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">50</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalMonsters; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// make a monster</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So far so good.  But there are a few other things we have to do with these monsters we create.  We need to put each one on a hill.  Then we need to dress each monster up in a Power Hat.  Next, we need to hide a key to each monster&#8217;s hilly domain.  Let&#8217;s modify the loop:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Make 50 monsters.</span>
<span style="color: #000000; font-weight: bold;">var</span> totalMonsters:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">50</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalMonsters; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// make a monster</span>
    <span style="color: #808080; font-style: italic;">// put the monster on a hill</span>
    <span style="color: #808080; font-style: italic;">// put a power hat on the monster</span>
    <span style="color: #808080; font-style: italic;">// hide a key</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Mmmm &#8230; we&#8217;re almost there.  Now we need to create a portrait for that each monster and put it inside the castle, so that when the hero defeats a monster, the portrait gets a big checkmark on it.  Then we need to create a magic gem that will kill each monster.  Each gem needs to be buried somewhere underground.  Finally, we need to create a villager for each monster in the game. Each villager will give you a clue as to where to find the monster-killing gem.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Make 50 monsters.</span>
<span style="color: #000000; font-weight: bold;">var</span> totalMonsters:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">50</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalMonsters; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// make a monster</span>
    <span style="color: #808080; font-style: italic;">// put the monster on a hill</span>
    <span style="color: #808080; font-style: italic;">// put a power hat on the monster</span>
    <span style="color: #808080; font-style: italic;">// hide a key</span>
    <span style="color: #808080; font-style: italic;">// create a portrait</span>
    <span style="color: #808080; font-style: italic;">// put the monster's face on the portrait</span>
    <span style="color: #808080; font-style: italic;">// put the portrait inside the castle</span>
    <span style="color: #808080; font-style: italic;">// create a gem</span>
    <span style="color: #808080; font-style: italic;">// bury the gem underground</span>
    <span style="color: #808080; font-style: italic;">// create a villager</span>
    <span style="color: #808080; font-style: italic;">// give the villager a gem clue</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Oh man.  This loop is getting real ugly, real fast.  There&#8217;s so much STUFF in there.  It&#8217;s getting out of control.</p>
<p>If we take a close look, we see that we&#8217;re running a few tasks that can be grouped together.  All the monster stuff goes together.  All the key stuff goes together.  The gem stuff goes together.  The villager stuff goes together.  That&#8217;s four separate <em>bundles</em> of tasks that, like a pile of sticks, we should be able to wrap up with twine in a tidy little package.</p>
<h2>I Have a Tiny Little Package</h2>
<p>That&#8217;s one of the things Functions do for us:  they allow us to group related tasks together into a little package, and we can refer to that package over and over again without having to repeat any lines.</p>
<p>A <b>Function declaration</b> looks very much like a variable or array declaration.  Instead of the <b>var</b> keyword, we use the <b>function</b> keyword.  In ECMA-based languages like Actionscript 3, Unity javascript and C#, functions are followed by round brackets.  Then you open up a statement block between curly brackets, just like in a <b>For Loop</b> or a conditional (If) statement:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> doSomething<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">// statements go here</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>Note:</b> If you&#8217;re using Unity javascript, best practice is to begin a function name with a capital letter.</p>
<p>So <b>doSomething</b> is our function name.  That&#8217;s a custom name that we came up with.  It makes a lot of sense to use verbs (action words) when naming your functions.  We&#8217;ll see why in a moment.</p>
<p>Let&#8217;s carve out the statements involving the monsters and put them in their own function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> initMonsters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// make a monster</span>
    <span style="color: #808080; font-style: italic;">// put the monster on a hill</span>
    <span style="color: #808080; font-style: italic;">// put a power hat on the monster</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>(the &#8220;init&#8221; here is short for &#8220;initialize&#8221;, which means &#8220;set it up&#8221;)</p>
<p>Now, back in the loop, we can substitute those three statements with a <b>Function call</b>.  A Function call always uses the name of the function, followed by round brackets, with a semi-colon to end the statement.  Take a look:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Make 50 monsters.</span>
<span style="color: #000000; font-weight: bold;">var</span> totalMonsters:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">50</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalMonsters; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    initMonsters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// We removed these three statements and put them in their own function.</span>
    <span style="color: #808080; font-style: italic;">// Now, we just call the function.</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// hide a key</span>
    <span style="color: #808080; font-style: italic;">// create a portrait</span>
    <span style="color: #808080; font-style: italic;">// put the monster's face on the portrait</span>
    <span style="color: #808080; font-style: italic;">// put the portrait inside the castle</span>
    <span style="color: #808080; font-style: italic;">// create a gem</span>
    <span style="color: #808080; font-style: italic;">// bury the gem underground</span>
    <span style="color: #808080; font-style: italic;">// create a villager</span>
    <span style="color: #808080; font-style: italic;">// give the villager a gem clue</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In common programming parlance, this is called <em>calling a Function</em>.</p>
<p>To understand how a function call works, we need to put ourselves back in the role of our code interpreter, which is a lot like Pac Man.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_01/functions.jpg" alt="Pac Man and functions"></p>
</div>
<p>Pac Man hits the first statement in the loop.  It&#8217;s a function call.  So he jumps out of the loop to wherever the Function is declared, and chews through all of the statements in that Function. When he reaches the bottom of the Function, he bounces back to the loop again, and resumes chewing through code at the very next line.</p>
<p>This works very nicely.  Now we can go through and offload all of our chunks of functionality into their own Functions.  When we write it all up, i looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Make 50 monsters.</span>
<span style="color: #000000; font-weight: bold;">var</span> totalMonsters:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">50</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalMonsters; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    initMonsters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    initKeys<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    initPortraits<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    initGems<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    initVillagers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Look how much easier to read that loop is now. It&#8217;s very clear that we loop 50 times, and each time we make some monsters, some keys, some portraits, some gems, and some villagers.  The details for each process are buried in their respective functions.  Now, when we look at the initKeys function, we only have to worry about tasks related to initializing those keys.</p>
<p>This bouncing around that the Pac Man-like code interpreter does is the reason why i say that Object Oriented Programming (OOP) is like taking multiple plane flights through time portals. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/map.jpg" alt="Pac Man Map"></p>
<p>Pac to the Future
</p></div>
<p>There are two more very important things to learn about functions: Arguments and Return Values.  These are detailed in two additional articles.  Once you read through those, you&#8217;ll know almost everything there is to know about Functions, and you&#8217;ll be ready to start putting that knowledge into practice by building your own stuff!</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F01%2Funderstanding-functions%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F01%2Funderstanding-functions%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-functions/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3243&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/01/understanding-functions/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Understanding Loops with Arrays</title>
		<link>http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 16:02:00 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3224</guid>
		<description><![CDATA[There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Arrays are one of them. Loops are another. i covered both of these structures in the previous aptly-named articles Understanding Arrays and Understanding Loops. Now that we understand loops, and we understand arrays, [...]]]></description>
			<content:encoded><![CDATA[<p>There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages.  Arrays are one of them.  Loops are another.  i covered both of these structures in the previous aptly-named articles <a href="http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/">Understanding Arrays</a> and <a href="http://www.untoldentertainment.com/blog/2010/11/24/understanding-loops/">Understanding Loops</a>.</p>
<p>Now that we understand loops, and we understand arrays, we&#8217;re ready to smack their featureless plastic genitals together like we&#8217;re making Barbie have babies. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_01/wonderTwins.gif" alt="Wonder Twins"></p>
<p>(Which one of these is the chick again?)
</p></div>
<p>Arrays really come alive with Loops.  By using an Array&#8217;s <b>length</b> property as the loop limiter, we can step through every element in an Array one by one.</p>
<p>Here&#8217;s what that looks like in code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Nigel Tufnel&quot;</span>, <span style="color: #ff0000;">&quot;Derek Smalls&quot;</span>, <span style="color: #ff0000;">&quot;David St. Hubbins&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> =<span style="color: #cc66cc;">0</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>myArray.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>myArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// This prints each element in the array, one by one, to the console/output window </span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>Note:</b> If you&#8217;re using Unity javascript, try print or Debug.Log instead of trace.</p>
<p>If you truly understand Loops, this shouldn&#8217;t be too challenging for you.  Instead of using some arbitrary value like one thousand as our limiter, we&#8217;re using the array&#8217;s <b>length</b> property, which is the number of elements in the array.  This property is dynamic, so that any time we add things to our array, or take them away, length is updated for us. We don&#8217;t have to rely on a hard-coded value (which means a number that doesn&#8217;t change).</p>
<p>The code snippet shown above traces out each element in the array, one after the other:</p>
<ul>
<li>Nigel Tufnel
<li>Derek Smalls
<li>David St. Hubbins
</ul>
<p>This is because on every <b>iteration</b> of the loop, our <b>iterator</b> variable called <b>i</b> is increased by one.</p>
<p><b>i</b> begins life with a value of 0.  On the first run through the loop, the code interpreter <b>resolves</b> myArray[i] to myArray[0].  The first element in the array, at index 0, is &#8220;Nigel Tufnel&#8221;, so that&#8217;s what you&#8217;ll see in the output console.</p>
<p>On the next iteration, <b>i</b> gets jacked up to 1.  The element at index 1 in the array is &#8220;Derek Smalls&#8221;, so that&#8217;s what prints out.  </p>
<p>Finally, in the third and final round, we print out &#8220;David St. Hubbins&#8221;, because myArray[i] resolves to myArray[2].  The element at index 2 of the array is &#8220;David St. Hubbins&#8221;.</p>
<p>After that, the interpreter bounces back to the top of the loop.  i is incremented to 3.  We check the limiter: </p>
<p><b>i < myArray.length</b></p>
<p>Because <b>i</b> is 3 and myArray.length is also 3, this line resolves to 3 < 3.  Since 3 is not less than 3, the answer here is <b>false</b>.  And because the answer is false, the code interpreter is not allowed to go through the loop any more.  It happily exits the loop and starts chewing through the code beneath the bottom closed curly brace that ties up the loop.</p>
<p><b>Protip:</b> If you&#8217;re a stickler for speed, it&#8217;s marginally faster to store myArray.length in a variable, to prevent your interpreter from having to do the length calculation on every iteration of your loop. Here&#8217;s what that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Nigel Tufnel&quot;</span>, <span style="color: #ff0000;">&quot;Derek Smalls&quot;</span>, <span style="color: #ff0000;">&quot;David St. Hubbins&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> =<span style="color: #cc66cc;">0</span>;
<span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">int</span> = myArray.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>len; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>myArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We store myArray.length in a variable called len, and then refer to that variable in the limiter section of the loop header.</p>
<p>For even <em>more</em> of a tiny speed increase, do away with the <b>new</b> keyword and declare the array like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myArray:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Nigel Tufnel&quot;</span>, <span style="color: #ff0000;">&quot;Derek Smalls&quot;</span>, <span style="color: #ff0000;">&quot;David St. Hubbins&quot;</span><span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<h2>Who Cares?</h2>
<p>This is all still pretty pointless.  When are you ever going to use this when programming a game?  Well, for the rest of the article, i&#8217;ll lay some real-world examples on you to give you an idea of how to maximize the wonderful union of Loops with Arrays.</p>
<p>Think of any situation in a video game when you have a list of similar things.  In a first person shooter, you need to keep track of all the bullets that get fired.  Makes sense to create an array to store all of those bullets.  You can add bullets to the list and remove them from the list using the Array class methods we looked at in the Understanding Arrays article.</p>
<p>Most games run on one giant loop, with an Update process and a Draw process.  <b>Update</b> figures out where everything should be, and <b>Draw</b> renders those things to the player&#8217;s view.  If you want to update the positions of all the bullets, you may need to use a <b>For Loop</b> to loop through your collection of bullets and move them one tiny bit.  Then in the Draw process, you loop through your list of bullets and render them to the screen. When you repeat these processes quickly enough, you get the illusion of animation.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_01/blackOps.jpg" alt="Black Ops"></p>
<p>We might keep separate lists of the bullets, the enemies, the friendlies, and the bomb shells.
</p></div>
<p>Very often, you&#8217;ll use a loop to create the initial array of objects, and then more loops later to pick through that list.  Here&#8217;s an example of two functions.  One initializes all of the monsters, and the other kills all of the monsters (assuming the player just used the &#8220;Kill All Monsters&#8221; megabomb power-up).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> aMonsters:<span style="color: #0066CC;">Array</span>; <span style="color: #808080; font-style: italic;">// this array is a bucket that can hold many things: namely, monsters.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> initMonsters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
     aMonsters = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;  <span style="color: #808080; font-style: italic;">// Defines an empty instance of the Array class.</span>
     <span style="color: #000000; font-weight: bold;">var</span> totalMonsters:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">50</span>; <span style="color: #808080; font-style: italic;">// this is a variable (bucket) that contains the number of monsters we'd like to begin with</span>
     <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalMonsters; i++<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#123;</span>
         aMonsters.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Monster<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Creates a new instance of the Monster class, and adds that instance to the aMonsters array.  </span>
         <span style="color: #808080; font-style: italic;">// If that's confusing to you, we can split this up into two separate lines:</span>
         <span style="color: #808080; font-style: italic;">// var monster:Monster = new Monster();</span>
         <span style="color: #808080; font-style: italic;">// aMonsters.push(monster);</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> killAllMonsters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">int</span> = aMonsters.<span style="color: #0066CC;">length</span>; <span style="color: #808080; font-style: italic;">// store the length of the aMonsters array in a variable called len.</span>
     <span style="color: #808080; font-style: italic;">// By doing this, we speed up our loop ever so slightly.</span>
     <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>len; i++<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">var</span> monster:Monster = aMonsters<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// stores a reference to the first monster in the array</span>
         monster.<span style="color: #006600;">die</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// calls some sort of death method on the monster instance</span>
         aMonsters.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// removes the first element of the array.  The aMonsters array gets shorter and shorter every time we loop, as we remove monster after monster.  By the end of this loop, the aMonsters array is empty.</span>
     <span style="color: #66cc66;">&#125;</span>   
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>Note:</b> If some of the initialization stuff here looks a bit spooky, try reading the <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Understanding Classes</a> series.  Hopefully you&#8217;re catching what i&#8217;m throwing regarding loops and arrays, even if the class stuff is a little unfamiliar.</p>
<p><b>For Unity javascript:</b> Everything here is almost identical &#8230; the main difference is that methods like <b>push</b> begin with capital letters.</p>
<p>Those double-slashes, by the way, are <b>comments</b>.  They are ignored by the compiler, and are added to clarify the code.</p>
<h2>Get Carded</h2>
<p>Here&#8217;s another example.  Let&#8217;s say we&#8217;re programming a card game.  We can use an array to store our deck of cards.  Each element in the array is a card.  We use one loop to build the deck.  Then we shuffle the deck.  Finally, we use a loop to deal some cards onto the table, using the Array.shift() method to deal off the top of the deck.  This removes the cards from the array one by one, and adds them to a different array called aCardsOnTable.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> aDeck:<span style="color: #0066CC;">Array</span>; <span style="color: #808080; font-style: italic;">// declare an array to hold all the cards</span>
<span style="color: #000000; font-weight: bold;">var</span> aCardsOnTable:<span style="color: #0066CC;">Array</span>; <span style="color: #808080; font-style: italic;">// declare an array to hold the cards that we deal out</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> buildDeck<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    aDeck = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// create an empty instance of the Array class and store it in a variable called aDeck</span>
    <span style="color: #000000; font-weight: bold;">var</span> totalCards:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">52</span>; <span style="color: #808080; font-style: italic;">// we want 52 cards in the deck, so that's how many times we'll loop</span>
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>totalCards; i++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
         aDeck.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Card<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// we instantiate the Card class, and add the new card instance to the aDeck array.</span>
         <span style="color: #808080; font-style: italic;">// Once again, if we're doing too much in this line for you to follow along, here's a longer version of the same code in two steps:</span>
        <span style="color: #808080; font-style: italic;">// var card:Card = new Card(); // create the card instance and store it in a variable</span>
        <span style="color: #808080; font-style: italic;">// aDeck.push(card); // add the card instance to the aDeck array</span>
    <span style="color: #66cc66;">&#125;</span>
    shuffle<span style="color: #66cc66;">&#40;</span>aDeck<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// this is a custom function that you'll probably have to write yourself (just like the Card class).  It randomizes all of the elements in the array.</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> dealCards<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    aCardsOnTable = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// define aCardsOnTable as an empty array</span>
    <span style="color: #000000; font-weight: bold;">var</span> numCardsToDeal : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>; <span style="color: #808080; font-style: italic;">// Let's deal 5 cards to the table.</span>
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>numCardsToDeal; i++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
         aCardsOnTable.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>aDeck.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// pulls the first element out of aDeck, and adds it to aCardsOnTable</span>
         <span style="color: #808080; font-style: italic;">// That's a bit of shorthand.  Let me blow that line out into separate steps in case you're having trouble following it:</span>
         <span style="color: #808080; font-style: italic;">// var topCard:Card = aDeck.shift();  // use shift to pull out the first card in the aDeck array, and store it in a variable called topCard</span>
         <span style="color: #808080; font-style: italic;">// aCardsOnTable.push(topCard); // add the card we removed to the end of the aCardsOnTable array.</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #808080; font-style: italic;">// When we finish with this loop, there are 5 cards in the aCardsOnTable array,</span>
    <span style="color: #808080; font-style: italic;">// and 47 cards left in the aDeck array.</span>
    <span style="color: #808080; font-style: italic;">// This is a great way to deal out cards!   By removing a card from the aDeck array each time using shift(), we ensure that we'll never deal the same card twice.</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Those previous two examples are very similar. Here&#8217;s another real-world example that&#8217;s slightly different.</p>
<h2>Looping Through Movieclips on the Stage</h2>
<p>Super eggheads like to lay out their GUIs (graphical user interfaces &#8211; buttons and windows and text boxes and junk) entirely in code.  i don&#8217;t prefer to do it that way, because i&#8217;m a very visual person, and i like to <em>see</em> my interface laid out on-screen.  i like to be able to poke, prod and nudge things around visually.</p>
<p>So often, i&#8217;ll have a bunch of buttons on the stage.  The buttons all have instance names.  i&#8217;ll put those instance names in an array, and then loop through the array to add standard buttonMode, rollover and rollout effects to all the buttons in one shot.  Here&#8217;s how that looks, presuming i have the buttons &#8220;phyiscally&#8221; on my stage and named properly:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">// i call the initBtns function somewhere in my code</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> initBtns<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> aBtnNames:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;btnPlay&quot;</span>, <span style="color: #ff0000;">&quot;btnQuit&quot;</span>, <span style="color: #ff0000;">&quot;btnCredits&quot;</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// we store the instance names of my on-stage buttons in an array</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">int</span> = aBtnNames.<span style="color: #0066CC;">length</span>; <span style="color: #808080; font-style: italic;">// Let's dynamically loop through as many buttons as we have in the array.</span>
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>len; i++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
         activateBtn<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span>aBtnNames<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
         <span style="color: #808080; font-style: italic;">// Here's a slower, possibly clearer way to say that:</span>
         <span style="color: #808080; font-style: italic;">// var btnName:String = aBtnNames[i]; // Refers to the i'th element of the array</span>
         <span style="color: #808080; font-style: italic;">// var btn:MovieClip = this[btnName] // Refers to a movieclip instance on stage with that name</span>
         <span style="color: #808080; font-style: italic;">// activateBtn(btn); // Passes the reference to that button to the activateBtn function</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> activateBtn<span style="color: #66cc66;">&#40;</span>btn:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #808080; font-style: italic;">// This is standard stuff i want all of my buttons to do:</span>
     btn.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">true</span>;
     btn.<span style="color: #006600;">mouseEnabled</span> = <span style="color: #000000; font-weight: bold;">true</span>;
     btn.<span style="color: #006600;">mouseChildren</span> = <span style="color: #000000; font-weight: bold;">false</span>;
     btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, rollOverBtn<span style="color: #66cc66;">&#41;</span>;
     btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, rollOutBtn<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, an even <em>better</em> way to handle this button situation is to create your own custom button class, but we&#8217;re taking baby steps here.  That&#8217;s the thing about code &#8211; these pieces we&#8217;re learning are like Lego blocks.  There are tons of different ways to build a sculpture out of Lego.  There are no &#8220;right&#8221; ways &#8211; only <em>preferred</em> ways.  Use this <b>Understanding &#8230;</b> series to get a grip on all the blocks you have to play with, and then &#8230; it&#8217;s time to play!</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_12_01/lego.jpg" alt="Lego Escher"></p>
<p>(We&#8217;ll work our way up to this.)
</p></div>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.  </p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F01%2Funderstanding-loops-with-arrays%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F12%2F01%2Funderstanding-loops-with-arrays%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3224&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Understanding Arrays</title>
		<link>http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 07:42:22 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Understanding]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3197</guid>
		<description><![CDATA[There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages. Arrays are one of them. If a variable is a bucket that can hold one thing, an array is a superbucket that can hold many things. Variable. Array. The language you use determines [...]]]></description>
			<content:encoded><![CDATA[<p>There is a very short list of programming structures you have to learn to be reasonably comfortable in most modern object-oriented languages.  Arrays are one of them.</p>
<p>If a variable is a bucket that can hold one thing, an array is a superbucket that can hold many things.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_22/variable.jpg" alt="Variable"></p>
<p>Variable.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_22/array.jpg" alt="Array"></p>
<p>Array.
</p></div>
<p>The language you use determines what an array can hold.  Certain languages will only let you put the same type of thing in your array superbucket &#8211; &#8220;this superbucket holds only screws&#8221;, or &#8220;this superbucket holds only issues of Playboy from 1972-1981&#8243;.  Actionscript 3 lets you mix it up, putting anything you want in your array &#8211; including other arrays!</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_22/manyArrays.jpg" alt="Many arrays"></p>
<p>An AS3 array can hold many arrays, which can each hold many things &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_22/manyManyArrays.jpg" alt="Many many arrays"></p>
<p>&#8230; and you can nest things even deeper, creating an array that holds many arrays, which in turn hold many arrays.
</p></div>
<h2>Creating an Array of Your Own</h2>
<p>Let&#8217;s start simply.  To get an instance (copy) of AS3&#8242;s built-in Array class that you can play around with, use the <b>new</b> keyword:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This line of code means that we want to reserve a place in the computer&#8217;s memory to store something.  The <em>name</em> of that thing is <b>myArray</b>.  The <em>type</em> of that thing is an Array.  We&#8217;re setting the <em>value</em> of that thing using the <b>=</b> operator.  We use the <b>new</b> keyword to get a copy of the Array class, and specifying <b>Array()</b> is how we do it, building our new Array instance from the Array class&#8217;s constructor function.  The semi-colon is like a period ending the statement.</p>
<p>Be sure to check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Understanding Classes</a> tutorials to learn more about constructor functions.</p>
<p>Once we&#8217;re done, we have an instance (copy) of the Array class called myArray that we can mess with.</p>
<h2>Filling the Superbucket</h2>
<p>Now that we&#8217;ve <b>declared</b> our array, let&#8217;s <b>define</b> it by adding some elements to it.  We&#8217;ll throw in a few strings, which are <b>datatypes</b> surrounded by quotation marks.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray = <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;wookie&quot;</span>, <span style="color: #ff0000;">&quot;ewok&quot;</span>, <span style="color: #ff0000;">&quot;sarlac&quot;</span><span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p>We use those square brackets a lot when working with arrays.   </p>
<p>Now the array instance has three <b>elements</b> in it.  They&#8217;re all string literals, but they could be a mix of things &#8211; numbers, ints (whole numbers), booleans (these are like lightswitches &#8211; on or off), or other arrays.  </p>
<h2>Peering Into the Superbucket</h2>
<p>In order to find out what&#8217;s in our array, we use the name of the array followed by two square brackets, and the number of the element we want to know more about.  A semi-colon ends the line, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p>Just typing that up won&#8217;t do anything, though. The Actionscript interpreter figures out the answer, and keeps it to itself.  We need to use the built-in <b>trace</b> function to get Flash to report the value to us in the Output window.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>myArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The answer is &#8220;sarlac&#8221;.  Surprised?  Arrays are zero-based, which means you start counting from zero.  So element 0 is &#8220;wookie&#8221;, element 1 is &#8220;ewok&#8221;, and element 2 is &#8220;sarlac&#8221;.</p>
<h2>Changing the Contents</h2>
<p>If we wanted to put something at the end of this array, we could just be direct about it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;taun taun&quot;</span>;</pre></div></div>

<p>Now the array is four elements long: &#8220;wookie&#8221;, &#8220;ewok&#8221;, &#8220;sarlac&#8221;, &#8220;taun taun&#8221;.  In fact, if we just trace out the name of the array, that&#8217;s what the Output window will report to us.  Give it a try:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>myArray<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>We can knock an element out and replace it with something else just as directly:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;hutt&quot;</span>;</pre></div></div>

<p>By directly accessing element 0, we&#8217;ve knocked out &#8220;wookie&#8221; and replaced it with &#8220;hutt&#8221;.  Poor wookie.</p>
<h2>The Array Class and its Toys</h2>
<p>The Array class gives us a bunch of goodies to play with.  Like any class, it has <b>methods</b> (things that it does) and <b>properties</b> (things that it is or has). </p>
<p>Many of the Array class&#8217;s methods (things that it does) help us to put new things into the array at specific points, and pull things out of the array.  Here are a few examples:</p>
<p><b>Array.pop();</b> removes the last element from the array<br />
<b>Array.push();</b> adds an element to the end of the array<br />
<b>Array.shift();</b> removes the first element and shifts everything back by one<br />
<b>Array.unshift();</b> adds an element to the beginning of the array and shifts everything ahead<br />
<b>Array.splice();</b> lets you specify an index point and a length so you can laser-cut elements out of an array and, if you wish, replace the removed elements with new ones.  It&#8217;s like open-heart surgery for arrays.</p>
<p>One of the most commonly-used properties (things that it is/has) of an array is <b>length</b>, which just reports how many elements are in your array.  So far, myArray.length is 4.  You can see this by tracing out your array&#8217;s length property:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>myArray.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Notice that no round brackets are required.  You need to use those round brackets for methods, not properties.</p>
<h2>Using the Toys</h2>
<p>So if we wanted to add a Gungan to our array (for some ungodly reason), we could use the Array class&#8217;s push method, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;gungan&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, myArray.length is 5 &#8211; &#8220;hutt&#8221;, &#8220;ewok&#8221;, &#8220;sarlac&#8221;, &#8220;taun taun&#8221;, &#8220;gungan&#8221;.  Let&#8217;s get the wookie back at the beginning of the list by using the <b>unshift</b> method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;wookie&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, myArray.length is 6.  Here&#8217;s what myArray looks like: &#8220;wookie&#8221;, &#8220;hutt&#8221;, &#8220;ewok&#8221;, &#8220;sarlac&#8221;, &#8220;taun taun&#8221;, &#8220;gungan&#8221;.</p>
<h2>Performing Open-Heart Surgery on an Array</h2>
<p>Finally, we&#8217;ll decide to get the ewok out of there using our surgical <b>splice</b> method. Splice wants to know where we want to start cutting, how many elements we want to remove, and which elements (if any) we want to insert into the array.  Let&#8217;s keep it simple, and kill off the ewok for now.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_22/ewok.jpg" alt="Ewok"></p>
<p>Yub nub!  (Translation: &#8220;tell my wife i love her.&#8221;)
</p></div>
<p>The ewok is sitting at index 2, if we count from the beginning starting at zero.  So we tell the <b>splice</b> method that we want to start hacking and slashing at index 2, for a total of 1 elements:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now when we trace the array, the ewok is gone: &#8220;wookie&#8221;, &#8220;hutt&#8221;, &#8220;sarlac&#8221;, &#8220;taun taun&#8221;, &#8220;gungan&#8221;.  (Note that when you actually trace out your array, the Output window leaves out the string quotation marks &#8230; i&#8217;ve left them in here to remind us that we&#8217;re working with strings, not variable names.)</p>
<p>What if we wanted to remove the hutt and the sarlac?  We&#8217;d tell the splice command to start at index 1, which is where the hutt is, and remove two elements, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That means &#8220;start at element 1, and remove two elements.&#8221;  So our array is now &#8220;wookie&#8221;, &#8220;taun taun&#8221;, &#8220;gungan&#8221;.</p>
<p>Finally, we&#8217;ll use splice to get rid of that taun taun, and throw some invasive Star Trek races into the mix:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myArray.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #ff0000;">&quot;vulcan&quot;</span>, <span style="color: #ff0000;">&quot;klingon&quot;</span>, <span style="color: #ff0000;">&quot;tribble&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, we trace out the array and we get &#8220;wookie&#8221;, &#8220;vulcan&#8221;, &#8220;klingon&#8221;, &#8220;tribble&#8221;, &#8220;gungan&#8221;.  That&#8217;s because we told splice to start at index 1 (which is where taun taun was), remove one element (namely, taun taun), and insert those three other species/races at the incision point.  The transplant was a success, doctor!</p>
<h2>How Does This Help Me Build Games?</h2>
<p>Admittedly, this all seems pretty pointless when you don&#8217;t have a real-world code example.  But arrays don&#8217;t really come to life until you learn one of the other basic programming elements: loops.  In the next tutorial, <a href="http://www.untoldentertainment.com/blog/2010/11/24/understanding-loops/">Understanding Loops</a>, we&#8217;ll break down the basic structure.  Then in <a href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/">Understanding Loops with Arrays</a>, we&#8217;ll see how they play together.  Finally, i&#8217;ll throw you a few real-world code examples of how i use loops and arrays to code different pieces of my games that you can learn to use in your own projects.</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F11%2F22%2Funderstanding-arrays%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F11%2F22%2Funderstanding-arrays%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3197&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Introduction to A* (A-Star) Pathfinding in ActionScript 3 (AS3)</title>
		<link>http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 13:37:48 +0000</pubDate>
		<dc:creator>Phil Chertok</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2780</guid>
		<description><![CDATA[Introduction For Spellirium we had to develop a system that could determine the most efficient path between a series of nodes on a point. After some diligent research and careful consideration it became clear that the A* search algorithm was the way to go. Now, I was familiar with the basics behind A* but had [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>For <a href="http://www.untoldentertainment.com/blog/spellirium-designer-diary/">Spellirium</a> we had to develop a system that could determine the most efficient path between  a series of nodes on a point.  After some diligent research and careful consideration it became clear that the A* search algorithm was the way to go.</p>
<p>Now, I was familiar with the basics behind A* but had never actually implemented it.  Thankfully though I had a copy of Keith Peter’s <a href="http://www.amazon.com/AdvancED-ActionScript-Animation-Keith-Peters/dp/1430216085/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1280088548&amp;sr=8-1">Advanced ActionScript 3.0 Animation</a> which has a very nice chapter all about path finding and A*.</p>
<div class="displayed">
<p><a href="http://www.amazon.com/AdvancED-ActionScript-Animation-Keith-Peters/dp/1430216085/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1281753769&amp;sr=8-1"><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/book.jpg" alt="Advanced Actionscript 3.0 Animation" /></a></p>
<p>Why read this post?  Just buy the book instead. ;)</p>
</div>
<p>Before we begin, some prerequisite knowledge is required.  In order to follow along with this you should have a knowledge of AS3 Classes and Interfaces. It is certainly possible to implement A* without knowledge of these concepts but in order to build a flexible, reusable solution, we will be using both. Check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Understanding Classes in AS3</a> tutorials if you need help getting up to speed.</p>
<h2>What is A* path finding?</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/dofus.jpg" alt="Dofus Arena" /></p>
</div>
<p>According to the ever-so-useful <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">Wikipedia</a>,  A* path finding can be described as:</p>
<blockquote><p>(A) computer algorithm that (finds an) efficiently traversable path between points,  called nodes…. A* uses a best-first search and finds the least-cost path from a given initial node to one goal node (out of one or more possible goals).</p></blockquote>
<p>Sounds nice, but what does this mean? It means that A* chooses a node (or tile in a tile based world) searches through all the directly connected nodes and calculates a cost to travel to all of them.  It then selects the node that has the lowest cost and uses it as the starting node for the next phase of the search.  A* continues to do this until it reaches the destination.</p>
<p>To implement the algorithm it is first important to understand a few concepts:</p>
<ul>
<li><strong>Node:</strong> This is a point along a path.  In a tile-based environment you can think of it as a tile.  The reason why we don’t call it a tile is because it can be used for non-tile based worlds.   You will provide A* with a starting node and ending node.  A* will then calculate all the nodes to get from the start to the end.</li>
<li><strong>Parent Node:</strong> When a node is tested in the algorithm all of its directly connected nodes (nodes that are traversable in a single step) are assigned that node as their parent.  By doing so,  we can easily work backwards through all of the parent nodes to create our path after we reach the destination node.</li>
<li><strong>Cost:</strong> Cost is assigned to each node based on two factors.  Nodes with a lower cost are used over nodes with a higher cost.  The two parts that make up the cost are: the cost to get to that node from the starting node and the estimated cost to get from that node to the end node.  The cost of a node is usually expressed via 3 variables f, g and h.</li>
<li><strong>g:</strong> This is the cost to get to this node via the starting node.  We know this value exactly because we can follow the parent nodes all the way to the start and add up their costs.</li>
<li><strong>h:</strong> This is the cost to get from this node to the final node.  This value is estimated.  It is not exact because we don’t know the path that we will take to get to the final node.  Therefore we must estimate the cost and we do so using a function called a heuristic.</li>
<li><strong>f:</strong> This is the total cost of the specific node – it is calculated by adding g + h.</li>
<li><strong>Heuristic:</strong> This is the function that estimates the cost of getting from a particular node to the end node.  The beauty of A* is that it supports a variety of heuristics, therefore you can try out different ones until you get the result you are looking for.  Heuristic functions can differ based on their speed, efficiency and a variety of other criteria.</li>
<li><strong>Open list:</strong> This is a list of all the nodes that have been visited (in an iteration of the search) and have been assigned a cost.  The node that has the lowest cost will be used to perform the next iteration of the search.</li>
<li><strong>Closed list:</strong> This is the list of nodes whose neighbors have been visited.</li>
</ul>
<h2>The Algorithm</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/algorithm.jpg" alt="Algorithm" /></p>
</div>
<p>Now that is out of the way we can move on to the actual algorithm.  Before we actually begin coding, let&#8217;s just take a look at how A* actually works.</p>
<ol>
<li>Provide a starting node and an destination node</li>
<li>Add the starting node to our open list (which should have been empty)</li>
<li>Start our search loop:
<ul>
<li>a. Pick the node currently in the open list with the lowest cost – we will call this node the current node.  When we first run the loop this will obviously be our starting node.</li>
<li>b. If the current node is the same as the destination  then we are done  and we can move on to step 5.</li>
<li>c. Check every directly connected node. In a tile based world this would be up to 8 tiles.  For our implementation we will provide a function that determines all the connected nodes.  For each node that is connected:
<ul>
<li>i. If the node is not traversable (you can’t move to it because it Is occupied or for some other reason) or the node is already in the open list or in the closed list we can skip and move on to the next node in the list of connected nodes. Otherwise continue to ii.</li>
<li>ii. Calculate the cost of that node.</li>
<li>iii. Set the current node as the parentNode.</li>
<li>iv. Add the node to the open list.</li>
</ul>
</li>
<li>d. Add the current node to the closed list.</li>
</ul>
</li>
<li>Now return to step 3 (loop again) with the newly updated open list. (The loop should keep running until it hits the destination node)</li>
<li>We have found the destination node (hurrah!) Now we create a list of nodes that will be our path list and we will add the destination node to that list.</li>
<li>Add the parent node of the destination node to the path list.</li>
<li>Add the parent of the previous node to the path list, we will continue to do this until we have added our starting node.</li>
</ol>
<p>We should now have a list of all the nodes that make up the best path from our starting node to our destination node.</p>
<h2>The Implementation</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/sleeves.jpg" alt="Roll up your sleeves" /></p>
</div>
<p>Now that our theory is out of the way, let’s start writing some code.  First we will start with our Node class.  For this  I will actually create an interface for your node:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> INode
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> f<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> g<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> h<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> x<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> y<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">parentNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:INode;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> traversable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> f<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> g<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> h<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">parentNode</span><span style="color: #66cc66;">&#40;</span>value:INode<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> traversable<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This is a pretty straight-forward interface.  It includes the variables for all the properties we discussed above (f,g,h and parentNode ).  The other variables are fairly self-explanatory.  X and Y obviously indicate the position of the node. We will use these values in our heuristic to estimate the cost of traveling from the node to the destination.  The traversable variable is a simple Boolean that indicates if the node can be used in our path finding.  If it is not then the algorithm will simply skip it when it is encountered.  This is useful in a world where a node might be occupied by a prop (say a rock, tree or wall) or if there is another player/character occupying that space and you don’t want to support multiple characters on a single node.  Implementing this in a class should be very easy.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/pathfinder.jpg" alt="Nissan Pathfinder" /></p>
</div>
<p>Now we will build a class called Pathfinder.  We will create the class only using static methods.  Hopefully this will make it very flexible.  It will not take much to turn this into a standard class but for our purposes static methods will do very well.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pathfinder
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> heuristic:<span style="color: #000000; font-weight: bold;">Function</span> = Pathfinder.<span style="color: #006600;">diagonalHeuristic</span>;</pre></div></div>

<p>Here, we define the heuristic function we will use to estimate the cost of traveling from a node to the end.  In this example we will use the diagonal heuristic.  At the end of this article, we will discuss some other heuristics and how to implement them.</p>
<p>Let’s move on to our actual path finding function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findPath<span style="color: #66cc66;">&#40;</span>firstNode:INode, destinationNode:INode, connectedNodeFunction:<span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> 	<span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>Let’s examine the parameters:</p>
<ul>
<li>firstNode: INode – this is the first node in our path</li>
<li>destinationNode: INode – The is the node that we are going to be traveling to</li>
<li>connectedNodeFunction:Function – This is a function that will return an array of nodes that are connected to a given node.</li>
</ul>
<p>Since each implementation might be different we abstract it out this way.  After we write our class I will use a tile- based example to illustrate how this works, but your method might be different.</p>
<p>Lastly, the function returns an Array. This will contain all the nodes along the path.</p>
<h2>Variable Setup</h2>
<p>So now that is out of the way let’s do some initial work:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> openNodes:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">var</span> closedNodes:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">var</span> currentNode:INode = firstNode;
<span style="color: #000000; font-weight: bold;">var</span> testNode:INode;
<span style="color: #000000; font-weight: bold;">var</span> connectedNodes:<span style="color: #0066CC;">Array</span>;
<span style="color: #000000; font-weight: bold;">var</span> travelCost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span>;
<span style="color: #000000; font-weight: bold;">var</span> g:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> h:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> f:<span style="color: #0066CC;">Number</span>;
currentNode.<span style="color: #006600;">g</span> = <span style="color: #cc66cc;">0</span>;
currentNode.<span style="color: #006600;">h</span> = Pathfinder.<span style="color: #006600;">heuristic</span><span style="color: #66cc66;">&#40;</span>currentNode, destinationNode, travelCost<span style="color: #66cc66;">&#41;</span>;
currentNode.<span style="color: #006600;">f</span> = currentNode.<span style="color: #006600;">g</span> + currentNode.<span style="color: #006600;">h</span>;
<span style="color: #000000; font-weight: bold;">var</span> l:<span style="color: #0066CC;">int</span> = nodes.<span style="color: #0066CC;">length</span>;
<span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;</pre></div></div>

<p>The first two arrays will hold our open nodes and our closed nodes.  Then we have a variable <strong>currentNode</strong> which will keep track of the node we are currently on.  We set this to be our firstNode since that is where we will start.</p>
<p>Next we keep a variable <strong>testNode:INode</strong>, which will be the node that will be a connected node of our current node.</p>
<p>Next we have <strong>g</strong>,<strong>h</strong> and <strong>f</strong> values we will keep track of for each node.</p>
<p>We then set the initial <strong>g</strong> value of the currentNode (which is also our first node) to 0 since it does not cost anything to get there since it is already our current location.</p>
<p>Then, we calculate the estimated cost to reach the end using our heuristic, and finally the final cost which is calculated by adding <strong>g</strong> + <strong>h</strong> (the starting cost plus the final cost).</p>
<p>Lastly, we have some variables that we will use inside of loops in our A* algorithm.</p>
<h2>The Loop</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/loop.jpg" alt="Roller coaster loop" /></p>
</div>
<p>Let’s move on to the actual loop.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>currentNode <span style="color: #66cc66;">!</span>= destinationNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	connectedNodes = connectedNodeFunction<span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
	l = connectedNodes.<span style="color: #0066CC;">length</span>;
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> l; ++i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		testNode = connectedNodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode == currentNode <span style="color: #66cc66;">||</span> testNode.<span style="color: #006600;">travesable</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;
		g = currentNode.<span style="color: #006600;">g</span>  + travelCost;
		h = heuristic<span style="color: #66cc66;">&#40;</span> testNode, destinationNode, travelCost<span style="color: #66cc66;">&#41;</span>;
		f = g + h;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> Pathfinder.<span style="color: #006600;">isOpen</span><span style="color: #66cc66;">&#40;</span>testNode, openNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> Pathfinder.<span style="color: #006600;">isClosed</span><span style="color: #66cc66;">&#40;</span> testNode, closedNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">f</span> <span style="color: #66cc66;">&gt;</span> f<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
&nbsp;
				testNode.<span style="color: #006600;">f</span> = f;
				testNode.<span style="color: #006600;">g</span> = g;
				testNode.<span style="color: #006600;">h</span> = h;
				testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			testNode.<span style="color: #006600;">f</span> = f;
			testNode.<span style="color: #006600;">g</span> = g;
			testNode.<span style="color: #006600;">h</span> = h;
			testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
			openNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>testNode<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	closedNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openNodes.<span style="color: #0066CC;">length</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
	openNodes.<span style="color: #0066CC;">sortOn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'f'</span>, <span style="color: #0066CC;">Array</span>.<span style="color: #006600;">NUMERIC</span><span style="color: #66cc66;">&#41;</span>;
	currentNode = openNodes.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as INode;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Whoa! There’s a lot going on there, so let’s try to break it down piece by piece.</p>

<div class="wp_syntax"><div class="code"><pre class="actioncript" style="font-family:monospace;">while (currentNode != destinationNode) {</pre></div></div>

<p>Start our loop.  If our current node is the same as our destination then we are done and we can build our path!</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">connectedNodes = connectedNodeFunction<span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
l = connectedNodes.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> l; ++i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>So fetch an array of all of the connected nodes of our current node.  Store the length and then loop through all of them.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">testNode = connectedNodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode == currentNode <span style="color: #66cc66;">||</span> testNode.<span style="color: #006600;">travesable</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;</pre></div></div>

<p>Test each node out of the connected nodes.  If the node is the same as our current node or it is not traversable, move on to the next connected node.</p>
<p><strong>Note:</strong> Your connectedNodeFunction might omit non traversable nodes which would make the above line redundant and unnecessary.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">g = currentNode.<span style="color: #006600;">g</span>  + travelCost;
h = heuristic<span style="color: #66cc66;">&#40;</span> testNode, destinationNode, travelCost<span style="color: #66cc66;">&#41;</span>;
f = g + h;</pre></div></div>

<p>Ok, so now we are calculating the actual cost of our node.  First,  <strong>g</strong>:</p>
<h2>g</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/aliG.jpg" alt="Ali G" /></p>
</div>
<p>In a tile based world, we simply add the cost of the <strong>currentNode</strong> to the <strong>travelCost</strong> to determine the cost of the tile.  In some tile based environments, a diagonal move sometimes cost more than other moves.  In this case you would need to determine if the <strong>testNode</strong> is diagonal to the <strong>currentNode</strong> and adjust the <strong>travelCost</strong> value accordingly.</p>
<p>In our case here at Untold, we had a situation where the cost of each connected node was not the same because the nodes could be spread out across variable distances. So in our case we changed the <strong>g</strong> calculation to use our heuristic between the <strong>currentNode</strong> and the <strong>testNode</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">g  = currentNode.<span style="color: #006600;">g</span> + Pathfinder.<span style="color: #006600;">heuristic</span><span style="color: #66cc66;">&#40;</span> currentNode, testNode, travelCost<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now, h:</p>
<h2>h</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/heroin.jpg" alt="Horse" /></p>
</div>
<p>This is where we use our heuristic.  Our heuristic takes in our <strong>testNode</strong> and our destination node. We also supply the cost to the heuristic which will return a number.  Again, we will go over the heuristic later.</p>
<h2>f</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/f.jpg" alt="f" /></p>
</div>
<p>Finally, we are at <strong>f</strong>.  This is a straightforward calculation to determine the total cost of the node.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> Pathfinder.<span style="color: #006600;">isOpen</span><span style="color: #66cc66;">&#40;</span>testNode, openNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> Pathfinder.<span style="color: #006600;">isClosed</span><span style="color: #66cc66;">&#40;</span> testNode, closedNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#123;</span></pre></div></div>

<p>Here, we use some simple utility functions to determine if the node is in either our open list or our closed list.  If it is, then we should only adjust its <strong>f</strong>,<strong>g</strong> and <strong>h</strong> value if its current cost (<strong>f</strong>) is greater than the cost we just calculated.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">f</span> <span style="color: #66cc66;">&gt;</span> f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		testNode.<span style="color: #006600;">f</span> = f;
		testNode.<span style="color: #006600;">g</span> = g;
		testNode.<span style="color: #006600;">h</span> = h;
		testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
	<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		testNode.<span style="color: #006600;">f</span> = f;
		testNode.<span style="color: #006600;">g</span> = g;
		testNode.<span style="color: #006600;">h</span> = h;
		testNode.<span style="color: #0066CC;">parentNode</span> = currentNode;
		openNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>testNode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If the node is not in either the open list or in the closed list, then we definitely want to assign it the values we just calculated.  We also want to add it to the list of open nodes and make sure its parent is set to our <strong>currentNode</strong>.</p>
<p>We are now done our loop and can add our <strong>currentNode</strong> to our list of closed nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">closedNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> currentNode <span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>openNodes.<span style="color: #0066CC;">length</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
openNodes.<span style="color: #0066CC;">sortOn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'f'</span>, <span style="color: #0066CC;">Array</span>.<span style="color: #006600;">NUMERIC</span><span style="color: #66cc66;">&#41;</span>;
currentNode = openNodes.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as INode;</pre></div></div>

<p>If there are no more open nodes, then no path is available between the provided nodes, so we return NULL.</p>
<p>Otherwise, sort our open nodes based on their cost and use the one with the lowest cost as our new <strong>currentNode</strong>.</p>
<p>If a path is available, the <strong>currentNode</strong> will eventually be the same as our destination node, and the <em>while</em> loop will exit.  At this point, we will be ready to build our path and end our function which we do with a single line:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">return</span> Pathfinder.<span style="color: #006600;">buildPath</span><span style="color: #66cc66;">&#40;</span>destinationNode, firstNode<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Of course this means we need a function called <strong>buildPath()</strong>.  Luckily, it is quite simple and looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> buildPath<span style="color: #66cc66;">&#40;</span>destinationNode:INode, startNode:INode<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> path:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> node:INode = destinationNode;
	path.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>node <span style="color: #66cc66;">!</span>= startNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		node = node.<span style="color: #0066CC;">parentNode</span>;
		path.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span> node <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> path;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In this function, we simply keep moving through all the parent nodes of the provided nodes and put them at the front of a new array we return to the caller.</p>
<h2>The Heuristic</h2>
<p>We are almost ready to test out our class with an example but as promised we will first discuss our heuristic.</p>
<p>There is a wide range of heuristics used to calculate an optimum path.  The methodology is based on a variety of criteria.  Different heuristics return different results and you will need to find the one that is best for your case.  Right now, we’ll examine three simple heuristics, but don’t be afraid to go out and find your own.</p>
<p>Now, I was familiar with the basics behind A* but had never actually implemented it.  Thankfully though I had a copy of Keith Peter’s <a href="http://www.amazon.com/AdvancED-ActionScript-Animation-Keith-Peters/dp/1430216085/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1280088548&amp;sr=8-1">Advanced ActionScript 3.0 Animation</a> which has a very nice chapter all about path finding and A*.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/manhattan.jpg" alt="Manhattan A* Heuristic" /></p>
</div>
<p><strong>Manhattan:</strong> This heuristic is the most simple.  It ignores any diagonal movement, so if your world does not allow for diagonal movement it might be a good fit. It simply adds the total number of rows and columns between the two provided nodes.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> manhattan<span style="color: #66cc66;">&#40;</span>node:INode, destinationNode:INode, cost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">x</span> – destinationNode.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> cost + <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">y</span> + destinationNode.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> cost;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/euclid.jpg" alt="Euclidian A* Heuristic" /></p>
</div>
<p><strong>Euclidian:</strong> This heuristic simply draws a straight line between the two provided nodes and returns its length.  You might recall the Pythagorean Theorem from math class?  Well, here it is in action.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> euclidianHeuristic<span style="color: #66cc66;">&#40;</span>node:INode, destinationNode:INode, cost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> dx:<span style="color: #0066CC;">Number</span> = node.<span style="color: #006600;">x</span> - destinationNode.<span style="color: #006600;">x</span>;
	<span style="color: #000000; font-weight: bold;">var</span> dy:<span style="color: #0066CC;">Number</span> = node.<span style="color: #006600;">y</span> - destinationNode.<span style="color: #006600;">y</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span> dx <span style="color: #66cc66;">*</span> dx + dy <span style="color: #66cc66;">*</span> dy <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> cost;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/diagonAlley.jpg" alt="Diagonal A* Heuristic" /></p>
</div>
<p><strong>Diagonal:</strong> This heuristic is the most accurate of the three.  It is more complex than the previous two, but runs fewer times because of its accuracy.  It takes into account a diagonal cost, if it is more expensive than a regular movement.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> diagonalHeuristic<span style="color: #66cc66;">&#40;</span>node:INode, destinationNode:INode, cost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span>, diagonalCost:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> dx:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">x</span> - destinationNode.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> dy:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">y</span> - destinationNode.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> diag:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">min</span><span style="color: #66cc66;">&#40;</span> dx, dy <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> straight:<span style="color: #0066CC;">Number</span> = dx + dy;
	<span style="color: #b1b100;">return</span> diagonalCost <span style="color: #66cc66;">*</span> diag + cost <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>straight - <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">*</span> diag<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Make it Go</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_08_13/rocket.jpg" alt="Rocket" /></p>
</div>
<p>Now that we are done with our pathfinding system, we can put it to work.   Take a look at the example below.  You can click on any two tiles in the grid and then we will then draw a path between the two tiles.  You can change the heuristic at any point to see the different results you might get.  Also highlighted are any tiles that the pathfinder tests.  Notice that the Manhattan heuristic tests far more nodes than the other two.</p>
<p>
<object width="640" height="480">
<param name="movie" value="http://www.untoldentertainment.com/blog/img/2010_08_13/pathfinding_example.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<param name="allowScriptAccess" value="always"></param>
<embed type="application/x-shockwave-flash" width="640" height="480" src="http://www.untoldentertainment.com/blog/img/2010_08_13/pathfinding_example.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
</p>
<p>I implement the pathfinding simply by calling the <strong>Pathfinder.findPath()</strong> function and passing the start node, end node and the function that determines connected nodes.  I then pass the returned array to a function called <strong>drawPath</strong>. For your usage you probably want to do a lot more than draw a path, you probably want to have a character traverse along that path, but this is just meant to show you how to acquire which path to follow.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">drawPath<span style="color: #66cc66;">&#40;</span> Pathfinder.<span style="color: #006600;">findPath</span><span style="color: #66cc66;">&#40;</span>_startNode, _endNode, findConnectedNodes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The findConnectedNodes method is very straightforward.  It simply returns all nodes that are adjacent to a given node.  You can check it out here:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findConnectedNodes<span style="color: #66cc66;">&#40;</span> node:INode <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> n:Node = node as Node;
	<span style="color: #000000; font-weight: bold;">var</span> connectedNodes:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> testNode:Node;
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> _nodes.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		testNode = _nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">row</span> <span style="color: #66cc66;">&lt;</span> n.<span style="color: #006600;">row</span> - <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">||</span> testNode.<span style="color: #006600;">row</span> <span style="color: #66cc66;">&gt;</span> n.<span style="color: #006600;">row</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>testNode.<span style="color: #006600;">col</span> <span style="color: #66cc66;">&lt;</span> n.<span style="color: #006600;">col</span> - <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">||</span> testNode.<span style="color: #006600;">col</span> <span style="color: #66cc66;">&gt;</span> n.<span style="color: #006600;">col</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">continue</span>;
		connectedNodes.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> testNode <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> connectedNodes;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, this implementation will not be sufficient if you want different tiles to have different costs based on terrain (quicksand might be more expensive than grass), but if you can grasp the above you should be able to make the adjustments fairly easily.</p>
<p>Hopefully you will now have a solid understanding of A* path finding and you can now use it in your own applications.</p>
<p>Click <a href="http://www.untoldentertainment.com/blog/labs/pathfinding/pathfinding_example.zip">here</a> to download the source code for this tutorial.</p>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/"><img src="http://www.untoldentertainment.com/blog/img/features/flash911/flash911.gif" alt="Flash and Actionscript 911" /></a></p>
<p>This article is part of our ongoing <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> Series, which is awesome.</p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F08%2F20%2Fintroduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F08%2F20%2Fintroduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2780&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/08/20/introduction-to-a-a-star-pathfinding-in-actionscript-3-as3-2/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>5 Ways to Cheat at Actionscript</title>
		<link>http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 13:13:50 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2248</guid>
		<description><![CDATA[As i see it, there are two ways to approach programming in Flash with Actionscript: the right way, and the &#8220;this thing is due at the end of the week&#8221; way. Throughout my career, i haven&#8217;t enjoyed the luxury of big budgets and generous timelines. The broadcasters are cheap, the teevee producers work mostly on [...]]]></description>
			<content:encoded><![CDATA[<p>As i see it, there are two ways to approach programming in Flash with Actionscript: the right way, and the &#8220;this thing is due at the end of the week&#8221; way.  Throughout my career, i haven&#8217;t enjoyed the luxury of big budgets and generous timelines. The broadcasters are cheap, the teevee producers work mostly on a borrowed dime, and the ad agencies (and everyone else) want to pocket as much as possible for themselves.  The result is that a successful independent Flash vendor needs to turn things around FAST.</p>
<p>So while i&#8217;m getting to know and understand the best practices of Object-Oriented programming, there are still some tried-and-true methods of finishing work more quickly that don&#8217;t obey strict OOP protocol.  What follows is a list of pragmatic programming tips &#8211; ways to bend the rules, or to leverage the visual tools in the Flash IDE &#8211; to speed things up, and to make life easier.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/theCheat.jpg"></p>
</div>
<h2>Cheat #1: The Injection Method</h2>
<p>The fact that the programming eggheads, the <em>nerderati</em>, have coined a term for this strategy means it can&#8217;t be all that bad.  The injection method breaks one of the pillars of OOP &#8211; encapsulation &#8211; but it&#8217;s a Hell of a time-saver.</p>
<p>Let&#8217;s say you&#8217;re building an adventure game.  You have an Inventory class and your MainGame class.  When the player combines the &#8220;dynamite&#8221; inventory with the &#8220;hammer&#8221; inventory item, that action might trigger a death animation.   The way you&#8217;ve built it, your MainGame class handles all death sequences. </p>
<p>According to the eggheads, the &#8220;right&#8221; way to do this is for MainGame to instantiate Inventory.  In order for Inventory to talk back to MainGame, it should dispatch a custom event, which MainGame listens for:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainGame <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">var</span> inventory:Inventory = <span style="color: #000000; font-weight: bold;">new</span> Inventory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          inventory.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Inventory.<span style="color: #006600;">DEADLY_ITEM_COMBINATION</span>, playDeathSequence<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> playDeathSequence<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
           <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You have died, because i'm a lousy game developer and i couldn't
                         think of a creative way to solve this situation without screeching
                          the game to a dead stop.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Here&#8217;s what the Inventory class looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Inventory
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const DEADLY_ITEM_COMBINATION:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;deadlyItemCombination&quot;</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Inventory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// Setup stuff goes here - etc etc.</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> combineItems<span style="color: #66cc66;">&#40;</span>item1:Item, item2:Item<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
           <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>item1.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;hammer&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> item2.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;dynaimte&quot;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
                 dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>DEADLY_ITEM_COMBINATION<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>i find it a very time-consuming process to set this up, especially if you need a lot of communication between the two classes.  The other asspain is that you can&#8217;t specify <em>which</em> deadly item combination triggered the death sequence. What if there is a special death sequence for any dynamite-related inventory screw-up, and a different sequence for any misstep involving the bagOSnakes item?  Sure, you can set up some kind of custom event class, but that takes even MORE time, and you&#8217;re on a deadline.</p>
<p>With the injection method, you pass a reference to the MainGame class into the Inventory class.  Then Inventory can invoke any public methods and properties on MainGame.  It breaks encapsulation, sure, but like i said, you&#8217;ve got until Friday to make this work, and you probably won&#8217;t end up reusing your Inventory class anyway.  (Certainly not NOW, you won&#8217;t ;)</p>
<p>Here&#8217;s how it looks:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainGame <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">var</span> inventory:Inventory = <span style="color: #000000; font-weight: bold;">new</span> Inventory<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// pass , or &quot;inject&quot;, a reference to the </span>
                                                                <span style="color: #808080; font-style: italic;">// MainGame class to inventory</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playDeathSequence<span style="color: #66cc66;">&#40;</span>typeOfDeath:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// inventory calls this public method on MainGame when the time is right </span>
           <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>typeOfDeath == <span style="color: #ff0000;">&quot;dynamiteRelated&quot;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #808080; font-style: italic;">// Play the dynamite death animation</span>
          <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>typeOfDeath == <span style="color: #ff0000;">&quot;bagOSnakesRelated&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #808080; font-style: italic;">// Play the bag o' snakes death animation</span>
          <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Inventory
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> main:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// A reference to the MainGame class</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Inventory<span style="color: #66cc66;">&#40;</span>main:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
         <span style="color: #0066CC;">this</span>.<span style="color: #006600;">main</span> = main; <span style="color: #808080; font-style: italic;">// Set the instance variable main to whatever's passed into the constructor</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> combineItems<span style="color: #66cc66;">&#40;</span>item1:Item, item2:Item<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
           <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>item1.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;hammer&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> item2.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;dynaimte&quot;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
                 main.<span style="color: #006600;">playDeathSequence</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;dynamiteRelated&quot;</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>item1.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;bagOSnakes&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> item2.<span style="color: #006600;">itemType</span> == <span style="color: #ff0000;">&quot;tickleFeather&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                 main.<span style="color: #006600;">playDeathSequence</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bagOSnakesRelated&quot;</span><span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, with anything in programming, there are many ways to skin a cat.  If you only ever have one Inventory in your game, you might even make it a static class.  The eggheads are going to crucify me for my abuse of the injection method, but that&#8217;s alright &#8211; i&#8217;ve got six dirt-cheap preschool games to build by Friday.  i welcome crucifixion with open arms, as it were.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/dynamite.jpg"></p>
<p>And BOOM! goes the trinitrotoluene
</p></div>
<h2>Cheat #2: Dispatching Events from the Timeline</h2>
<p>The architects of Actionscript 3 pulled kind of a dick move by rendering the timeline incredibly difficult to deal with.  One of the best things about Flash was the amount of complicated graphics and animation stuff it did for you right out of the box, but AS3 robbed us of productivity by making us write a lot of code when we shouldn&#8217;t have to.</p>
<p>This disadvantage rears its ugly head when you need to wait for a MovieClip to finish playing its animation.  One way to deal with this is to listen to the clip&#8217;s ENTER_FRAME or EXIT_FRAME events, and wait until it hits its last frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> groovyMovie:GroovyMovie = <span style="color: #000000; font-weight: bold;">new</span> GroovyMovie<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>groovyMovie<span style="color: #66cc66;">&#41;</span>;
groovyMovie.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">EXIT_FRAME</span>, checkCompletion<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> checkCompletion<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	 <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">currentFrame</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">totalFrames</span><span style="color: #66cc66;">&#41;</span>
	 <span style="color: #66cc66;">&#123;</span>
		 <span style="color: #808080; font-style: italic;">// MovieClip has reached the end of its timeline.</span>
	 <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>That&#8217;s fine.  But what happens if the clip contains a number of separate animation sequences?  You can write a conditional statement inside the checkCompletion() method that looks at the currentLabel property, but that makes your code even thicker:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> groovyMovie:GroovyMovie = <span style="color: #000000; font-weight: bold;">new</span> GroovyMovie<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>groovyMovie<span style="color: #66cc66;">&#41;</span>;
groovyMovie.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">EXIT_FRAME</span>, checkCompletion<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> checkCompletion<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">var</span> clip:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	 <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>clip.<span style="color: #006600;">currentLabel</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;finishedJumping&quot;</span>:
                     doSomethingAfterFinishingJumping<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                     <span style="color: #b1b100;">break</span>;
                <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;finishedGyrating&quot;</span>:
                     doSomethingAfterFinishingGyrating<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                     <span style="color: #b1b100;">break</span>;
                <span style="color: #000000; font-weight: bold;">default</span>:
                     <span style="color: #b1b100;">break</span>;
         <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Putting code on the timeline is dirty and bad.  Occasionally, i&#8217;ll throw in some timeline events (gotoAndPlay(), stop(), etc) but that&#8217;s the extent of it.  Except in this situation.  In this sitaution, if it helps speed things up, i&#8217;ll advocate slipping in a dispatchEvent() action on the timeline.</p>
<p>On the salient frame of GroovyMovie:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;finishedGyrating&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And here&#8217;s our modified code from elsewhere:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> groovyMovie:GroovyMovie = <span style="color: #000000; font-weight: bold;">new</span> GroovyMovie<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>groovyMovie<span style="color: #66cc66;">&#41;</span>;
groovyMovie.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;finishedGyrating&quot;</span>, doSomethingAfterFinishingGyrating<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>No more messy switch statement and &#8211; maybe the eggheads can confirm or deny this &#8211; i think the code may speed up, because you&#8217;re no longer checking that conditional statement every frame.  (i&#8217;ve never cottoned on to the inner workings of the virtual machine, so if i&#8217;m wrong about that, feel free to nerdspank me.)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/spanking.jpg"></p>
<p>i&#8217;ve been a baaaaad programmer, and i deserve to be punished.
</p></div>
<h2>Cheat #3: Positioning Markers on the Stage</h2>
<p>One of the most difficult things about AS3 for me, a visual person, is that i suddenly have to lay visual elements like buttons and text fields out in code, instead of on the stage.  You get a lot more control over your elements when you manipulate them with code only, and some stuff just downright doesn&#8217;t work <em>unless</em> you work solely with code.</p>
<p>What i can&#8217;t stand is not knowing where things are going to end up on the stage.  i start by positioning something:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">widget.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">173</span>;
widget.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">221</span>;</pre></div></div>

<p>Then i compile.  When the code places my element on the stage, i soon see it&#8217;s off by a longshot. So i go back to the drawing board:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">widget.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">192</span>;
widget.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">237</span>;</pre></div></div>

<p>Compile. Check.  Damn &#8211; this time, i&#8217;ve overshot the mark.  i repeat this process &#8211; set x/y, compile, check, close swf &#8211; until it&#8217;s in the right place.  And then i do the same with some other element.  It&#8217;s aggravating.</p>
<p>i&#8217;ve found one of the fastest ways to cut through this is to create an empty MovieClip in your library and call it &#8220;Empty&#8221;.  Drag it out to the stage where you want the code-created element to exist.  Give it the instance name &#8220;marker&#8221;, or whatever.  Then in your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">widget.<span style="color: #006600;">x</span> = marker.<span style="color: #006600;">x</span>;
widget.<span style="color: #006600;">y</span> = marker.<span style="color: #006600;">y</span>;</pre></div></div>

<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/empty.jpg"></p>
<p>Nothing up my sleeve &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/marker.jpg"></p>
<p>Give the clip the instance name &#8220;marker&#8221; and position your elements to it.
</p></div>
<p>Later, if you find out you got it wrong, don&#8217;t painstakingly tweak the numbers in code.  Just grab the marker instance and drag it somewhere else on the stage.  Step 4: profit.</p>
<h2>Cheat #4: Stealing Color Effect Data</h2>
<p>i hope i&#8217;m not the only one, but classes like ColorTransform confuse the heck out of me.  ColorTransform takes <em>eight</em> arguments:</p>
<p>ColorTransform(redMultiplier:Number = 1.0, greenMultiplier:Number = 1.0, blueMultiplier:Number = 1.0, alphaMultiplier:Number = 1.0, redOffset:Number =0, greenOffset:Number =0, blueOffset:Number = 0, alphaOffset:Number = 0)</p>
<p>Whenever i need a specific outcome from the ColorTransform class, i put a MovieClip on the stage and adjust the Color Effect properties in the Properties panel.  If you choose &#8220;Advanced&#8221; and play with the dials, you may notice that there are eight different parameters you can tweak.  These eight params map onto the eight arguments that the ColorTransform class expects.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/colorTransform.jpg"></p>
<p>The numbers in the left column are the multipliers. The numbers in the right column are the offsets.
</p></div>
<p>Here&#8217;s the corresponding code, taken from the numbers on the dials. The offset parameters are percentages, so remember to divide the first four offset numbers by 100 (just throw a decimal place in front of them):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ct:ColorTransform = <span style="color: #000000; font-weight: bold;">new</span> ColorTransform<span style="color: #66cc66;">&#40;</span>.29, .38, .33, .77, <span style="color: #cc66cc;">82</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">72</span>, <span style="color: #cc66cc;">51</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It&#8217;s not exactly a &#8220;cheat&#8221;, but it does get around this notion that we need to fight tooth and nail to imagine everything through code.</p>
<h2>Cheat #5: Guided Layers</h2>
<p>This last one is also less of a cheat, and more of a way to get stuff out of codespace and into the more visual Flash environment.</p>
<p>If you&#8217;ve ever had to open another programmer&#8217;s code and Flash file, you may have been faced with a blank screen.  All of the elements are linked in the library, but you don&#8217;t know where they&#8217;re supposed to go or how they&#8217;re supposed to appear without compiling the movie.  And if you&#8217;ve been hired to fix an incomplete project and the fla won&#8217;t compile, you <em>could</em> be in for a world of hurt.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/blank.jpg"></p>
<p>oh, GREAT.
</p></div>
<p>This is usually the path you follow to figure out how the file&#8217;s been built:</p>
<ol>
<li>Check for timeline code in the first frame.
<li>Check for a document class.
<li>Check the library for linkage names to see what gets added to the stage dynamically
</ol>
<p>In order to be kind to Future-Me, or to anyone who inherits my code if i get hit by a bus, i use guided layers.  If you put something on a layer, and then right-click the layer and hit &#8220;Guide&#8221;, that thing won&#8217;t show up when you compile.  It&#8217;s MUCH friendlier to open up a file and see elements on the stage, so you get <em>some</em> idea of what&#8217;s supposed to appear in the file, instead of seeing a blank screen.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/guide.jpg"></p>
<p>Don&#8217;t be cruel. Put dynamically-linked stuff on a guided layer.
</p></div>
<blockquote><p>Important note: If you put code on your timeline (and in nearly all cases, you shouldn&#8217;t), your code will still fire even if you Guide out the layer that it&#8217;s on.</p></blockquote>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_03_01/layers.jpg"></p>
<p>The stuff on the graphics layer won&#8217;t show up, but the actions on the code layer will still fire, even though the layer is guided.
</p></div>
<h2>Speed Kills</h2>
<p>So those are a few ways that i &#8220;cheat&#8221; by leveraging the Flash IDE or using &#8220;good-enough&#8221; practices instead of &#8220;best&#8221; practices to speed through tight timelines.  Do you have any less-than-perfect strategies to get around writing mountains of code, or to make things easier on yourself?  i&#8217;d love hear them!</p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F03%2F01%2F5-ways-to-cheat-at-actionscript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F03%2F01%2F5-ways-to-cheat-at-actionscript%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2248&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Five Common AS3 to UnityScript Translations</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/25/five-common-as3-to-unityscript-translations/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/25/five-common-as3-to-unityscript-translations/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:05:06 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Unity3D]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1832</guid>
		<description><![CDATA[i&#8217;ve been using Unity3D on and off for a week now. i&#8217;d burned through about a dozen beginner tutorials, and am a little overwhelmed by how much i don&#8217;t know about the engine, which enables you to create 3D games to deploy to a number of platforms, including the iPhone and web browsers. i&#8217;d look [...]]]></description>
			<content:encoded><![CDATA[<p>i&#8217;ve been using Unity3D on and off for a week now.  i&#8217;d burned through about a dozen beginner tutorials, and am a little overwhelmed by how much i <em>don&#8217;t</em> know about the engine, which enables you to create 3D games to deploy to a number of platforms, including the iPhone and web browsers.</p>
<p>i&#8217;d look like a fool if i tried to write a tutorial at this point. So instead, i&#8217;ll share this list of translations of common Actionscript 3 coding tasks to the Unity3D engine&#8217;s JavaScript-like UnityScript language.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_25/flashToUnity.jpg" alt="Flash to Unity"></p>
</div>
<h2>1. Render an Object Visible or Invisible</h2>
<p><b>Actionscript 3:</b></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">thing.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #808080; font-style: italic;">// or false</span></pre></div></div>

<p><b>UnityScript:</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">thing.<span style="color: #660066;">renderer</span>.<span style="color: #660066;">enabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// or false</span></pre></div></div>

<h2>2. Tint an Object</h2>
<p><b>Actionscript 3:</b></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> colorTransform:ColorTransform = <span style="color: #000000; font-weight: bold;">new</span> ColorTransform<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
colorTransform.<span style="color: #0066CC;">color</span> = 0xFF0000;
thing.<span style="color: #006600;">transform</span>.<span style="color: #006600;">colorTransform</span>=colorTransform;</pre></div></div>

<p><b>UnityScript:</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">thing.<span style="color: #660066;">renderer</span>.<span style="color: #660066;">material</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> Color.<span style="color: #660066;">red</span><span style="color: #339933;">;</span></pre></div></div>

<h2>3. Get a Random Number</h2>
<p><b>Actionscript 3:</b></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someRandomNumberBetween0And5:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><b>UnityScript:</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> someRandomNumberBetween0And5<span style="color: #339933;">:</span>int <span style="color: #339933;">=</span> Random.<span style="color: #660066;">Range</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>4. Respond to a Key Press</h2>
<p><b>Actionscript 3:</b></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">&nbsp;
<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, doKeyDown<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doKeyDown<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">32</span>:
			<span style="color: #808080; font-style: italic;">// Space bar! Do stuff.</span>
			<span style="color: #b1b100;">break</span>;
		<span style="color: #000000; font-weight: bold;">default</span>:
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><b>UnityScript:</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>Input.<span style="color: #660066;">GetKeyDown</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;space&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Do stuff.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>5. Open a Web Page</h2>
<p><b>Actionscript 3:</b></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">navigateToURL<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">URL</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://www.untoldentertainment.com&quot;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;_self&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><b>UnityScript:</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Application.<span style="color: #660066;">OpenURL</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.untoldentertainment.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/unity-nuub/"><img src="http://www.untoldentertainment.com/blog/img/features/unityNuub/unityNuub.jpg"></a></p>
</div>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F25%2Ffive-common-as3-to-unityscript-translations%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F25%2Ffive-common-as3-to-unityscript-translations%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/25/five-common-as3-to-unityscript-translations/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1832&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/25/five-common-as3-to-unityscript-translations/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Tutorial &#8211; Understanding Classes in AS3 Part 4</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/24/tutorial-understanding-classes-in-as3-part-4/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/24/tutorial-understanding-classes-in-as3-part-4/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 13:25:25 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1787</guid>
		<description><![CDATA[In this tutorial, you&#8217;ll learn about the OOP (Object-Oriented Programming) concept of encapsulation, and how to break encapsulation to get stuff done in a decent amount of time. Technical Correctness vs. Getting Sh*t Done i&#8217;ve been really happy with the way this series has been received. If you&#8217;re like me, you&#8217;ve had a hard time [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, you&#8217;ll learn about the OOP (Object-Oriented Programming) concept of <em>encapsulation</em>, and how to break encapsulation to get stuff done in a decent amount of time.</p>
<h2>Technical Correctness vs. Getting Sh*t Done</h2>
<p>i&#8217;ve been really happy with the way this series has been received. If you&#8217;re like me, you&#8217;ve had a hard time understanding the big deal about OOP and Classes &#8211; you know it&#8217;s something you&#8217;ve got to bite the bullet and eventually learn, but it seems like a whole lot of extra typing for nothing.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/catOnKeyboard.jpg" alt="Cat on keyboard"></p>
<p>i&#8217;ve been typing for friggin&#8217; HOURS!
</p></div>
<p>But for those of us designers-turned-coders who have embraced Classes (or have been forced into using them for a paid project with a deadline), we&#8217;ll tell you that when written the &#8220;right&#8221; way, Classes can free you from writing a lot of code in your next project.</p>
<p>The trouble comes when you know so much about Classes that you write them <em>too</em> correctly.  You can definitely spend too much time making a Class all things to all people and for all purposes.  &#8220;It&#8217;s a MegaClass that can be an animated sprite AND a media player!  It&#8217;s so VERSATILE!&#8221;  Meanwhile, you either haven&#8217;t launched a game in a year, or your clients are calling every other hour wondering how their project is coming along.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/kidsInBackSeat.jpg" alt="Kids in back seat"></p>
<p>Are we there yet? Are we there yet? Are we there yet? Are we there yet?
</p></div>
<h2>Let&#8217;s Recap</h2>
<p>In <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/">Understanding Classes Part 1</a>, you freed yourself from the timeline.  In <a href="http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/">Part 2</a>, you took a little detour and learned how to piggyback your library to write custom MovieClip symbols using <em>inheritance</em>.  In <a href="http://www.untoldentertainment.com/blog/2009/09/15/tutorial-understanding-classes-in-as3-part-3/">Part 3</a>, you started looking at how code could be organized in separate files to keep your workspace light, and to better organize your work.  Now let&#8217;s take a look at the next principle of OOP, <em>encaspulation</em>, to see if we can&#8217;t turn our Classes into true drag-and-drop Lego bricks.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/encapsulation.jpg" alt="OOP Encapsulation"></p>
<p>Good encapsulation makes dinosaurs happen.  Bad encapsulation makes the dinosaurs go crazy and kill people.
</p></div>
<h2>Lego, Ikea furniture, and Your Code</h2>
<p>Once again, the three principles of Object-Oriented Programming are <em>inheritance</em>, <em>encapsulation</em>, and <em>polymorphism</em>.  </p>
<p>Think of your Classes as Lego bricks.  You want to build a Media Player brick that works in your current project, that you can lift out and snap into your next project that requires a Media Player, with no extra work required.  That&#8217;s the dream, anyway.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/lego.jpg" alt="Lego"></p>
<p>You want your code to be less time-consuming.  You know &#8211; like Lego.
</p></div>
<p>Up to this point, i&#8217;ve recommended that you make all of your Classes&#8217; fields (things that a Class <em>is</em>) and methods (things that a Class <em>does</em>) <b>public</b>. Public variables and methods can be accessed by anyone and anything outside the Class.</p>
<p>Let&#8217;s look at some code. We have an ImageGallery Class and our Main Class.  Main creates an instance (stamp, version, print) of ImageGallery and draws it to the screen using addChild:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageGallery <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> numberOfImagesICanDisplay:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ImageGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Display numberOfImagesICanDisplay</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> imageGallery:ImageGallery = <span style="color: #000000; font-weight: bold;">new</span> ImageGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>imageGallery<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So far so good.  But take a close look at that variable on ImageGallery:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> numberOfImagesICanDisplay:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>;</pre></div></div>

<p>That field is declared <b>public</b>.  That means that any idiot (including YOU) can access that field and change it.  Let&#8217;s be a total jackwad and, from Main, change it to eight million:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">		imageGallery.<span style="color: #006600;">numberOfImagesICanDisplay</span>:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">8000000</span>;</pre></div></div>

<p>This messes up ImageGallery&#8217;s pyjama party to the very limit.  How is ImageGallery supposed to display eight million photos?  It&#8217;s only really set up to handle five images.  There HAS to be a way to protect important variables like numberOfImagesICanDisplay from other ass-hat Classes like Main.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/terminator.jpg" alt="Terminator"></p>
<p>i recommend sending the Terminator back in time to prevent yourself from messing with that variable.
</p></div>
<h2>Stop Looking at My Privates</h2>
<p>Fortunately, there is a better and less future-altering way. If we change <b>public</b> to <b>private</b>, only instances (copies, stamps) of the ImageGallery Class can read and write the value of that field.  Hooray!</p>
<p>The way i suggested you write your Classes was completely backards. <b>Public</b> is the <em>most permissive</em> access modifier. <b>Private</b> is the <em>least permissive</em> access modifier. </p>
<p>The rule i&#8217;ve read is that you should always start out restrictive, declaring everything <b>private</b>, because once you go <b>public</b> it&#8217;s kinda hard to go back to <b>private</b>.  (If you were an early bloomer in high school who &#8220;went public&#8221;, you&#8217;ll know how difficult it is to regain your privacy)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/schoolgirl.jpg" alt="Slutty schoolgirl"></p>
<p>It&#8217;s hard to recover from going public
</p></div>
<p>So declare everything &#8211; methods and fields/variables &#8211; <em>private</em>.  If you find that any outside Classes need to access those methods and fields later, you can open them up by making them <em>public</em>.</p>
<h2>What Should I Make Public?</h2>
<p>Object-oriented programming eggheads are pretty particular about the kinds of things that should be public, and those that should be private.  The example i&#8217;ve heard is to think of a device like a vending machine.  There are certain client-facing input methods that need to be accessible to the outside world:</p>
<ul>
<li>insert coin
<li>push snack button
<li>push coin return button
</ul>
<p>And there are certain things the vending machine is going to return to the person using it:</p>
<ul>
<li>snacks
<li>coins
<li>error messages (&#8220;SOLD OUT&#8221;, INCORRECT CHANGE&#8221;, &#8220;NO CANDY FOR YOU, FATTY&#8221;, etc)
</ul>
<p>So the vending machine accepts input and dispenses output.  <em>Everything else is private</em>.  The user does not need to be able to control the coils that turn to dispense the snacks.  The user has no business figuring out how to make change inside the machine, or managing the vending machine&#8217;s inventory of goodies.  That&#8217;s all internal stuff.  That&#8217;s private.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/vendingMachine.jpg" alt="Vending Machine"></p>
<p>None shall know the labyrinthine secrets of the vending machine!  MWA HA HA HA HA!!
</p></div>
<h2>Dispatching Events</h2>
<p>We already know how to write a public method on a Class, and we learned how to pass a <em>parameter</em> through to the Class.  Here&#8217;s a snippet from an imaginary vending machine Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertCoin<span style="color: #66cc66;">&#40;</span>coin:Coin<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// Tally up the value of the coin that has been inserted</span>
	<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>coin.<span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;quarter&quot;</span>:
			coinValue += <span style="color: #cc66cc;">25</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;dime&quot;</span>:
			coinValue += <span style="color: #cc66cc;">10</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;nickel&quot;</span>:
			coinValue += <span style="color: #cc66cc;">5</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;penny&quot;</span>:
			<span style="color: #808080; font-style: italic;">// No pennies!</span>
			<span style="color: #b1b100;">return</span> coin<span style="color: #66cc66;">&#40;</span>coin<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #000000; font-weight: bold;">default</span>:
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And here&#8217;s an example of how you might call that function from outside the Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">vendingMachine.<span style="color: #006600;">insertCoin</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Quarter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>In Actionscript 3, the <em>proper</em> way to signal to the outside world that something important has happened <em>inside</em> a Class is to fire an even dispatcher.  Here&#8217;s what that looks like in our imaginary vending machine Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// This line goes at the top of the Class where you declare your fields:</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const RETURN_SNACK:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;returnSnack&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// And then later, when you declare your methods:</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> returnSnack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>RETURN_SNACK<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And here&#8217;s how you listen for that event in the Main Class (or from whichever Class instantiates [makes a copy of] the vending machine):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Make sure you import the generic Event stuff near the top of the file:</span>
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">Events</span>.<span style="color: #006600;">event</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// This is what your instantiation and listener definition look like:</span>
<span style="color: #000000; font-weight: bold;">var</span> vendingMachine:VendingMachine = <span style="color: #000000; font-weight: bold;">new</span> VendingMachine<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
vendingMachine.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>VendingMachine.<span style="color: #006600;">RETURN_SNACK</span>, getASnack<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// And elsewhere, you define the method to respond to the event:</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getASnack<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// HOORAY FOAR SNAKZ!!!</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Anyone see a problem yet?  i do.  We&#8217;re not really able to pass a parameter back to Main through our dispatchEvent command.  So how does Main know what kind of snack it received?</p>
<p>There are a few ways around this.  We can store a variable on Main called requestedSnack and take it on faith that vendingMachine returns the correct snack.  Or, we can create a public variable on VendingMachine to store the returnedSnack, and in Main&#8217;s getASnack method, we refer to vendingMachine.returnedSnack.  And then there are super egghead ways to do it that remain mysterious to me.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/nerd.jpg" alt="Nerd"></p>
<p>i am not well-versed in the Way of the Nerd.
</p></div>
<h2>Let&#8217;s Get Ready to Fudge It</h2>
<p>But here&#8217;s the CHEATERPANTS way of doing things.  Using dispatchEvent is nice, because we can plop this vending machine down in any other game or app and it WORKS.  It&#8217;s <em>encapsulated</em>.  There are no dependancies.  We just need to listen to all of its dispatched events in the new file, and we&#8217;re gold.  But to be honest, that&#8217;s a whole lot more typing, and i&#8217;ve got things to do.</p>
<p>The cheaty way to do it is to pass a reference to Main as a parameter to the VendingMachine Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// From Main:</span>
<span style="color: #000000; font-weight: bold;">var</span> vendingMachine:VendingMachine = <span style="color: #000000; font-weight: bold;">new</span> VendingMachine<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// From VendingMachine:</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> VendingMachine<span style="color: #66cc66;">&#40;</span>main:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// And then later in VendingMachine:</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dispenseSnack<span style="color: #66cc66;">&#40;</span>snack:Snack<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	main.<span style="color: #006600;">getASnack</span><span style="color: #66cc66;">&#40;</span>snack<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Hooray!  No need to listen for an event, dispatch an event, declare a constant, or tapdance around the return value problem.  The vendingMachine instance knows who its daddy is, and can invoke any of Main&#8217;s public methods.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/whosYourDaddy.jpg" alt="Who's Your Daddy"></p>
<p>(um &#8230; i can&#8217;t actually tell, &#8217;cause his head is cut off)
</p></div>
<p>Of course, the drawback is that this breaks encapsulation.  You can no longer pick up your VendingMachine Class and move it to a new project, because VendingMachine is <em>dependant</em> on receiving a reference to a Class that has a public method called getASnack() on it.</p>
<p>Here&#8217;s how i go about it: if i&#8217;m writing something that i think is pretty handy, and it&#8217;s abstract enough that i&#8217;ll likely use it in future projects, i might do the extra work to write a nicely encaspulated Class.  But if it&#8217;s sunny outside and all the other kids are playing in the park, and i can hear their joyous squeals through my opened window, i&#8217;ll break encapsulation and throw my Class a reference to the Class that instantiated it.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_24/splashPad.jpg" alt="Splash Pad"></p>
<p>Encapsulation be damned &#8211; the splash pad&#8217;s open!
</p></div>
<h2>Don&#8217;t Cry, Nerds</h2>
<p>Dear Eggheads: i haven&#8217;t left you out this time.  The concept of passing a variable to the constructor function of a Class was explained perfectly clearly here, i hope, but i know how you like to muck things up with big words and impressive terminology. Your term for this technique is &#8220;Dependency Injection&#8221;, and if you&#8217;d like to read a more serious article on it with fewer pictures and bigger words, check out Joel Hooks&#8217;s article <a href="http://www.insideria.com/2009/09/as3-dependency-injection-demys.html">AS3 Dependency Injection Demystified</a>. </p>
<p>Here&#8217;s a solid step towards demystifying it:  STOP CALLING IT DEPENDENCY INJECTION.  Sheesh almighty.</p>
<p>To read the rest of the Understanding Classes series, or to see more tutorials, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F24%2Ftutorial-understanding-classes-in-as3-part-4%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F24%2Ftutorial-understanding-classes-in-as3-part-4%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/24/tutorial-understanding-classes-in-as3-part-4/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1787&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/24/tutorial-understanding-classes-in-as3-part-4/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Tutorial: Understanding Classes in AS3 Part 3</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/15/tutorial-understanding-classes-in-as3-part-3/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/15/tutorial-understanding-classes-in-as3-part-3/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 13:39:06 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Pitfalls]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1572</guid>
		<description><![CDATA[In this tutorial, learn how to identify when a chunk of code should be spun off into its own .as Class file. Trial by Flash This series of Understanding Classes tutorials chart my progress through unfamiliar Actionscript 3.0 territory. i am a designer-turned-coder, so everything i understand about code was learned in the School of [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, learn how to identify when a chunk of code should be spun off into its own .as Class file.</p>
<h2>Trial by Flash</h2>
<p>This series of Understanding Classes tutorials chart my progress through unfamiliar Actionscript 3.0 territory. i am a designer-turned-coder, so everything i understand about code was learned in the School of Hard Knocks &#8230; most often while a producer breathed down my neck and i had two weeks to program a game using concepts i didn&#8217;t grok.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_15/grokking.jpg" alt="Sgt. Slaughter"></p>
<p>Grokking is Half the Battle
</p></div>
<p>i had already learned to avoid &#8220;spaghetti code&#8221;, a common newbie Flash developer habit where you stick little bits of code on various MovieClip symbol instances and frames throughout your Flash movie.  The next step was to round up all of those little bits of spaghetti code and confine it all to the first frame of my Flash movie.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_15/spaghetti.jpg" alt="Baby w/ spaghetti"></p>
<p>Don&#8217;t spaghetti code. Think of the children.
</p></div>
<p>The next leg of the journey into Actionscript 3.0 was to escape the first frame, and to move all of that first-frame code into its own seprate .as Class file. That&#8217;s where <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/">Understanding Classes began</a>.  It seemed like a pointless step &#8211; whether the code was in its own frame or in its own Class file seemed like picky Egghead semantics.  </p>
<p>The fuzzy picture came more clearly into focus with the <a href="http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/">next Understanding Classes tutorial</a>, where we learned to write a custom Class to represent symbols in the Flash library.  We used the less-than-helpful example of a Dog Class to explain how to pull this off.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_15/retardedDog.jpg" alt="Retarded Dog"></p>
<p>Thanks for nothing, DOG.
</p></div>
<p>But the time for theory is over.  If we&#8217;re going to finally learn how to take full advantage of Object Oriented Programming, we need to look at some real-world examples.  In this tutorial, we&#8217;ll pick through that glob of code in our Main.as file and find places where it makes sense to group chunks of functionality and split them off into their own Class files.</p>
<h2>It&#8217;s Apparent You&#8217;re A Parent</h2>
<p>In order to understand when and where to create new Classes, you almost have to think like a parent.  All of the methods (things a Class can <em>do</em>) and properties (things a Class <em>is</em>) are your children.  As your children grow older, you want them to learn how to take care of themselves.  You (hopefully) don&#8217;t want to be changing your son&#8217;s diapers when he&#8217;s 17 years old.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_15/babyhuey.jpg" alt="Baby Huey"></p>
<p>Worst. Cartoon character. Evar.
</p></div>
<p>So when you look at that mess of code in your Main.as Class, start asking yourself this question: should this kid, this group of properties and methods, be taking care of himself?  Is this stuff that me, the Main.as Class, should be doing, or is this something that this kid can go off and do by himself?</p>
<h2>Huh?</h2>
<p>Here&#8217;s a real-world example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> myButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">label_txt</span>.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Play&quot;</span>;
			myButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickMyButton<span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, rollOverMyButton<span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, rollOutMyButton<span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">mouseChildren</span> = <span style="color: #000000; font-weight: bold;">false</span>;
			addChild<span style="color: #66cc66;">&#40;</span>myButton<span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			myButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOverMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> myButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;highlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOutMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> myButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unhighlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Do some stuff here</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In this piece of code, the Main.as file adds an instance of the MyButton Class to the stage.  When the player clicks the button, we fire the playGame method.  </p>
<p>There are some things here that the Main.as Class should probably do, and a bunch of things that the LabelButton Class should do for itself.  Let me ask you this: why on God&#8217;s green Earth should Main.as care about how the MyButton Class implements all of its MouseEvent code?  Making the button highlight on rollOver is MyButton&#8217;s responsibility, not Main.as&#8217;s responsibility.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_15/rollover.jpg" alt="Rollover"></p>
<p>Take care of your own rollover.
</p></div>
<p>Let&#8217;s imagine a not-too-distant future.  Robots rule the Earth.  Gary Coleman is in the White House.  And you&#8217;re writing a new Actionscript project where you need a MyButton.  The way you&#8217;ve written it here, can you easily pluck your MyButton Class out of this old project, and plunk it into your new project and expect it to work?  No!  That&#8217;s because you have code that is crucial to the functioning of your MyButton class <em>outside</em> your MyButton Class.</p>
<p>Do the clickMyButton, rollOverMyButton, and rollOutMyButton methods <em>belong</em> to Main, or do they belong to MyButton?  Let&#8217;s go back to our semi-useless Dog example.  Do bark, sniff and peeOnLeg belong to Main, or do they belong to Dog?  They belong to Dog.  So let&#8217;s put them in Dog.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_15/fatDog.jpg" alt="Fat Dog"></p>
<p>Stuff everything concerning Dog IN Dog.
</p></div>
<p>Here&#8217;s the above script with the MyButton-specific methods removed:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> myButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">label_txt</span>.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Play&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>myButton<span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			myButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Wow!  We really cleaned up our Main Class, didn&#8217;t we?</p>
<p>Here&#8217;s what our MyButton Class looks like.  We&#8217;ve just plucked the methods out of our Main Class and put them where they belong, in MyButton.  <em>MyButton changes its own diapers</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickMyButton<span style="color: #66cc66;">&#41;</span>;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, rollOverMyButton<span style="color: #66cc66;">&#41;</span>;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, rollOutMyButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOverMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;highlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOutMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unhighlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Do some stuff here</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you&#8217;re keen-eyed, you&#8217;ll notice that we were even able to cut down the code when we moved it to the MyButton Class.  Instead of saying <b>myButton.addEventListener(MouseEvent.CLICK, clickMyButton);</b>, we can drop the &#8220;myButton&#8221; part and just say <b>addEventListener(MouseEvent.CLICK, clickMyButton);</b>.  That&#8217;s essentially the same as saying <b>this.addEventListener(MouseEvent.CLICK, clickMyButton);</b>.  The &#8220;this&#8221; keyword is like saying &#8220;me&#8221;.  We&#8217;re <em>inside</em> the MyButton world, so we&#8217;re referring to ourselves.  While we are writing code inside the MyButton class, we <em>are</em> MyButton.</p>
<h2>Starting an Argumemt</h2>
<p>Now what about that line in Main.as where we label the button?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myButton.<span style="color: #006600;">label_txt</span>.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Play&quot;</span>;</pre></div></div>

<p>Is labelling the button a responsibility of Main.as, or a responsibility of MyButton.as?  The answer is slightly tricky.</p>
<p>MyButton might not always be labelled &#8220;Play&#8221;.  We could (and should) think generically, so that MyButton could be labelled &#8220;Play&#8221; or &#8220;New Game&#8221; or &#8220;Change Character&#8221; or whatever.  So Main.as should be responsible for labelling the button, right?</p>
<p>Not exactly.  MyButton should still be responsible for labelling itself. But Main.as should be responsible for deciding what that label says. That way, Main could create buttons that say &#8220;Play&#8221;, &#8220;New Game&#8221; and &#8220;Change Character&#8221;,all using the same MyButton Class.  The MyButton Class takes care of all the logic of actually displaying that text on itself.</p>
<p>To pull this off, we pass a <em>parameter</em> to our MyButton Class from Main.as.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> myButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Play&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Check it out!!!</span>
			addChild<span style="color: #66cc66;">&#40;</span>myButton<span style="color: #66cc66;">&#41;</span>;
			myButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			myButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Main.as creates a new MyButton instance (stamp, copy), and <em>passes</em> something called a parameter to the MyButton Class.  MyButton grabs that parameter and uses it to label itself.</p>
<p>Note: if you&#8217;re fuzzy on the concept of datatypes (String, Number, Int, Boolean, etc), you may already be in over your head.  Go read a basic tutorial on datatypes, and come on back.</p>
<p>Here&#8217;s the MyButton Class, now modified to grab that parameter from Main.as (or whatever Class instantiates [makes a stamp of, makes a copy of] the MyButton class).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span>labelName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			label_txt.<span style="color: #0066CC;">text</span> = labelName; <span style="color: #808080; font-style: italic;">// Grab that parameter and label myself!</span>
&nbsp;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickMyButton<span style="color: #66cc66;">&#41;</span>;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, rollOverMyButton<span style="color: #66cc66;">&#41;</span>;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, rollOutMyButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOverMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;highlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOutMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unhighlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Do some stuff here</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Beautiful!  In this line, we accept a parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span>labelName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>And in this line, we grab that parameter and use it to label ourselves:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">label_txt.<span style="color: #0066CC;">text</span> = labelName; <span style="color: #808080; font-style: italic;">// Grab that parameter and label myself!</span></pre></div></div>

<h2>Jockeying for Position</h2>
<p>Finally, we&#8217;ll talk about the x/y lines in the Main Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
myButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;</pre></div></div>

<p>Is that something that Main should be responsible for, or something that MyButton should be responsible for? x and y are properties of MyButton, so MyButton should handle them, right?</p>
<p>Not so fast!  While we <em>could</em> let MyButton position itself, it wouldn&#8217;t make sense to.  The Class that needs a MyButton should decide where on the screen the button should go.  Otherwise, every time we create a new MyButton instance, it&#8217;ll appear in the same place on the screen.  Not so hot.  Main.as should take care of positioning the button instance.</p>
<h2>Real Ultimate Power</h2>
<p>Et voila!  We&#8217;re in good shape now.  We&#8217;ve:</p>
<ol>
<li>Identified a chunk of code that should change its own diapers
<li>Moved that code to its own .as Class file
<li>Thought generically about how an instance (stamp, copy) of that Class should be treated
</ol>
<p>Now, check out the glorious wonderland we can experience and enjoy:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Instantiate a new play button:</span>
			<span style="color: #000000; font-weight: bold;">var</span> myPlayButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Play&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>myPlayButton<span style="color: #66cc66;">&#41;</span>;
			myPlayButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			myPlayButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Instantiate a new instructions button:</span>
			<span style="color: #000000; font-weight: bold;">var</span> myInstructionsButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Instructions&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>myInstructionsButton<span style="color: #66cc66;">&#41;</span>;
			myInstructionsButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">210</span>;
			myInstructionsButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Instantiate a new credits button:</span>
			<span style="color: #000000; font-weight: bold;">var</span> myCreditsButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Credits&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>myCreditsButton<span style="color: #66cc66;">&#41;</span>;
			myCreditsButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">340</span>;
			myCreditsButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Wowee!  THREE new button instances in half the code it took to have ONE button when we first started out.</p>
<p>We&#8217;re almost out of the woods.  But there&#8217;s one nagging thing remaining.  Notice that we have a method in the MyButton Class that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	playGame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Should MyButton decide what happens when it gets clicked?  We saw that the Main.as Class can quickly create three instances of MyButton &#8211; a Play button, an Instructions button, and a Credits button.  These three buttons are all going to do different things when the player clicks them.  So the generic MyButton Class cannot possibly define its own click behaviour.  Main.as is going to have to decide.</p>
<p>Let&#8217;s yank that click code right out of the MyButton Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span>labelName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			label_txt.<span style="color: #0066CC;">text</span> = labelName; <span style="color: #808080; font-style: italic;">// Grab that parameter and label myself!</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// this is where the MouseEvent.CLICK listener used to be</span>
&nbsp;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OVER</span>, rollOverMyButton<span style="color: #66cc66;">&#41;</span>;
			addEventListener<span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_OUT</span>, rollOutMyButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOverMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;highlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rollOutMyButton<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">background</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unhighlight&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And instead, we&#8217;ll write the listener for each button&#8217;s MouseEvent.CLICK in the Main.as Class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Instantiate a new play button:</span>
			<span style="color: #000000; font-weight: bold;">var</span> myPlayButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Play&quot;</span><span style="color: #66cc66;">&#41;</span>;
			myPlayButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickBtnPlay<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// new line!</span>
			addChild<span style="color: #66cc66;">&#40;</span>myPlayButton<span style="color: #66cc66;">&#41;</span>;
			myPlayButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			myPlayButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Instantiate a new instructions button:</span>
			<span style="color: #000000; font-weight: bold;">var</span> myInstructionsButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Instructions&quot;</span><span style="color: #66cc66;">&#41;</span>;
			myInstructionsButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickBtnInstructions<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// new line!</span>
			addChild<span style="color: #66cc66;">&#40;</span>myInstructionsButton<span style="color: #66cc66;">&#41;</span>;
			myInstructionsButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">210</span>;
			myInstructionsButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Instantiate a new credits button:</span>
			<span style="color: #000000; font-weight: bold;">var</span> myCreditsButton:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Credits&quot;</span><span style="color: #66cc66;">&#41;</span>;
			myCreditsButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickBtnCredits<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// new line!</span>
			addChild<span style="color: #66cc66;">&#40;</span>myCreditsButton<span style="color: #66cc66;">&#41;</span>;
			myCreditsButton.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">340</span>;
			myCreditsButton.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">89</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickBtnPlay<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Play the game</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickBtnInstructions<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Show the instructions</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clickBtnCredits<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Show the credits</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And there you have it.  The MyButton Class takes care of its own diapers.  It labels itself based on a parameter that&#8217;s passed to it.  It handles its own rollOver and rollOut events.  Main.as takes care of creating instances (stamps, copies) of the MyButton Class. Main.as decides:</p>
<ul>
<li>What the button label will say
<li>How the button will be positioned on the screen
<li>What the button does when clicked
</ul>
<h2>The First of Many</h2>
<p>That&#8217;s a real-world example of where and when it makes sense to collect your code in a separate Actionscript Class file.  Of course, this is just one example in an endless sea of possiblilities.  The general rule is that if the code should be changing its own diapers, it should be in its own Class file. And if the code can be written generally so that it can be re-used, you&#8217;re ahead of the game.  </p>
<p>The mystery that springs up from all of this is the OOP rule of Encapsulation, which states that you should write your Classes so that they&#8217;re self-contained and self-reliant. Not only do they change their own diapers, but they rent their own apartment and cook meals for themselves, with no reliance on outside Classes whatsoever to live.  </p>
<p>There are advantages and disadvantages to Encapsulation, which we can look at in our next Understanding Classes tutorial.  The biggest disadvantage is that to create a truly encapsulated Class, you have to write an ASSLOAD of repetetive Actionscript 3 code.  i&#8217;ll show you a cheat that&#8217;ll break the encapsulation rule, but will save you HOURS of coding.  The Eggheads will have my head for it, so let&#8217;s just keep it between you and me.</p>
<p>For more tips, tricks and tutorials on Flash and Actionscript, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F15%2Ftutorial-understanding-classes-in-as3-part-3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F15%2Ftutorial-understanding-classes-in-as3-part-3%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/15/tutorial-understanding-classes-in-as3-part-3/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1572&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/15/tutorial-understanding-classes-in-as3-part-3/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Tutorial: Understanding Classes in AS3 Part 2</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 13:42:14 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Pitfalls]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1661</guid>
		<description><![CDATA[In our first thrilling look at Understanding Classes in Actionscript 3.0, we talked about how Classes are structured, and how you can make that leap from stashing all your code in the first frame of your Flash movie like an IT chipmunk, to moving your code to its own Class. I promised that in Part [...]]]></description>
			<content:encoded><![CDATA[<p>In our first thrilling look at <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/">Understanding Classes in Actionscript 3.0</a>, we talked about how Classes are structured, and how you can make that leap from stashing all your code in the first frame of your Flash movie like an IT chipmunk, to moving your code to its own Class.</p>
<p>I promised that in Part 2, we’d learn how to identify chunks of code that should be moved into their own Classes.  But before we do that, it’s important to talk about what a Class <em>is</em>.  And if you haven’t learned how to extend a MovieClip symbol in your library to write a Class around it, this tutorial is for you!</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/egg.jpg" alt="Take THAT, eggheads"></p>
</div>
<p><b>Disclaimer</b> Dear Eggheads: This tutorial is between me, a designer-turned-coder, and readers who want to learn more about Actionscript programming who likely are not Computer Science graduates.  The terminology i use may not be 100% accurate, and some of the things i say may inflame your nerd rage and your unwavering demand for technical accuracy.  But this article is not for you.  Stop reading now. Go write some slash fiction about Captain Kirk totally doing it with the dog from Battlestar Galactica, and leave us alone to learn.</p>
<h2>OOP: There It Is</h2>
<p>A Class is an element of OOP – Object Oriented Programming.  OOP is a flavour of coding where different objects – or <em>things</em> &#8211; interact with each other to solve a problem.  If you’re a game programmer, the problem can be described as “How do i make a fun, functional zombie game?”  The word “object” is synonymous with “Class”.  You could say that Actionscript 3.0 is a <em>Class Oriented Programming</em> language, but the eggheads would stab you in the face.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/nerdRage.jpg" alt="She-Nerd"></p>
<p>She-Nerd SMASH!!
</p></div>
<p>So a Class is a <em>thing</em> &#8211; a chunk, a morsel, a <em>module</em> of code.   By writing your Classes the &#8220;right&#8221; way, you’ll find you can re-use Classes from project to project.  By writing them the &#8220;wrong&#8221; way, you’ll still probably wind up with a project that has nice, well-organized code that makes sense if you re-visit it a year later.  </p>
<p>The three principles or rules to follow if you want to maximize the advantages  of OOP are <b>encapsulation</b>, <b>inheritance</b> and <b>polymorphism</b>.  We’ll be taking a brief, gentle look at <b>inheritance</b> in this tutorial, because i think it’s the easiest to understand when you’re starting out.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/twins.jpg" alt="Inbred Twins"></p>
<p>Herbert n&#8217; Zed inherit their genes from their paw n&#8217; their sis
</p></div>
<p>Classes contain methods and properties.  (Another word for &#8220;method&#8221; is &#8220;function&#8221; or &#8220;behaviour&#8221;.  Another word for &#8220;property&#8221; is &#8220;field&#8221;.)  A method is something a Class can <em>do</em>.  A property is something a Class <em>is</em>.<br />
Let’s explain this somewhat better by using a crappy example that a lot of OOP tutorials use.  You have a Class called “Dog”.   Methods of Dog – things that Dog can <em>do</em> &#8211; include:</p>
<ul>
<li>bark
<li>sniff
<li>humpLeg
</ul>
<p>Properties of Dog &#8211; things that dog <em>is</em> &#8211; include:</p>
<ul>
<li>color
<li>breed
<li>favouriteTreat
</ul>
<p>Another way to think of it is that methods are verbs, and properties are adjectives.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/uglyDog.jpg" alt="Ugly Dog"></p>
<p>The OOP Dog example rears its ugly head
</p></div>
<h2>Let&#8217;s Get Real</h2>
<p>The Dog example is almost entirely useless to us as programmers. In all my years of experience, i have never written a Dog Class.  But i HAVE written my fair share of music and movie players in Flash.  Let&#8217;s look at the methods and properties of a media player.</p>
<p>Methods of our MediaPlayer Class &#8211; things that a media player can <em>do</em> &#8211; include:</p>
<ul>
<li>play
<li>pause
<li>stop
<li>scrub
<li>changeVolume
<li>loadMedia
</ul>
<p>Properties of our MediaPlayer Class &#8211; things that a media player <em>is</em>, or things that describe its <em>state</em> &#8211; include:</p>
<ul>
<li>isPaused
<li>songDuration (or movieDuration)
<li>isFullScreen
<li>volume
</ul>
<p>Note that the <em>method</em> &#8220;changeVolume&#8221; probably changes the <em>property</em> &#8220;volume&#8221;.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/snowths.jpg" alt="Snowths"></p>
<p>We&#8217;re gonna need a method called &#8220;turnVolumeWayWayUp&#8221;
</p></div>
<h2>You&#8217;re Already an Expert</h2>
<p>The good news is that as a Flash developer, you’ve been using Classes this whole time, though you may not have known it.  A MovieClip is a Class.  It has methods and properties.  Let&#8217;s look at some of the methods of a MovieClip &#8211; things a MovieClip can <em>do</em>:</p>
<ul>
<li>gotoAndPlay
<li>stop
<li>hitTestPoint (determine whether a point is colliding with me)
</ul>
<p>And some properties of a MovieClip &#8211; things a MovieClip <em>is</em> (or things that describe its <em>state</em>):</p>
<ul>
<li>x and y
<li>alpha
<li>rotation
</ul>
<p>Eggheads: Are you still reading this?  i told you to take a hike. Don&#8217;t jump down my throat.  i know that a few of these methods and properties actually belong to Classes that MovieClip <em>inherits</em> from, but we&#8217;re just learning, alright?  Leave us alone.  Go out and LARP or something.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/larp.jpg" alt="LARP"></p>
</div>
<h2>No Pain, No Awesome</h2>
<p>Just as you can make the leap from stowing all your code in the first frame of your movie to putting it in an .as Class file, you can (and should) make the leap from having all of your MovieClip instances &#8220;physically&#8221; present on your stage, to keeping them in the library and drawing them to the stage using code.  There are many advantages to using a code-only approach which probably won&#8217;t become clear to you until you go through the excrutiating growing pains of &#8220;WHY THE HELL AM I DOING IT THIS WAY??&#8221; like i did.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/growingPains.jpg" alt="Growing Pains"></p>
<p>Growing Pains: excrutiating
</p></div>
<p>So create a MovieClip in your library.  NOTE: If you don&#8217;t know how to do this, you may already be in over your head.  Go check out a beginner Flash tutorial and come on back.</p>
<p>In order for you to make the MovieClip available to you through code, you need to give it a linkage name. Here&#8217;s how:</p>
<ol>
<li>Open your Library (CTRL+L)
<li>Right-click the MovieClip symbol
<li>In Flash CS3, choose &#8220;Linkage&#8221;.  In Flash CS4, choose &#8220;Propeties&#8221;.
<li>Check the box that says &#8220;Export for ActionScript&#8221;
<li>In the &#8220;Class:&#8221; field, type a logical name for your MovieClip.  It&#8217;s best practice to start this name with a capital letter.  Don&#8217;t use any spaces &#8211; capitalize each new word SoThatItLooksLikeThis.  (This is called &#8220;Pascal case&#8221;)
<li>Click &#8220;OK&#8221;
<li>If you are a Mac user, and you&#8217;re saying &#8220;What&#8217;s a CTRL??  What&#8217;s a right mouse button?? i&#8217;m freaking OUT, man!&#8221;, go buy a PC  ;)
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/linkageCS3.jpg" alt="Linkage CS3"></p>
<p>Setting linkage for a library item in Flash CS3 &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/linkageCS4.jpg" alt="Linkage CS4"></p>
<p>&#8230; and in Flash CS4.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/linkageName.jpg" alt="Linkage Name"></p>
<p>Choosing a linkage name
</p></div>
<p>Did you notice, before you hit OK, that there was a field labelled &#8220;Base Class:&#8221; that said &#8220;flash.display.MovieClip&#8221;?  If this is your first time creating a library linkage, or if it&#8217;s old hat and you&#8217;ve never noticed this field, prepare to <em>blow your own mind</em>: you&#8217;ve just used the first principle of OOP &#8211; <em>inheritance</em>.  </p>
<h2>My Mind Done Blow&#8217;d Up</h2>
<p>Here&#8217;s what&#8217;s going on behind the scenes. &#8220;MovieClip&#8221; is a built-in Class that the creators of Actionscript wrote. When you give your library item a linkage name, you are actually subclassing, or <em>inheriting</em>, from the MovieClip Class.  That means that your new Class &#8220;Dog&#8221; or &#8220;MediaPlayer&#8221; or &#8220;EnemySpaceship&#8221; can now do all the stuff that a MovieClip can do.  It can <b>gotoAndPlay</b>, it can <b>stop</b>, it can <b>hitTestPoint</b>, etc.  It also inherits all of MovieClip&#8217;s properties &#8211; <b>x</b> and <b>y</b>, <b>alpha</b>, <b>rotation</b>, etc. &#8211; things that a MovieClip <em>is</em>, things that describe its <em>state</em>.</p>
<p>So now we&#8217;re getting to the point.  When you create a library linkage like this, Flash is automagically creating a new Class (&#8220;Dog&#8221;, &#8220;MediaPlayer&#8221;, whatever).  You don&#8217;t have to write it.  But you CAN write it, if you want to.  And since that&#8217;s what this tutorial is all about, then oh yes &#8211; we want to.</p>
<h2>Oh Yes &#8211; Let&#8217;s</h2>
<p>Start a new .as Class file.  You can do it in the Flash IDE, you can write it with Notepad or Notepad ++ or any other text editor.  i highly recommend Flash Develop.   Remember our <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/">first Understanding Classes tutorial</a>? Here&#8217;s the structure of a Class:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/AS3ClassStructure.jpg" alt="AS3 Class Structure"></p>
<p>i chose these colours from Arsenio Hall&#8217;s wardrobe
</p></div>
<p>So here&#8217;s what our empty Class code should look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// Import statement(s)</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Class definition</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> isBarking:<span style="color: #0066CC;">Boolean</span>; <span style="color: #808080; font-style: italic;">// Instance variable(s)</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Dog<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Constructor function</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;i am a dog&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Other functions</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> bark<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			isBarking = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> stopBarking<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			isBarking = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s take note of a few important things.</p>
<ol>
<li>i&#8217;m calling my Class &#8220;Dog&#8221;.  That&#8217;s because the linkage name in used in the Flash library was &#8220;Dog&#8221;.  Note: not &#8220;dog&#8221; or &#8220;DogClip&#8221; or anything.  D-o-g.  Same capitalization, exact same name.
<li>And note that the linkage name can be <em>completely different</em> from the library symbol name.  My library symbol could be called &#8220;This is a thing what i created in teh Flashs&#8221;.  Doesn&#8217;t matter.  Actionscript can&#8217;t &#8220;see&#8221; the library item name.  It can only &#8220;see&#8221; the linkage name.
<li>Finally, remember that the Class file name has to be the same as the Class name.  If our Class is called <b>Dog</b>, we save the file as <b>Dog.as</b>.  Pull any other shenanigans, and you&#8217;ll get errors.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/sillyDog.jpg" alt="Silly dog"></p>
<p>i SAID &#8220;no shenanigans&#8221;, DOG
</p></div>
<p>i mentioned mysteriously in the last tutorial that the word &#8220;extends&#8221; is an inheritance word.  When you create a linkage name in the library, Flash automagically writes a Class for you, and automagically <em>extends</em> (inherits from) MovieClip.  Now that we&#8217;re writing the Class ourselves, nothing is automagic.  We have to explicitly say that Dog (or whatever) extends (inherits from) the MovieClip Class.  When this is all done correctly, Flash understands that there is a connection between the linked library Class &#8220;Dog&#8221;, and the &#8220;Dog.as&#8221; Class we just wrote.</p>
<p>You may or may not know the code for dynamically placing a linked library item on the stage.  Here it is, using Dog as the example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> dog:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #808080; font-style: italic;">// or var dog:Dog = new Dog();</span>
addChild<span style="color: #66cc66;">&#40;</span>dog<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>&#8220;BUT WAIT!&#8221;, you say. &#8220;I THOUGHT DOG HAD TO START WITH A CAPITAL LETTER??&#8221;  Okay, first of all, stop yelling. Lower-case &#8220;dog&#8221; is the name of the variable that contains an instance (a stamp, a copy) of our master Dog Class.  We could use &#8220;dog1&#8243;, &#8220;dog2&#8243;, or even &#8220;thisIsACopyOfDog&#8221;.</p>
<p>When you addChild(dog), you add your instance (stamp, copy) of the Dog Class to the list of things for Flash to draw to the stage (the viewable area that the player sees).</p>
<p>To confirm that your custom library Class is working, test your movie.  You should see the trace action &#8220;i am a dog&#8221; in the Output window. (Press F2 to see the Output window &#8211; the trace action won&#8217;t appear on the stage)</p>
<h2>What&#8217;s the Point?</h2>
<p>You may be wondering why we went through all of this agony.  The easy way is just to draw a dog and make it a MovieClip, and then leave it on the stage.  There are many, many advantages to what we&#8217;ve just done.  Let me list a few:</p>
<ul>
<li>You can now add as many Dogs to the stage as you like <em>dynamically</em>. Until now, you had to add them by hand.  If you needed another dog, you (the creator) had to drag one out of the library.  Now you can create an &#8220;Add Dog&#8221; button. When the player presses it at run-time, your code dynamically places dogs on the stage.  Now the code is doing the work, not you.
<li>We&#8217;re edging closer to the next OOP principle, <em>encapsulation</em>.  Within our Dog class, we can store methods (things that a Dog <em>does</em>) and properties (things that a Dog <em>is</em>).  Every time we initialize (make a stamp or copy of) Dog, Dog can run a bunch of Dog-specific setup routines:  initializeSenseOfSmell, testBarkTone, prepareToChewShoes.  If you do it &#8220;right&#8221;, you&#8217;ve created a self-reliant, self-contained Class.  Now every time you have a project that needs a Dog (or a MediaPlayer, an ImageGallery, a CountdownClock), you can just copy that .as Class file to a new project. If you&#8217;ve done everything &#8220;right&#8221;, it&#8217;ll just <em>work</em>.
<li>The more you start abstracting your thinking, the more you&#8217;ll be able to write generic Classes that do a number of different jobs.  You might not have another project that needs a Dog, but your generic Animal Class would still do the trick.
<li>There&#8217;s a problem that Actionscript 3 introduced where you can&#8217;t always access certain pieces of MovieClips until they&#8217;re drawn to the screen.  Writing a custom Class for your library elements can overcome this problem.
</ul>
<h2>What&#8217;s Next?</h2>
<p>Moving your frame code to an .as Class file was the first step. Learning how to write custom Classes for your linked library items is the next step, since it builds on (hopefully) familiar MovieClip territory and your existing knowledge.  In the next tutorial, we&#8217;ll take a look at that big wad of code in your Main.as file and try to figure out which methods and properties can be logically grouped and moved into their own Classes.</p>
<p>For more tips, tricks and tutorials, check out our <b><a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a></b> feature.</p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F09%2Ftutorial-understanding-classes-in-as3-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F09%2Ftutorial-understanding-classes-in-as3-part-2%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1661&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Start Me Up &#8211; Great Lists of Links for Game Developers</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/04/start-me-up-great-lists-of-links/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/04/start-me-up-great-lists-of-links/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 14:42:01 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Bidness]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1645</guid>
		<description><![CDATA[HeyZap start-up honcho Jude Gomila posted this handy list of articles for start-up companies on Twitter. The list was compiled by Douglas de Jager, himself the head of a data mining start-up called BytePlay Limited. (All this is true, as long as my Google-fu remains strong) i love big lists of articles like this that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.heyzap.com">HeyZap</a> start-up honcho Jude Gomila posted this <a href="http://www.doc.ic.ac.uk/~dvd03/startups/">handy list of articles for start-up companies</a> on Twitter.  The list was compiled by Douglas de Jager, himself the head of a data mining start-up called BytePlay Limited.  (All this is true, as long as my Google-fu remains strong)</p>
<div class="displayed">
<p><img src = "http://www.untoldentertainment.com/blog/img/2009_09_04/kung-fu-master.jpg" alt="Kung Fu Master"></p>
</div>
<p>i love big lists of articles like this that are grouped by topic!  We&#8217;ve made a few of our own here on the site:</p>
<ul>
<li><b><a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a></b> &#8211; An ongoing series of articles and tutorials, including common error messages and what to do about them
<li><b><a href="http://www.untoldentertainment.com/boards/viewforum.php?f=41">News For You</a></b>  ue_newspoodle compiles a daily list of interesting stories on game development and related topics.  Feel free to add your own links!
<li><b><a href="http://www.untoldentertainment.com/blog/feature-articles/pimp-my-game/">Pimp My Game</a></b> Our feature about monetizing Flash games has been updated with a GREAT list of articles on the subject &#8211; scroll to the bottom of the page to find them
<li>Conjure, a group of ETS students in Montréal, maintain <a href="http://conjuregames.com/linksgda.php"><b>a whopping pantsload of great game industry links</b></a> (thanks <a href="http://twitter.com/jeanpierflash">@jeanpierflash</a>!)
<li>Smashing Magazine offers <a href="http://www.smashingmagazine.com/2009/09/01/35-excellent-wireframing-resources/"><b>35 Excellent Wireframing Resources</b></a> (thanks <a href="http://www.twitter.com/danmartell">@danmartell</a>!)
<li><b><a href="http://www.flashgameblogs.com/index1-2008-03.html">Flash Game Blogs</a></b> &#8211; A Who&#8217;s Who of the Flash game dev world and links to their blogs, maintained by the folks at <a href="https://www.gamersafe.com/">GamerSafe</a>/<a href="http://www.flashgamelicense.com/">Flash Game License</a>
<li><b><a href="http://blog.gambrinous.com/2009/05/07/top-indie-game-development-blogs/">Top Indie Developer Blogs</a></b> &#8211; We were delighted and honoured to find ourselves among such great sources on this list, which was posted by the buys at <a href="http://www.gambrinous.com/">Gambrinous</a>
<li><b><a href="http://www.instigatorblog.com/how-to-raise-startup-financing/">How to Raise Start-Up Financing</a></b> &#8211; We prefer to bootstrap wherever possible, but Ben Yoskovitz has a series of articles on building a bidness with Other People&#8217;s Money
<li><b><a href="http://www.untoldentertainment.com/boards/viewtopic.php?f=27&#038;t=181">Blender Resources</a></b> &#8211; <a href="http://www.untoldentertainment.com/boards/memberlist.php?mode=viewprofile&#038;u=121">lanzoma</a> composed a list of links to help developers who are new to Blender, a free 3D software package
<li><a href="http://biztools.pbworks.com/">Business Resources for Startups</a> &#8211; a FANTASTIC list of links across the full spectrum of concerns for start-up companies, game studios included
<li><a href="http://blog.princeporter.com/awesome-sources-for-game-music/692/">Awesome Sources for Game Music</a> &#8211; a list of links and mini-reviews for various music stock &#038; service providers
<li><a href="http://www.accreditedonlinecolleges.org/blog/2009/50-awesome-open-courses-for-web-writers/">50 Awesome Open Courses for Web Writers</a> &#8211; Make your corporate game blog sing!
<li><a href="http://jeez.eu/2009/10/20/all-the-developers-books-you-ever-wanted/">All the Developer&#8217;s Books You Ever Wanted</a> &#8211; A great list from Jeez.com of free pdf instructional books on a variety of languages and technologies.
<li><b><a href="http://www.gaminghorror.net/indie-game-developer-resources/">Indie Game Developer Links</a></b> &#8211; Gaming Horror maintains his own superb list similar to this one.
</ul>
<p>If you have lists of links that you want to share, leave a comment and i&#8217;ll add them to this list!
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F04%2Fstart-me-up-great-lists-of-links%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F04%2Fstart-me-up-great-lists-of-links%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/04/start-me-up-great-lists-of-links/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1645&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/04/start-me-up-great-lists-of-links/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial: Understanding Classes in AS3 Part 1</title>
		<link>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 13:31:29 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1527</guid>
		<description><![CDATA[A reader named Ryan (no relation) recently requested that we create a post to help him and other beginners understand Classes. So this is an ultra-gentle primer on Classes &#8211; what they are, when to use them, and how to write them. If you&#8217;re stuck in an Actionscript 2 rut, or you&#8217;re new to Actionscript [...]]]></description>
			<content:encoded><![CDATA[<p>A reader named Ryan (no relation) recently requested that we create a post to help him and other beginners understand Classes.  So this is an ultra-gentle primer on Classes &#8211; what they are, when to use them, and how to write them.  If you&#8217;re stuck in an Actionscript 2 rut, or you&#8217;re new to Actionscript 3 and it&#8217;s <em>blowing your mind</em>, this should help ease you in a bit better.</p>
<p>The path i travelled from complete code newbie to the shock-inducing AS3 god that i am today ( er &#8230; <em>hardly</em>) went like this:</p>
<p><b>Beginner</b> &#8211; i put my Actionscript code <em>everywhere</em> &#8211; on different frames of the timeline, embedded in nested MovieClips &#8211; if i can write code on it, i do.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/spaghetti.jpg" alt="Spaghetti Code"></p>
<p>That&#8217;s called &#8220;spaghetti code&#8221;, and it makes your life as a programmer difficult.
</p></div>
<p><b>Intermediate</b> &#8211; Scattering code through my Flash movie makes my project an enormous pain to manage and update.  i&#8217;m going to put ALL of my code on the very first frame of my movie, so i only have to look in one place for it.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/firstFrame.jpg" alt="First Frame"></p>
<p>Placing all your code in Frame 1 was a good idea with AS2, but you can do better
</p></div>
<p><b>Pro</b> &#8211; i don&#8217;t understand Classes, but i have to take the plunge somehow.  i&#8217;m going to move all of my first-frame code into a separate .as Class file. i hope that&#8217;ll be the first step to breaking everything up into Classes.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/codeGoesHere.jpg" alt="Code Goes Here"></p>
<p>This is what we&#8217;re striving for
</p></div>
<p><b>Shock-Inducing AS3 God</b> &#8211; Man almighty &#8211; packing an entire project&#8217;s code into one .as Class file makes it an enormous pain to manage and update.  i&#8217;m going to identify little modules of code and split them off into their own .as Class files.  Hey &#8230; now i&#8217;m starting to use Classes like a superstar!  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/god.jpg" alt="God"></p>
<p>Not *THE* God &#8211; just *A* god
</p></div>
<h2>The Road to Pro</h2>
<p>In this tutorial, we&#8217;ll help you move from <b>Intermediate</b> to <b>Pro</b> by moving your first-frame code to its own Class in a separate .as file.  In Part 2, we&#8217;ll take you from <b>Pro</b> to <b>God</b> by discussing ways in which you can split that enormous file up into smaller Classes, and how to figure out which pieces would make good Classes.</p>
<p>Many of the AS3 tutorials you see on the Internatz are written so that you can just paste the code into the first frame and compile the project.  This is because it&#8217;s easier on the tutorial writers &#8211; they assume that you know how to re-write that code to put it in its own Class.  But what if you don&#8217;t know?</p>
<p>Let&#8217;s take a piece of frame code, and bring it outside our Flash .fla file into a separate .as Class file.  Here&#8217;s the frame code we&#8217;ll be using:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> speed:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #000000; font-weight: bold;">var</span> ball:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> Ball<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>;
&nbsp;
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> moveBall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  ball.<span style="color: #006600;">x</span> += speed;
  ball.<span style="color: #006600;">y</span> += speed;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This code grabs a MovieClip called &#8220;Ball&#8221; from the library and places it on the stage.  On every frame, the ball moves to the right 5 pixels and down 5 pixels.  We&#8217;re going to pretend that this code exists on Frame 1 of our Flash movie.  (<em>Note: you don&#8217;t need to copy/paste this code and try it, but go ahead.  If you&#8217;re not sure how to create a ball MovieClip and link it in the library, you might be in over your head already.  Read up a bit on super-basic Flash stuff, and we&#8217;ll see you again shortly.</em>)</p>
<p>Now let&#8217;s create an empty, external .as Class file to hold all this code.  So your project will consist of two files: the Flash .fla file, which has a Ball Movieclip in the library, and a Class file with an .as extension.  This isn&#8217;t anything fancy &#8211; an .as file is just a plaintext (.txt) file that you save with a .as extension.  So yes, you can even write this file using Notepad or your favourite text editor.</p>
<p>The .fla and the .as file should exist side-by-side in the same directory.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/sideBySide.jpg" alt="Side by side"></p>
<p>Same directory.  We can get fancy later.
</p></div>
<h2>The Care and Feeding of Classes</h2>
<p>Classes demand a particular structure:</p>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructure.jpg" alt="AS3 Class Structure Diagram"></p>
</div>
<p>Notice a few things:</p>
<ol>
<li><font color="#009900">Package</font> (whatever that is) wraps the whole thing
<li>the <font color="#003399">Import statements</font> (whatever <em>they</em> are) come next
<li>the <font color="#FF3366">Class Definition</font> (huh?) wraps everything else
<li>the <font color="#CC6600">Instance Variables</font>, <font color="#660066">Constructor Function</font> and other methods/functions are tucked inside the <font color="#FF3366">Class</font> wrapper.  You can put these three things in any order, but this is how you&#8217;ll usually see them in the wild.
</ol>
<h2>Package</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructurePackage.jpg" alt="AS3 Class Structure Package Diagram"></p>
</div>
<p>Let&#8217;s deal with that Package thing first.  The Package declaration is a way of grouping all your Classes together. Let&#8217;s say you&#8217;re working on a project with Bob. You have a class called &#8220;Stuff&#8221;, and Bob has a class called &#8220;Stuff&#8221;.  By uniquely naming your Package, your classes won&#8217;t conflict.</p>
<p>But are you working with Bob right now?  Are you on a big project with multiple programmers?  No.  So don&#8217;t worry about it.  Forget Bob &#8211; just start with a generic Package declaration, which looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #808080; font-style: italic;">// All the other stuff goes in here, between those curly brackets</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>(note that &#8220;package&#8221; has a lower-case &#8220;p&#8221; &#8230; i&#8217;ve used an upper-case &#8220;P&#8221; in the article just to make the word pop out at you)</p>
<h2>Import Statements</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureImportStatements.jpg" alt="AS3 Class Structure Import Statements Diagram"></p>
</div>
<p>Different parts of the ActionScript 3 language are already split up into packages.  When you put your code on a frame in Flash, all of those packages are automatically accessible.  You don&#8217;t have to do any extra work.</p>
<p>But when you work outside Flash, you have to tell the compiler to go grab portions of the AS3 language before you can use them.  So if you say this in an .as Class file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myclip:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>the compiler will throw you an error, effectively saying &#8220;What&#8217;s a MovieClip??&#8221;</p>
<p>Use an Import Statement to explicitely tell the compiler that you are using a certain part of the AS3 language.  This is a giant pain in the ass, and a real roadblock when you&#8217;re starting out. As a beginner, you don&#8217;t know the package names you&#8217;ll need.  This is just a painstaking trial-and-error period of looking in the documentation, or referring to other people&#8217;s code, and eventually you&#8217;ll memorize this stuff.  (Or you can use a program like <a href="http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page">Flash Develop</a>, which automagically figures some of it out for you.)</p>
<p>So if you use a line like this later on in your code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myclip:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>you have to include an Import Statement like this at the top of the file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;</pre></td></tr></table></div>

<p>(note: small &#8220;i&#8221; on &#8220;import&#8221;)</p>
<p>It&#8217;s like you&#8217;re saying &#8220;OK, compiler &#8211; we&#8217;re going to talk about fish today.  Go get that fish book out of the library so that we&#8217;re all on the same page.&#8221;</p>
<h2>Class Definition</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureClassDefinition.jpg" alt="AS3 Class Structure Class Definition Diagram"></p>
</div>
<p>This is where you actually start writing your Class.  Here&#8217;s the format:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// The Instance Variables, the Constructor Function and Methods go here</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break down that line piece by piece:</p>
<p><font color="#FF3366"><b>public</b> Class Main extends MovieClip</font></p>
<p>We don&#8217;t really need to bother with understanding public vs. private vs. protected vs. internal.  Those keywords are all in the same family, but here&#8217;s what you need to know: you gotta use &#8220;public&#8221; here.  Don&#8217;t worry about the other possibilities until you&#8217;ve got a handle on the basics.  If you don&#8217;t use &#8220;public&#8221; in the Class that replaces your first-frame code, you&#8217;ll get an error.</p>
<p><font color="#FF3366">public <b>Class</b> Main extends MovieClip</font></p>
<p>We&#8217;re writing a Class!  i&#8217;m getting all tiingly.</p>
<p><font color="#FF3366">public class <b>Main</b> extends MovieClip</font></p>
<p>This is the name of the Class.  Feel free to use your creativity here &#8211; you can call it whatever you like.  But there are a few important rules:</p>
<ol>
<li>You must use standard naming conventions. Don&#8217;t start the name with a number, and stay away from spaces and special characters.
<li>Use a capital letter to begin your Class name
<li>When you save the file, the file MUST have the same name as your Class.  If your Class is called Main, save the file as Main.as.  If it&#8217;s called MyProject, save the file as MyProject.as.   If you don&#8217;t do this, there will be trouble.
</ol>
<p><font color="#FF3366">public class Main <b>extends</b> MovieClip</font></p>
<p>In earlier attempts to understand Classes and Object Oriented Programming (OOP), you may have heard of the concept of &#8220;inheritance.&#8221;  That&#8217;s what the &#8220;extends&#8221; keyword is all about.  Without exploring too deeply, we need to <em>extend</em> / subclass / inherit from an ActionScript 3 Class called MovieClip.  Why?  Because if we don&#8217;t, we&#8217;ll get yelled at.  So just do it.  Strive to understand it later.</p>
<p><font color="#FF3366">public class Main extends <b>MovieClip</b></font></p>
<p>That&#8217;s the Class we&#8217;re inheriting from.  It&#8217;s like saying &#8220;my Class called Main can do everything a MovieClip can do, and more.&#8221;</p>
<h2>Instance Variables</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureInstanceVariables.jpg" alt="AS3 Class Structure Instance Variables Diagram"></p>
</div>
<p>Instance Variables, sometimes called Fields, are variables that you need to access throughout your Class.  If you&#8217;re familiar with the idea of scope, you&#8217;ll know that when we do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> myFirstFunction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">var</span> myVariable:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;someValue&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>we&#8217;ll get an error if we try doing this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mySecondFunction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>myVariable<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s because myVariable is <em>scoped</em> to myFirstFunction.  It doesn&#8217;t exist outside that function.</p>
<p>If you want variables to be accessible to all the different functions in your Class, you declare them in the orange area on the diagram.  You&#8217;ll also need to decide whether you&#8217;re making these variables public, private, internal, protected, etc.  This is not a decision you need to make when all your code is on the first frame.  OOP purists would absolutely FREAK OUT if they were reading this, but i&#8217;m going to recommend you make all your variables public for now.  It&#8217;s the least error-prone approach when you&#8217;re learning, and when you know more, you can get a little fancier.</p>
<p>So here&#8217;s what a typical Instance Variable would look like at this point in the .as Class file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> myVariable:<span style="color: #0066CC;">String</span>;</pre></td></tr></table></div>

<p>Notice that we&#8217;re not even setting a value yet.  We can, but we don&#8217;t have to.  We&#8217;re just reserving a spot in the computer&#8217;s memory for this variable so that the whole Class can use it.</p>
<h2>The Constructor Function</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureConstructorFunction.jpg" alt="AS3 Class Structure Constructor Function Diagram"></p>
</div>
<p>This Main Class will be what&#8217;s called the &#8220;point of entry&#8221; into our Flash program.  It&#8217;s the first place where code will start being activated.  A Constructor Function is like the point of entry in any Class.  It&#8217;s the first function that&#8217;s fired when a Class is encountered by the interpreter.</p>
<p>Here&#8217;s what our Constructor Function should look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As with Classes, you have to follow a few rules with your Constructor Function:</p>
<ol>
<li>The Highlander rule: &#8220;there can be only one&#8221;.  i believe other languages allow you to have multiple Constructor Functions, but AS3 does not.
<li>The name of the Constructor function must be the same as the name of the Class.
<li>Make sure it&#8217;s public.
<li>Don&#8217;t return a value &#8211; not even &#8220;void&#8221;.  Constructor Functions are not allowed to return a value.
</ol>
<h2>Putting It All Together</h2>
<p>Let&#8217;s slap it all together and see what we&#8217;ve got:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// instance variables go here</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
             <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;It works!&quot;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>  
&nbsp;
        <span style="color: #808080; font-style: italic;">// other functions can go here</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Super!  We&#8217;ve got the shell of a Class that we can use as our point of entry. (Make sure it&#8217;s saved as &#8220;Main.as&#8221;, or whatever you called your Class.)  We&#8217;re just missing one step.  We have to <em>tell</em> Flash that instead of looking to the first frame of the movie as our point of entry, we want it to look at the &#8220;Main&#8221; Class.</p>
<p>Open up the Properties panel in Flash and look for an empty field labelled &#8220;Document class:&#8221; (CS3) or &#8220;Class:&#8221; (CS4).</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/documentClassCS3.jpg" alt="Document Class Flash CS3"></p>
<p>This is what it looks like in Flash CS3
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/documentClassCS4.jpg" alt="Document Class Flash CS4"></p>
<p>&#8230; and in CS4
</p></div>
<p>Note: you won&#8217;t be able to use this feature unless you&#8217;re targeting Flash Player 9 or above, with ActionScript 3 or above.</p>
<p>Type &#8220;Main&#8221; (without the quotes) into this field.  Then save and compile.  You should see the trace statement &#8220;It works!&#8221; in the Ouput panel.</p>
<h2>All the Rest</h2>
<p>Now that we have Flash looking to an external .as file to get the party started, let&#8217;s port over some of that first-frame code into our class.  i&#8217;ll paste the clean version first, and then an identical copy with extensive comments so that you can follow along at home:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ball:<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
             ball = <span style="color: #000000; font-weight: bold;">new</span> Ball<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
             addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>;
             ball.<span style="color: #006600;">speed</span> = <span style="color: #cc66cc;">5</span>;
&nbsp;
             addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moveBall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
                ball.<span style="color: #006600;">x</span> += ball.<span style="color: #006600;">speed</span>;
                ball.<span style="color: #006600;">y</span> += ball.<span style="color: #006600;">speed</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// This is the commented version.</span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// since both Ball and Main are MovieClips,</span>
              <span style="color: #808080; font-style: italic;">// we have to import the MovieClip Class </span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>; <span style="color: #808080; font-style: italic;">// Later on, we use the Event Class, so</span>
             <span style="color: #808080; font-style: italic;">// we need to import it here.</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// See how the Class definition and import statements</span>
        <span style="color: #808080; font-style: italic;">// are inside the package curly brackets?</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ball:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// We declare &quot;ball&quot; up here</span>
             <span style="color: #808080; font-style: italic;">// so that the whole Class can refer to it</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
             <span style="color: #808080; font-style: italic;">// This is the constructor function!</span>
&nbsp;
             ball = <span style="color: #000000; font-weight: bold;">new</span> Ball<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Since we already declared &quot;ball&quot;</span>
               <span style="color: #808080; font-style: italic;">// as an instance variable, we can just refer to it</span>
               <span style="color: #808080; font-style: italic;">// directly without redefining it</span>
             addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Where does ball get added?  Main</span>
               <span style="color: #808080; font-style: italic;">// is representing our Flash movie's main timeline,</span>
               <span style="color: #808080; font-style: italic;">// so that's where the ball appears</span>
             ball.<span style="color: #006600;">speed</span> = <span style="color: #cc66cc;">5</span>;
&nbsp;
             addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
             <span style="color: #808080; font-style: italic;">// note: no need for the word &quot;this&quot;, in case you</span>
             <span style="color: #808080; font-style: italic;">// were wondering</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moveBall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
                ball.<span style="color: #006600;">x</span> += ball.<span style="color: #006600;">speed</span>;
                ball.<span style="color: #006600;">y</span> += ball.<span style="color: #006600;">speed</span>;
                <span style="color: #808080; font-style: italic;">// Again, because &quot;ball&quot; was defined up top, </span>
                <span style="color: #808080; font-style: italic;">// the moveBall method can refer to it</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h2>Are You Smart Yet?</h2>
<p>Hopefully, you know enough now to port your whole wad of first-frame code to a separate .as Class file.  That&#8217;s a baby step towards actually using the power of Classes to save yourself time in the future, and to keep your code organized, updatable, and a pleasure to re-visit.</p>
<p>In the next part of this tutorial, we&#8217;ll look at ways you can pick through your Wall of Code and identify which pieces you can could out into their own Class files.</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F25%2Ftutorial-understanding-classes-in-as3-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F25%2Ftutorial-understanding-classes-in-as3-part-1%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1527&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		</item>
		<item>
		<title>The Making of Bloat.</title>
		<link>http://www.untoldentertainment.com/blog/2009/05/05/the-making-of-bloat/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/05/05/the-making-of-bloat/#comments</comments>
		<pubDate>Tue, 05 May 2009 20:44:36 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[TOJam]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1266</guid>
		<description><![CDATA[i created Bloat. at TOJam, the Toronto game jam, this past weekend. Click here to play Bloat.! i spent a lot less time on this year&#8217;s game than i did with my other entries, Two By Two and Here Be Dragons &#8211; Bloat. clocked in at around twenty-two hours. i took time out of the [...]]]></description>
			<content:encoded><![CDATA[<p>i created <b>Bloat.</b> at TOJam, the Toronto game jam, this past weekend.</p>
<div class="displayed">
<p><a href="http://www.untoldentertainment.com/blog/2009/05/05/bloat/"><img src="http://www.untoldentertainment.com/blog/img/projects/bloat/bloat.jpg" alt="Untold Entertainment's fun Flash game Bloat."></a>
</p>
<p><a href="http://www.untoldentertainment.com/blog/2009/05/05/bloat/">Click here to play Bloat.!</a>
</div>
<p>i spent a lot less time on this year&#8217;s game than i did with my other entries, <b>Two By Two</b> and <b>Here Be Dragons</b> &#8211; <b>Bloat.</b> clocked in at around twenty-two hours.  i took time out of the schedule for luxuries like eating, sleeping, and going to the bathroom.  As Laurence Olivier said to John Hurt in the 1983 version of Shakespeare&#8217;s <b>King Lear</b>, &#8220;i&#8217;m getting too old for this shit.&#8221;  Still, at the risk of being labelled a big girl&#8217;s blouse, i <em>did</em> pull an all-nighter from 11AM Saturday to 1AM Monday, racking up a total of two and a half hour&#8217;s sleep.  i still got it, baby.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_05_05/harenmahkeester.jpg" alt="Harenmahkeester">
</p>
<p>The Innovation Toronto space got about 79% weirder since <a href="http://www.untoldentertainment.com/blog/2008/05/12/veni-vidi-video-game/">last year</a> (thanks again to <a href="http://www.flickr.com/photos/lestrade/sets/72157617460374223/">Ben Rivers</a> for the environment photos!)
</div>
<h2>Peer Pressure</h2>
<p>When i got to the jam on Friday night, i had my plan down semi-solid: armed with a MacBook and a copy of <b><a href="http://www.amazon.com/Beginning-iPhone-Development-Exploring-SDK/dp/1430216263/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1241552060&#038;sr=8-1">Beginning iPhone Development</a></b> (i&#8217;m on chapter four!), i was going to churn out a few small not-really-game iPhone apps by the end of the weekend.  So i sat down, cracked the book, and started reading all about model view controllers and Interface Builder, and view states and multi-view panel buttons and snoooooooooooore.  All around me, enthusiastic jammers were yelling stuff like &#8220;score!&#8221; and &#8220;player!&#8221; and &#8220;enemies!&#8221; and &#8220;hot coffee!&#8221; at each other, and i started to feel a little left out.</p>
<p>So i calmly closed the book, and opened up the trial copy of Unity that i&#8217;d just downloaded.  i already knew how to make a box fall from the sky and land on the ground.  That&#8217;s, like, halfway to a game &#8230; right?  And all around me, the game banter continued: &#8220;extra lives!&#8221; &#8220;state machine!&#8221; &#8220;Robotron controls!&#8221; &#8220;EA spouse!&#8221;   The din was deafening, and i&#8217;d forgotten my headphones.</p>
<p>This was no place to learn new software.  This was no place to struggle with an unfamiliar operating system.  This was the mother-effing Toronto Game Jam, and i was there to make a GAME.  So i lit the MacBook on fire and threw it across the room, took off my pants and tied them around my hair like a headband, booted up my PC laptop and opened Flash.  i was there to make a game, and nothing was going to stop me.</p>
<h2>A Dark Room</h2>
<p>i&#8217;d promised myself that this year, i was going to go into the game jam cold, with no game ideas and a commitment to the year&#8217;s theme.  The theme this year was &#8220;scale&#8221;.  Little did i realize that actually cooking up an idea right there on the spot was going to be challenging.  A weekend-long jam requires a lot of planning; you have to choose a game idea that you can build in that tight amount of time, and what&#8217;s worse, it actually has to be <em>playable</em>.  And on top of that, and completely optionally, it also has to be <em>fun</em>.  Since fun is that just-out-of-reach jewel that we game developers stretch for when we have a lot more time to think it through, the jam presents a significant level of challenge.</p>
<p>So i went into a dark room and thought for nearly two hours.  When i emerged, i had a concept, but i didn&#8217;t have any idea whether it was good or not.  The only way to find out was to build it.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_05_05/bookChair.jpg" alt="A chair made of books">
</p>
<p>My brainstorming room was home to a thousand delapidated office chairs, and this little number made entirely out of books.  It wasn&#8217;t nearly as inspiring as it looks.
</p></div>
<h2>A Little Help</h2>
<p>When i first attended the game jam, i didn&#8217;t have any libraries or prior code.  i just opened up a fresh Flash project and started building.  i knew that other people were using pre-existing libraries, and i considered that &#8220;cheating&#8221;.  These days, though, i&#8217;ve caught glimpses of how other programs work, and i now appreciate the number of tasks that Flash automates for the developer. For example, other languages require lines and lines of code just to draw something to the screen.  Flash handles this for you.  So really, i had been using a tool to speed up development all along.</p>
<p>So from there, and with the enormous asspains presented by Actionscript 3, i was able to justify the use of a few more libraries this year, which helped me focus on the core game experience.  Here are the snippets i used:</p>
<ol>
<li>Corey O&#8217;Neil&#8217;s excellent <a href="http://www.coreyoneil.com/Flash/CDK/index.html">Collision Detection Kit</a> (thanks, Corey!)
<li>Jeff Gold&#8217;s audio classes for playing music and sound effects (Jeff is our game developer here at Untold Entertainment, but he couldn&#8217;t make the jam this year.  Something about nice weather and &#8220;having a life&#8221;.)
<li>Our Game Screen Manager class, which handles switching between screens and popping up prompts and dialogues
<li>Our Helper class, the guts of which you can copy-paste from our <b><a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash 911</a></b> series to use in your own projects
<li>A fireworks particle effect that i found online and ported from AS2 to AS3 for our fun, crime-themed puzzle game <b><a href="http://www.untoldentertainment.com/blog/kahoots-designer-diary">Kahoots™</a></b>, which we modeled entirely in clay
</ol>
<h2>The Result</h2>
<p>i&#8217;m very happy with how <b>Bloat.</b> turned out!  The biggest brainwave of the weekend was looking at all of the features i had in the game, and deciding to split them out across multiple tutorial levels. i remember watching in dismay at last year&#8217;s jam as people walked up to <b>Here Be Dragons</b>, clicked until they got some ship rigging stuck in the sea monster&#8217;s throat, couldn&#8217;t figure out what to do, and floated away to try another game.  If there&#8217;s one thing i&#8217;ve learned is a weakness of mine as a designer, it&#8217;s getting what&#8217;s in my head out of my head.</p>
<p>The Closed Public Beta team for <b>Kahoots™</b> is doing an excellent job of pointing out where <b>Kahoots</b> game needs more explanation.  The best advice i can offer you, if you have this problem too, is:</p>
<ol>
<li>Make your game more simple
<li>Introduce escalations gradually
<li>Treat your audience as if they ride the short bus
</ol>
<p>Your players aren&#8217;t stupid.  (Well, most aren&#8217;t &#8230; )  But they can&#8217;t read minds either.  By dumbing down your instructions and game progress into digestible chunks, you&#8217;re allowing the smart people to sail through to the more difficult content, without leaving your less savvy players in the dust.  Either way, you ensure that absolutely everything that was in your brain is out of your brain, and everyone&#8217;s on the same footing.</p>
<p>Until i develop and convince everyone to purchase and install my mind control ray, this approach will have to do.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_05_05/ryanCreighton.jpg" alt="Ryan Creighton at TOJam 4">
</p>
<p>Special thanks to <a href="http://www.flickr.com/photos/brendanlynch/">Brendan Lynch</a> for using the &#8220;demented&#8221; filter on his camera
</div>
<ul>
<li><b><a href="http://www.untoldentertainment.com/boards/viewforum.php?f=39">Talk about Bloat. on the message boards!</a></b>
</ul>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F05%2F05%2Fthe-making-of-bloat%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F05%2F05%2Fthe-making-of-bloat%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/05/05/the-making-of-bloat/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1266&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/05/05/the-making-of-bloat/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Tutorial: Pause Your Game in Flash AS3</title>
		<link>http://www.untoldentertainment.com/blog/2009/04/07/tutorial-pause-your-game-in-flash-as3/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/04/07/tutorial-pause-your-game-in-flash-as3/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 13:52:04 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1072</guid>
		<description><![CDATA[This tutorial teaches you how to set up your game so that everything stops when you hit the &#8220;pause&#8221; button. This is a bit of structural/best-practices advice i gleaned when i was learning C# programming in XNA for the Xbox 360. Flash/Actionscript behaves a little differently than other programs. The way many games are programmed, [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial teaches you how to set up your game so that everything stops when you hit the &#8220;pause&#8221; button.</p>
<p><center><br />

<object width="550" height="300">
<param name="movie" value="http://www.untoldentertainment.com/blog/tutorials/gameLoop/gameLoop.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<param name="allowScriptAccess" value="always"></param>
<embed type="application/x-shockwave-flash" width="550" height="300" src="http://www.untoldentertainment.com/blog/tutorials/gameLoop/gameLoop.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
<br />
</center></p>
<p>This is a bit of structural/best-practices advice i gleaned when i was learning C# programming in XNA for the Xbox 360.  Flash/Actionscript behaves a little differently than other programs. The way many games are programmed, there is one giant loop, and everything takes place in that loop.</p>
<p>Flash encourages you to throw code all over the place &#8211; on frames, on movie clips, in separate .as files &#8211; but Flash coders could really benefit from the sanity and cleanliness that a master loop brings.</p>
<h2>Here Comes the Code</h2>
<p>At its most basic, this is what we&#8217;re talking about:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> isPaused:<span style="color: #0066CC;">Boolean</span>; <span style="color: #808080; font-style: italic;">// This is the variable that holds our paused/unpaused state</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Unpause the game:</span>
			isPaused = <span style="color: #000000; font-weight: bold;">false</span>; 
			<span style="color: #808080; font-style: italic;">// Add an event listener to keep an eye (ear?) on the built-in ENTER_FRAME event.</span>
			<span style="color: #808080; font-style: italic;">// Every time Flash processes a frame tick, doMasterGameLoop will execute:</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, doMasterGameLoop<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doMasterGameLoop<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// This is the main game loop that does all the work.  It's the</span>
			<span style="color: #808080; font-style: italic;">// &quot;gateway&quot; to everything our game does.  If the game is paused,</span>
			<span style="color: #808080; font-style: italic;">// nothing in the game loop happens.</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>isPaused<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				draw<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>			
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Tweak all your numbers and math stuff here</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> draw<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Update all your graphics here			</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>So all you need to do is rig up a &#8220;PAUSE&#8221; button that toggles the isPaused variable when you click it.  When the game is paused, stuff won&#8217;t happen.  Unpaused?  Stuff happens.</p>
<h2>Whiling Away the Hours</h2>
<p>If you come from another programming language, you might be accustomed to seeing the game loop built in a <em>while</em> statement.  Actionscript is a different beast when it comes to <em>while</em>.  The whole program runs on frame ticks, meaning it will execute your code according to the frame rate you&#8217;ve set in your fla file.  A framerate of 12 fps (frames per second) means that Flash will try to run your code 12 times a second. &#8220;Try&#8221; is the operative word &#8211; the code will only execute according to the processor&#8217;s ability.  </p>
<p>There are ways to get your code to execute more quickly than the set frame rate, but for our purposes, since we&#8217;re talking about the ENTER_FRAME event, we&#8217;re talking about code executing on frame ticks according to the set frame rate.</p>
<p>If you try to put your master game loop in a <em>while</em> statement in Actionscript 3, here&#8217;s what&#8217;ll happen:</p>
<ol>
<li>first frame tick: code is executed
<li>interpreter hits the <em>while</em> statement
<li>there is no escape or break condition in the <em>while</em> statement, so Flash keeps trying to execute those commands <em>in the same frame tick</em>
<li>after a prescribed number of seconds, Flash poops out and you get an endless loop error
</ol>
<p>So instead, think of Flash as having one big automatic <em>while</em> loop, and you&#8217;re tucking all of your code inside it.</p>
<h2>Keep Em Separated</h2>
<p>To a native flashcoder, it may look odd to see two separate processes &#8211; update and draw &#8211; in the game loop.  The way games in other languages work is that first, an <b>update</b> process figures out where everything in the game should be &#8211; bullets, enemies, the player character, the power-ups &#8211; and then in the <b>draw</b> process, all of the graphics for those things get refreshed on the screen in their (sometimes) new positions. </p>
<p>One mixed blessing with Flash is that code and graphics are married.  If you update the x position of a movieclip, you&#8217;re updating both the <em>logic</em> and the <em>display</em>.  They&#8217;re fused together.  This is one of the reasons Flash makes for rapid prototyping, but it can also mess you up when you&#8217;re trying to do something complicated.</p>
<p>The hard lesson i&#8217;ve learned over many years of development is to divorce your code from your graphics, wherever it makes sense.  Let&#8217;s say you have bullets in your game.  You might be tempted to have all your little bullet01, bullet02 &#8230; bulletn movieclips on the stage, and then update their x/y co-ordinates to move them around.  Then, when you want to find out where your bullets are, you loop through everything on the stage that&#8217;s of type &#8220;bullet&#8221; and have the bullet movieclips report their positions.</p>
<p>A better way to do it is to create a Bullet object (class).  It has properties for xPosition, yPosition and graphic.  In the <b>update</b> process, you change the xPosition and yPosition properties based on where the bullet should be.  In the <b>draw</b> process, you set the positions of the bullets&#8217; graphics:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">graphic.<span style="color: #006600;">x</span> = xPosition;
graphic.<span style="color: #006600;">y</span> = yPosition;</pre></td></tr></table></div>

<p>There are better examples of where this separation of logic and display is useful &#8211; i&#8217;m just introducting the concept simply for now.</p>
<h2>Pausing Sprites</h2>
<p>One of the other things Flash does to save you time is to give nearly every on-screen object an automatically-looping timeline. If you create a 3-frame MovieClip object and put it on the stage, it will automatically animate.  Good!  But since it&#8217;s not controlled by code, but rather by some secret behind-the-scenes process, it won&#8217;t stop when you hit the &#8220;pause&#8221; button.  Not so good!</p>
<p>What you really want to do, and it takes quite a bit more work, is to create a class to represent your on-screen object (we&#8217;ll call them &#8220;sprites&#8221;, although the term has slightly different connotations in ActionScript), and keep track of which frame the sprite should display.  So on your <em>update</em> process, you figure out which frame to display, and on your <em>draw</em> process, you send the MovieClip to that frame.  That&#8217;s a far better example of what i&#8217;m talking about re: separating code from display, but it&#8217;s a bit beyond the scope of this tutorial.  Let&#8217;s just see our pause button simply doing what it does one more time:  </p>
<p><center><br />

<object width="550" height="300">
<param name="movie" value="http://www.untoldentertainment.com/blog/tutorials/gameLoop/gameLoop.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<param name="allowScriptAccess" value="always"></param>
<embed type="application/x-shockwave-flash" width="550" height="300" src="http://www.untoldentertainment.com/blog/tutorials/gameLoop/gameLoop.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
<br />
</center></p>
<p>Hooray!  You can download the source code here:</p>
<p><a href="http://www.untoldentertainment.com/blog/tutorials/gameLoop/gameLoop.zip">http://www.untoldentertainment.com/blog/tutorials/gameLoop/gameLoop.zip</a></p>
<p>We&#8217;ll tackle programmatic sprites in another tutorial!</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F04%2F07%2Ftutorial-pause-your-game-in-flash-as3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F04%2F07%2Ftutorial-pause-your-game-in-flash-as3%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/04/07/tutorial-pause-your-game-in-flash-as3/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1072&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/04/07/tutorial-pause-your-game-in-flash-as3/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Tutorial: Pull Twitter Updates into Flash</title>
		<link>http://www.untoldentertainment.com/blog/2009/04/02/tutorial-pull-twitter-updates-into-flash/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/04/02/tutorial-pull-twitter-updates-into-flash/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 16:11:02 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1042</guid>
		<description><![CDATA[That&#8217;s right &#8211; i said &#8220;Fwitter&#8221; It&#8217;s a very simple thing to display your Twitter updates in a Flash widget. We&#8217;re going to use the xml feed, because that&#8217;s what i&#8217;m most comfy with. Unfortunately, you can&#8217;t grab the xml feed from Twitter&#8217;s site, because they&#8217;ve locked their crossdomain policy file &#8230; but you can [...]]]></description>
			<content:encoded><![CDATA[<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_04_02/fwitter.jpg" alt="Fwitter">
</p>
<p>That&#8217;s right &#8211; i said &#8220;Fwitter&#8221;
</p></div>
<p>It&#8217;s a very simple thing to display your Twitter updates in a Flash widget. We&#8217;re going to use the xml feed, because that&#8217;s what i&#8217;m most comfy with.  Unfortunately, you can&#8217;t grab the xml feed from Twitter&#8217;s site, because they&#8217;ve locked their crossdomain policy file &#8230; but you can pull down the data using any other damned process.  Here&#8217;s the basic flow you have to follow:</p>
<ol>
<li>Hit a PHP page or javascript function from Flash (we&#8217;ll use PHP)
<li>Parse the XML data that the call returns
<li>Display the goodies in your Flash app
</ol>
<h2>Here Comes the Code</h2>
<p>This is the Actionscript 3 code you&#8217;ll need:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**********************************************
	 * 
	 *  This fun Twitter widget is brough to you
	 *  by Untold Entertainment Inc, and 
	 *  peanut butter!  Peanut butter: a gooey,
	 *  yummy snack that might kill you.
	 * 
	 *  http://www.untoldentertainment.com
	 * 
	 **********************************************/</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// Here are all the goodies you're going to need </span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> twitterXML:<span style="color: #0066CC;">XML</span>; <span style="color: #808080; font-style: italic;">// This holds the xml data</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Put your Twitter username here.  For example, ours is &quot;untoldEnt&quot; :</span>
			<span style="color: #000000; font-weight: bold;">var</span> myTwitterID:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;myTwitterID&quot;</span>; 
			<span style="color: #808080; font-style: italic;">// Put the path to your php script here:</span>
			<span style="color: #000000; font-weight: bold;">var</span> twitterPHPScriptPath:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;http://www.yourdomain.com/thePathToYourPHPScript&quot;</span>;
			<span style="color: #808080; font-style: italic;">// Fire the loadTwitterXML method, passing it the url to your Twitter info:</span>
			loadTwitterXML<span style="color: #66cc66;">&#40;</span>twitterPHPScriptPath + <span style="color: #ff0000;">&quot;?twitterId=&quot;</span> + myTwitterID<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loadTwitterXML<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">URL</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> urlLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// When all the junk has been pulled in from the url, we'll fire finishedLoadingXML:</span>
			urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, finishLoadingXML<span style="color: #66cc66;">&#41;</span>;
			urlLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">URL</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;			
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> finishLoadingXML<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// All the junk has been pulled in from the xml!  Hooray!</span>
			<span style="color: #808080; font-style: italic;">// Remove the eventListener as a bit of housecleaning:</span>
			<span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, finishLoadingXML<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Populate the xml object with the xml data:</span>
			twitterXML = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			showTwitterStatus<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> showTwitterStatus<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Uncomment this line if you want to see all the fun stuff Twitter sends you:</span>
			<span style="color: #808080; font-style: italic;">//trace(twitterXML);</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// Prep the text field to hold our latest Twitter update:</span>
			twitter_txt.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			twitter_txt.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Populate the text field with the first element in the status.text nodes:</span>
			twitter_txt.<span style="color: #0066CC;">htmlText</span> = twitterXML.<span style="color: #0066CC;">status</span>.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>And here&#8217;s the PHP code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$twitterId</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;twitterId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;twitterId&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #33
