<?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; AS3</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/as3/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>Hooray!  It&#8217;s Ponycorn P&#8217;toosday!</title>
		<link>http://www.untoldentertainment.com/blog/2011/07/11/hooray-its-ponycorn-ptoosday/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/07/11/hooray-its-ponycorn-ptoosday/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 04:12:54 +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[Canadian Media News]]></category>
		<category><![CDATA[Company News]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Original Games]]></category>
		<category><![CDATA[ponycorns]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3840</guid>
		<description><![CDATA[Today marks the iPad release of Sissy&#8217;s Magical Ponycorn Adventure! Now you can take Poo-Pants, Fluffybuns, Lady Fuzzwuzzle, Orangeboy, and (inaudible) with you wherever you go. Impress your extended family! Accost sour-looking strangers in waiting rooms! Play it for a pick-me-up when the boss isn&#8217;t watching! To celebrate, we&#8217;re showcasing the work of some dyed-in-the-wool [...]]]></description>
			<content:encoded><![CDATA[<p>Today marks the <a href="http://itunes.apple.com/us/app/sissys-magical-ponycorn-adventure/id445696590?ls=1&#038;mt=8" title="Sissy's Magical Ponycorn Adventure on the iPad">iPad release of <b>Sissy&#8217;s Magical Ponycorn Adventure</b></a>!  Now you can take Poo-Pants, Fluffybuns, Lady Fuzzwuzzle, Orangeboy, and (inaudible) with you wherever you go.  Impress your extended family!  Accost sour-looking strangers in waiting rooms!  Play it for a pick-me-up when the boss isn&#8217;t watching!</p>
<p>To celebrate, we&#8217;re showcasing the work of some dyed-in-the-wool ponycorn fans.</p>
<h2>Ponycorns at Anime North</h2>
<p>Mike Barltrop is an educator who showed Sissy&#8217;s Magical Ponycorn Adventure to his high school students so they&#8217;d be without excuse &#8211; if a 5-year-old could pull off a game, he can expect a lot more from them.  Mike enjoyed ponycorns so much that he painstakingly re-created a number of Cassie&#8217;s drawings and <em>dressed up as the game</em> for the Anime North nerdfest in Toronto:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/animeNorth.jpg" alt="Sissy's Magical Ponycorn Adventure Anime North costume"></p>
<p>For serious.
</p></div>
<p>To put Mike&#8217;s ponycorns passion into perspective, the game was released on May 25th.  Anime North started the following Friday and ran the length of the weekend.  And people at the conference <em>recognized Mike&#8217;s costume</em>.  Awesomazing!</p>
<h2>The Star&#8217;s Crayon Forgery Shenanigans</h2>
<p>We spotted one piece of fan art that was no less hilarious than Mike&#8217;s costume, but unbelievable for a different reason.  When the <a href="http://www.thestar.com/news/article/1001877--game-drawn-by-toronto-girl-5-becomes-online-sensation?bn=1&#038;sms_ss=twitter&#038;at_xt=4de8578f2d414d61,0">Toronto Star posted its story</a> about the ponycorns&#8217; meteoric rise to prominence, the online piece had two images in its gallery: a shot of Cassie playing the game, and THIS:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/forgery.jpg" alt="Sissy's Magical Ponycorn Adventure Toronto Star forgery"></p>
<p>Also for serious.
</p></div>
<p>It doesn&#8217;t take a keen eye to see that this image is actually a forgery, created by an adult employed at the Star.  When we saw this image on the site, we were a little baffled.  We have a stack of original crayon art from the game that we could have sent to the newspaper at a moment&#8217;s notice.  This kind of thing is in keeping with some of my other dealings with news media, like my <a href="http://www.untoldentertainment.com/blog/2008/12/04/ryan-creighton-on-city-news-at-6-with-dr-karl/" title="Ryan Henson Creighton on CityTV">interview with CityTV</a> from a while back where they grabbed one tiny soundbite from me to prove the point they were trying to make, and completely discarded everything else i said.  Hilarious!  If you don&#8217;t already watch the news and read the paper with a cocked eye, i hope you&#8217;ll start today.</p>
<p>We asked the Toronto Star to correct the article, and the picture was removed in short order.</p>
<p><b>UPDATE</b></p>
<p>Now it&#8217;s ME doing the retracting. The wacky lads over at <a href="http://www.shh-mom.com/shh-mom-in-the-ponycorn-world/">Shh-Mom.com</a> have confessed to penning the offending artwork which, in its original context, isn&#8217;t offending at all.  The folks at the Star must have grabbed it, thinking it to be genuine.  i&#8217;m glad we got to the bottom of this. Has it restored my faith in traditional news media?  No.  Question everything!</p>
<h2>Flannery&#8217;s Fanfic</h2>
<p>We&#8217;re saving the best for last on Ponycorn P&#8217;toosday.  9-year-old Flannery, proud owner of a <a href="http://untoldentertainment.com/store/products/Ponycorns-Mega%252dPack.html">plush ponycorns mega-pack</a>, sent us this fantastic storybook featuring the extended adventures of the ponycorns gang.  Enjoy your face off!</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/01.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/02.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/03.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/04.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/05.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/06.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/07.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/08.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/09.jpg" alt="Ponycorns Fanfic"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_07_12/10.jpg" alt="Ponycorns Fanfic"></p>
</div>
<h2>Get Some</h2>
<p>If this is your first exposure to ponycorns, <a href="http://itunes.apple.com/us/app/sissys-magical-ponycorn-adventure/id445696590?ls=1&#038;mt=8" title="Sissy's Magical Ponycorn Adventure on the iPad">pick up your copy on the iPad today</a>!  The game is also available on the <a href="http://appworld.blackberry.com/webstore/content/45781" title="Sissy's Magical Ponycorn Adventure on the BlackBerry Playbook">BlackBerry Playbook</a> and <a href="http://www.ponycorns.com/game.html" title="Sissy's Magical Ponycorn Adventure">in your browser</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%2F07%2F11%2Fhooray-its-ponycorn-ptoosday%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F07%2F11%2Fhooray-its-ponycorn-ptoosday%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/07/11/hooray-its-ponycorn-ptoosday/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3840&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/07/11/hooray-its-ponycorn-ptoosday/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Dear RIM Blackberry Playbook People: Please Put that Shit on a Button</title>
		<link>http://www.untoldentertainment.com/blog/2011/05/19/dear-rim-blackberry-playbook-people-please-put-that-shit-on-a-button/</link>
		<comments>http://www.untoldentertainment.com/blog/2011/05/19/dear-rim-blackberry-playbook-people-please-put-that-shit-on-a-button/#comments</comments>
		<pubDate>Thu, 19 May 2011 18:40:58 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Playbook]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3722</guid>
		<description><![CDATA[Dear RIM Blackberry Playbook People, Thank you for sending me a Playbook. i like it very much. i didn&#8217;t very much like the steps involved to put my work on the device, though. It was the most needlessly complicated thing i&#8217;ve had to do in all my life. i&#8217;d like to see the Playbook succeed, [...]]]></description>
			<content:encoded><![CDATA[<p>Dear RIM Blackberry Playbook People,</p>
<p>Thank you for sending me a Playbook.  i like it very much.  i didn&#8217;t very much like the steps involved to put my work on the device, though. It was the most needlessly complicated thing i&#8217;ve had to do in all my life. i&#8217;d like to see the Playbook succeed, but you need to put more effort into helping your developers succeed first.  </p>
<p>Here are a few of the issues i ran into while porting my game <a href="https://appworld.blackberry.com/webstore/content/reviews/38777?lang=en">Heads</a> to your platform:</p>
<ol>
<li>i had to download Many Things, and sign up for Many Accounts. Each Thing and each Account came with 15 pages of legalese with an &#8220;I Agree&#8221; button at the bottom.  <em>I Agree</em> &#8230; that this stinks.
<li>One of the Many Things i had to download was Adobe AIR 2.5.  i followed the link on your site to Adobe AIR <em>2.6</em>, which i downloaded instead.  When i tried to follow your workflow, i was told that only AIR 2.5 would work, so i had to cast about the Internatz to find the 2.5 download, which wasn&#8217;t made immediately and obviously available on the Adobe site.  If i&#8217;m creating something for your platform, everything i do should ideally be immediate and obvious.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/clickHere.jpg" alt="Click here">
</div>
<li>i downloaded VMWare and your VMWare Playbook profile so that i could run a virtual Playbook.  But the emulator stalled at the startup screen for a very long time.  i checked message boards, and found two possible solutions:
<ol>
<li>Leave it overnight.
<li>Alternatingly restart your computer or VMWare multiple times (some reports said &#8220;six or seven&#8221;) until it works.
</ol>
<p>i opted to restart VMWare and my computer multiple times until it worked.  This was very frustrating.  i&#8217;m not the only one who experienced this problem, as evidenced by this web comic by my Twitter pal @<a href="http://twitter.com/#!/iqandreas">IQAndreas</a>:</p>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic1.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic2.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic3.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic0.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<div class="displayed">
<p><a href="http://iqandreas.blogspot.com/2011/03/developing-for-playbook-chapter-3.html"><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/comic4.png" alt="Developing for the Playbook: Chapter 3"></a>
</div>
<li>In order to deploy my game to the virtual Playbook, i had to know its IP.  To get that, i had to swipe the &#8220;development&#8221; option into the &#8220;on&#8221; position and punch in my password.  i had to use the software keyboard to punch in my password, because my computer keyboard didn&#8217;t work.  And worse than that, it took about 4-5 tries swiping the slider and punching in my password before the Developer hammer icon would appear on the home screen &#8230; for whatever reason, that slider kept undoing itself.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2011_05_19/slider.jpg" alt="Playbook Development Slider"></p>
<p>Somebody call Jerry O&#8217;Connell, cuz this slider be busted.
</p></div>
<li>When it came time to bundle my files together in a .bar file, i was dismayed to find that since i don&#8217;t use Flash Builder (but, rather, FlashDevelop), i would need to use the command line to continue.  i hate the command line. HAATE it.  i know that eggheads love it, and that you employ Many Eggheads at RIM, but you have to understand that even though the command line is useful and powerful and 1337 and everything, i absolutely can&#8217;t be arsed with it.  Like, not at all.  So knock it off.
<p>Here is what i had to type into the command line in order to bundle my project into a .bar file:</p>
<blockquote><p>C:\dev\BlackBerryTabletSDK\blackberry-tablet-sdk-0.9.3\bin\blackberry-airpackager -package MyGame.bar -installApp -launchApp MyGame-app.xml blackberry-tablet.xml MyGame.swf blackberry-tablet-icon.png -device 192.168.58.128 -password 123456</p></blockquote>
<p>This is not a fun thing to have to type.  Know what i want to do?  Click a button.  Can you make it so that i just click a button?  Buttons good, typing bad.  It may not be 1337, but it also doesn&#8217;t eat up my entire afternoon.</p>
<li>i am currently rocking three Blackberry accounts: one to develop my game, one to sell my game, and one to talk about my game on your forums.  This is Too Many Accounts.  Know how many there should be? One.  Know why?  Because it&#8217;s easier.  Know what&#8217;s not easy?  You.
<li>When i signed my application, i had to download a file that you sent to me two days after i emailed you and asked you for it.  That&#8217;s Too Many Days.  That&#8217;s because you also took two or three days to approve my vendor account.  Why not do this in one step instead of two?  Clearly, a vendor is always going to need the application signing file.  See how you don&#8217;t make things easy, when you potentially could?
<li>Then i had to use the <em>command line</em> (which, as we&#8217;ve already established, is <em>bad</em>) to create a file that i could send to you so that my computer could sign files.  At least i think that&#8217;s what i was doing. Here&#8217;s what the command looked like:<br />
<blockquote><p>blackberry-signer -csksetup -cskpass DesiredCSKPassword</p></blockquote>
<p>Then i had to use the <em>command line</em> (bad. BAD!) to send you my .csj file <em>to receive permission to sign my other file</em>.  i think.  i&#8217;m not quite sure what was going on, because it was tough to interpret the command, which looked like this:</p>
<blockquote><p>blackberry-signer -register -csjpin PinEnteredWhileRequestingCSJ -cskpass PasswordEnteredWhileGeneratingCSK client-RDK-XXXXXXXXXX.csj</p></blockquote>
<p>Next, i had to create a .p12 certificate using this command:</p>
<blockquote><p>
blackberry-keytool -genkeypair -keystore DesiredCertificateName.p12 -storepass NewPassword -dname &#8220;cn=MyCompanyName&#8221; -alias author</p></blockquote>
<p>Then i had to get you to sign the file using this command:</p>
<blockquote><p>blackberry-signer -verbose -cskpass CSKPassword -keystore CertificateName.p12 -storepass StorePassword BarFileNameForRIMToSign.bar RDK</p></blockquote>
<p>Then i had to sign the file myself using this command:</p>
<blockquote><p>blackberry-signer -keystore CertificateName.p12 -storepass StorePassword RimSignedBarFile.bar author</p></blockquote>
<li>When i finally went to upload my file, in the web form you asked me for an <em>additional</em> icon in some bizarre size (243&#215;717 or something like that).  i went away and produced that icon, and by the time i returned, the web form had timed out.  Know what would be easier?  A checklist!<br />
<blockquote><p><b>YOU WILL NEED:</b></p>
<ul>
<li>A swf
<li>An xml file called whatever.xml &#8211; download it HERE!
<li>A thumbnail icon &#8211; download a template HERE!
<li>A second icon &#8211; download a template HERE!
<li>A brief description of your application &#8211; max X words
<li>A long-form description of your application &#8211; max Y words
</ul>
<p>And HERE&#8217;S an image of how all this stuff looks when it&#8217;s in the Blackberry App World!  We&#8217;ve LABELLED everything for you, so you know where the descriptions and icons appear and how they&#8217;ll look to the user.</p></blockquote>
<p>Really, though &#8211; how long does that kind of thing take to set up?  An afternoon?  Why does this not exist yet?</p>
<li>To add insult to injury, my game was initially rejected because it did not contain the icon.png.  i figured i must have forgotten to include the .png filename when i created the .bar file, so i went through all of those horrible steps <em>again</em>.  For a second time, my game was rejected.  Same reason.
<p>Know what the problem was?  i hadn&#8217;t added this to the xml file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;icon<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>whatever.png<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/icon<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>N&#8217;awesome.
</ol>
<p>i didn&#8217;t enjoy doing this, and i don&#8217;t want to have to do it ever again.  Know what i want?  i want a big blank <em>area</em> where i can drag and drop my file, with a huge shiny juicy button that says &#8220;GO BITCH GO&#8221; which, when i click it, <em>does all the bullshit i just described above</em>.  Please get your eggheads on that.  </p>
<p>In addition to all of the brilliant software and hardware engineers you employ, you simply need to hire more <em>people</em> to evaluate this process.  An egghead will tell you that using the command line is cool and awesome and that everyone loves doing it.  A <em>person</em> will tell you the actual truth: using the command line blows, and you need to put that shit on a button.  </p>
<p>Please let me know if and when you plan to put that shit on a button, and i&#8217;ll gladly continue developing for your device, because it&#8217;s pretty cool.</p>
<p>Sincerely,</p>
<p>Ryan Henson Creighton</p>
<p>President, Untold Entertainment Inc.</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%2F05%2F19%2Fdear-rim-blackberry-playbook-people-please-put-that-shit-on-a-button%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2011%2F05%2F19%2Fdear-rim-blackberry-playbook-people-please-put-that-shit-on-a-button%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/05/19/dear-rim-blackberry-playbook-people-please-put-that-shit-on-a-button/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3722&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2011/05/19/dear-rim-blackberry-playbook-people-please-put-that-shit-on-a-button/feed/</wfw:commentRss>
		<slash:comments>25</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 Loops</title>
		<link>http://www.untoldentertainment.com/blog/2010/11/24/understanding-loops/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/11/24/understanding-loops/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 16:08:29 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=3205</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. Loops are one of them. It&#8217;s a great idea to back up and read Understanding Arrays if you haven&#8217;t already. The reason is that Loops and Arrays go hand-in-hand. Loops are the interface [...]]]></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.  Loops are one of them.</p>
<p>It&#8217;s a great idea to back up and read <a href="http://www.untoldentertainment.com/blog/2010/11/22/understanding-arrays/">Understanding Arrays</a> if you haven&#8217;t already.  The reason is that Loops and Arrays go hand-in-hand.  Loops are the interface we use to do interesting things with Arrays.  You might think of Arrays as an mp3 player, and Loops as headphones &#8230; if you try to use either of these things separately, they may not seem too terribly useful.  But put them together, and boy &#8230; <em>beautiful music</em> happens.  Unless you&#8217;re listening to Nickelback.</p>
<p>That&#8217;s why the next article in this series is called Understanding Loops <em>with</em> Arrays.  For now, just muscle through this article to understand what loops are all about &#8211; i&#8217;ll try to make it somewhat interesting.  The beautiful music starts playing in the next article.</p>
<h2>Pac Man</h2>
<p>Your code is usually chewed through, line by line, by an interpreter.  The interpreter is like Pac Man.  It eats the first line, and carries out its instructions. Then it eats the second line and carries out those instructions and so on, until the end of your program.  Earlier programming languages had each statement numbered, and the Pac Man code interpreter would chew through each one.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/basic.jpg" alt="Commodore Basic"></p>
<p>Goodness knows what &#8220;poke&#8221; meant in the cocaine-fueled days of the 1980&#8242;s.
</p></div>
<p>Object-oriented programming (OOP) does away with those line numbers and sends Pac Man flying all over the maze a little bit, jumping into and out of chunks of code.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/map.jpg" alt="Pac Man Map"></p>
<p>If procedural code with line numbers is like traveling by train on a straight track, OOP is like taking multiple plane flights through TIME PORTALS.
</p></div>
<p>But imagine for a second that your Pac Man code interpreter has to eat straight down the page to the bottom, to the last line of code.  If you want to do things more than once, you have to write the command more than once:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// This is pseudo-code, which means you can't really copy/paste it and expect it to work,</span>
<span style="color: #808080; font-style: italic;">// but it's very readable.</span>
eat a power pellet;
eat a power pellet;
eat a power pellet;</pre></div></div>

<p>Fine.  Pac Man eats a power pellet three times.</p>
<h2>Hungry Hungry Pac Man</h2>
<p>But what happens if your code interpreter needs to do something <em>more</em> than three times?  Say, fifty times.  Or a <em>thousand</em>?  Now you&#8217;re going to have that same line repeated a thousand times down the screen.  Yuck.</p>
<p>Or even worse, let&#8217;s say there are a number of instructions we want to carry out, and we want that set of instructions repeated:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// This is pseudo-code, which means you can't really copy/paste it and expect it to work,</span>
<span style="color: #808080; font-style: italic;">// but it's very readable.</span>
eat a power pellet;
give Ms. <span style="color: #006600;">Pac</span> Man a smooch;
punch a ghost <span style="color: #b1b100;">in</span> the face;
&nbsp;
eat a power pellet;
give Ms. <span style="color: #006600;">Pac</span> Man a smooch;
punch a ghost <span style="color: #b1b100;">in</span> the face;
&nbsp;
eat a power pellet;
give Ms. <span style="color: #006600;">Pac</span> Man a smooch;
punch a ghost <span style="color: #b1b100;">in</span> the face;</pre></div></div>

<p>Now you&#8217;ve got a real problem.  Your little set of instructions, repeated a thousand times, adds three thousand lines to the code &#8211; four thousand with line breaks.  This code becomes ridiculous to manage and maintain, it looks ugly, and it takes up way too much room.</p>
<p>Enter that frustrated mom from every infomercial ever: &#8220;There&#8217;s <em>got</em> to be a better way!&#8221;</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/clingFilm.jpg" alt="Plastic wrap is hard"></p>
<p>Plastic wrap is HARD!
</p></div>
<p>Loops are the better way.  You build them to make the interpreter repeat an instruction, or set of instructions, a bunch of times.  When the loop is done, the interpreter continues on its merry way down the page, chewing through each subsequent instruction you&#8217;ve written for it.</p>
<h2>The Anatomy of a Loop</h2>
<p>Here&#8217;s what a loop (often called a &#8220;for&#8221; loop) looks like in Actionscript 3 and other ECMA-based languages:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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><span style="color: #cc66cc;">1000</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Let&#8217;s break it down piece by piece.</p>
<p>You&#8217;ll notice there are two sections: the declaration line, and those curly braces.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/loop1.jpg" alt="Two main loop sections"></p>
</div>
<p>The instructions you want to repeat go in between those curly braces, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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><span style="color: #cc66cc;">1000</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    eat a power pellet;
    give Ms. <span style="color: #006600;">Pac</span> Man a smooch;
    punch a ghost <span style="color: #b1b100;">in</span> the face;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>That declaration line looks pretty hairy, though.  Let&#8217;s break it down.</p>
<p>Notice that the whole thing is just the word &#8220;for&#8221; followed by open and closed round brackets.  If we completely gut the loop, it looks like this:</p>

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

<p>Doesn&#8217;t look so scary any more, yeah?</p>
<p>The stuff between the round brackets is split up into three sections: </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/loop2.jpg" alt="Loop header breakdown"></p>
</div>
<ol>
<li>Start here.
<li>Keep looping as long as this is true.
<li>Do this after every run through the loop.
</ol>
<p>The first and second sections are separated by a semi-colon, which is the dot-above-the-comma symbol that we usually use to end our statements.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/punctuation.jpg" alt="Names of common punctuation marks"></p>
</div>
<h2>1. Start Here</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/loopsStartHere.jpg" alt="Loops start here"></p>
</div>
<p>A &#8220;run&#8221; through a loop is called an <b>iteration</b>.  In order to keep count of how many times we&#8217;ve looped, we use a variable called an <b>iterator</b>.  Because the word &#8220;iterator&#8221; starts with the letter &#8220;i&#8221;, it&#8217;s common practice to use &#8220;i&#8221; as the iterator variable name.</p>
<p>So you need to declare and define that variable first, to give your loop a starting point.  Depending on how picky your language is, you may have to type the variable (give it a datatype).  Here are two examples of loops written in a permissive vs. strict language:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Permissive, like Unity javascript:</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><span style="color: #cc66cc;">1000</span>; i++<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;">// Strict, like Actionscript 3:</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><span style="color: #cc66cc;">1000</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So the only real difference is that in the stricter language, we had to declare the iterator variable using the &#8220;var&#8217; keyword, and we had to expressly say that it was an integer datatype (whole number) by adding &#8220;:int&#8221; to the end of the variable name.</p>
<p>Note that you can also <b>declare</b> the iterator outside the loop, and <b>define</b> it inside the loop header:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</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><span style="color: #cc66cc;">1000</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>i like doing this, because it makes my loop header look cleaner. If i have more than one loop in my program, the compiler throws an error if i use the <b>var</b> keyword to declare the iterator more than once.  That means that if i quickly copy/paste a loop where the iterator is delcared inside the header, the compiler yells at me:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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><span style="color: #cc66cc;">1000</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<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><span style="color: #cc66cc;">20</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// ERROR!  You've already declared a variable called i, dummy.</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Meanwhile, this does not throw an error:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</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><span style="color: #cc66cc;">1000</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<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><span style="color: #cc66cc;">20</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// No error. We're just resetting i to zero.</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>What this section does is set the value of a variable called i to zero. <b>i is our counter that will keep track of how many times we&#8217;ve run through the loop.</b></p>
<p>Now, onto the second section of that confusing header.</p>
<h2>2. Keep looping as long as this is true.</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/loopsKeepLooping.jpg" alt="Loops keep looping"></p>
</div>
<p>Every time the Pac Man interpreter starts to chew through this loop, he&#8217;s going to check the second section.  This section tells him &#8220;you&#8217;re okay to chew through this loop, as long as this statement <b>resolves</b> to true.&#8221;</p>
<p>What do i mean by &#8220;resolves&#8221;?  Imagine you take a bunch of ingredients and put them in a pan, and then bake what&#8217;s in the pan.  You&#8217;ve <em>resolved</em> the ingredients into a cake.  In the same way, we&#8217;re going to <em>resolve</em> the ingredients in this statement until they result in either <b>true</b> or <b>false</b>.</p>
<p>Here&#8217;s an easy one:</p>
<p>3<5</p>
<p>That statement resolves to <b>true</b>, because 3 is less than 5.  But this statement:</p>
<p>3>5</p>
<p>resolves to <b>false</b>, because 3 is not greater than 5.  These are the same rules we follow when we&#8217;re setting up conditional if statements.  If you need a review, check out Understanding Operators,  and Understanding Conditionals.  (note: i haven&#8217;t written these articles yet, but they&#8217;re on the way!)</p>
<p>Things get a touch more complicated when variables are involved.  Take this statement:</p>
<p>i<5</p>
<p>That's not as straightforward. i is the name of a variable (a bucket) that holds a value.  Whether this statement resolves to true or false really depends on the value of i.  If the value of i is 3, the statement resolves to true.  But if the value of i is 867, the statement resolves to false.</p>
<p>You can put all kinds of complicated nonsense into the second section of your loop header.  Regard:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #cc66cc;">0</span>; <span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">1000</span> <span style="color: #66cc66;">&amp;&amp;</span> theSunIsShining<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> dogsLoveBacon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In English, that second section now reads &#8220;run through the loop as long as the value of i is less than one thousand and the value of theSunIsShining is true, OR if the function dogsLoveBacon returns true.</p>
<p>Gross!  Let&#8217;s keep it simple.  When i was first learning programming, i&#8217;d sometimes write very complicated loop limiters, and i&#8217;d always regret it.  It&#8217;s rare that you see anything beyond a simple <b>i<something</b> in this second section.</p>
<h2>3. Do this after every run through the loop.</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/loopsDoThis.jpg" alt="Loops do this after every iteration"></p>
</div>
<p>The third and final section of the loop header tells the interpreter what to do after it chews through the last statement inside the curly braces of the loop.  The interpreter gets to the bottom, does whatever is in the third section, and then checks the second section to see if it should run through the loop again.  (If that&#8217;s confuding, don&#8217;t worry &#8211; we&#8217;ll take a closer look at that process in a moment.)</p>
<p>In most cases, this third section will say i++.  That&#8217;s the <b>increment</b> operator, which is code shorthand for saying &#8220;add 1 to the value of i&#8221;.  So every time through the loop, the counter goes up by one.</p>
<p>Again, you can make this bit crazy complicated if you like.  You could say something like i+=Math.sqrt(beesHaveKnees()), which means &#8220;add the square root of the value returned by the beesHaveKnees function to the value of i&#8221;.  You can even comma-separate multiple statements by saying i++, j++, which means &#8220;increment the value of i, and also increment the value of j&#8221;.  You <em>can</em> do this, but in day-to-day vanilla programming, and especially just starting out, you&#8217;ll want to keep this stuff very, very simple.</p>
<h2>Follow the Interpreter Through the Loop</h2>
<p>So let&#8217;s trace the path of the Pac Man code interpreter through our loop.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process1.jpg" alt="Do the thing in the first section"></p>
</div>
<p>1. Do what&#8217;s in the first section first.  In this case, we set the value of the iterator variable <b>i</b> to zero.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process2.jpg" alt="Check the second section"></p>
</div>
<p>2. Check to see if the second section resolves to true.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process3.jpg" alt="Chew through the statement block"></p>
</div>
<p>3. If it does resolve to true, chew through all the statements in the statement block (the stuff between those curly braces). A power pellet gets eaten, Ms. Pac Man gets some lovin&#8217;, and a ghost gets his just desserts.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process4.jpg" alt="Execute the third section"></p>
</div>
<p>4. When you finish chewing through the last statement in order, do what&#8217;s in the third section.  In this example, we increase the value of i by one. The value of i (0) plus 1 equals 1, so the second time through the loop, i equals 1.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process5.jpg" alt="Check the second section again"></p>
</div>
<p>5. Check the second section to see if it resolves to true.  In our example, the value of i (1) is indeed less than 1000, so our interpreter should chew through all the statements in the loop body again.  He eats another power pellet, plants another kiss, and dispatches another ghost.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process6.jpg" alt="Loop 1000 times"></p>
</div>
<p>6.  Let&#8217;s pretend we&#8217;ve gone through the loop 1000 times.  Pac Man&#8217;s gluttony continues unabated, Ms. Pac Man struggles to cope with her husband&#8217;s relentless sexual advances, and his plan for spectral genocide is finally complete.   At the bottom of the loop, the value of i is 999.  (Note: it&#8217;s not 1000, because we started counting at zero)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process7.jpg" alt="Execute the third section"></p>
</div>
<p>7. The interpreter jumps to the top of the loop header, and does what&#8217;s in the third section. Adding 1 to the value of i puts it at 1000.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process8.jpg" alt="Check the second section"></p>
</div>
<p>8. The interpreter looks at the second section of the loop header to determine whether it should go through the loop again.  This time, i <em>is not</em> less than 1000. (1000 is not less than 1000.)  So because this statement resolves to <b>false</b>, the interpreter exits the loop, and continues after the closed curly brace at the bottom of the whole structure.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_11_23/process9.jpg" alt="Exit the loop"></p>
</div>
<p>9. Our Pac Man-like code interpreter exits the loop and smugly strikes a pose.</p>
<h2>A Real-World Example</h2>
<p>None of the code in this article will actually run properly, so here&#8217;s a chunk that will:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</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><span style="color: #cc66cc;">5</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><span style="color: #ff0000;">&quot;The value of i is &quot;</span> + i<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>(If you want it to work in Unity javascript, change &#8220;trace&#8221; to &#8220;print&#8221; or &#8220;Debug.Log&#8221;, and you should see the results of the loop pop up in the Console window.)</p>
<p>As i mentioned off the top, loops on their own are very useful for keeping our programs down to a manageable size, but they really come alive when we combine them with Arrays.  In the next article, <a href="http://www.untoldentertainment.com/blog/2010/12/01/understanding-loops-with-arrays/">Understanding Loops with Arrays</a>, we&#8217;ll see how these two code constructs, when married, are a match made in programming heaven.  And if we&#8217;re lucky, we&#8217;ll even see Pac Man tried as a war criminal.  Join me, won&#8217;t you?</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%2F11%2F24%2Funderstanding-loops%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F11%2F24%2Funderstanding-loops%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/24/understanding-loops/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=3205&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/11/24/understanding-loops/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>Cooking With Flash: Basic Flash Project Setup</title>
		<link>http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/#comments</comments>
		<pubDate>Thu, 06 May 2010 18:36:44 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Cooking With Flash]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2504</guid>
		<description><![CDATA[Prerequisites: none. Begin So you want to start creating games with Adobe Flash? Here&#8217;s the very first step. You&#8217;ll need two pieces of software: Adobe Flash, and a program to in which to write your code. (Code is a list of instructions you give to the computer to make it bend to your steely will.) [...]]]></description>
			<content:encoded><![CDATA[<p><b>Prerequisites:</b></p>
<ul>
<li>none.
</ul>
<h2>Begin</h2>
<p>So you want to start creating games with Adobe Flash?  Here&#8217;s the very first step.</p>
<p>You&#8217;ll need two pieces of software: Adobe Flash, and a program to in which to write your code.  (Code is a list of instructions you give to the computer to make it bend to your steely will.)  You can write your code inside the Flash program, but Flash is always playing catch-up with the great features available in other programs that make coding easier.  And i like easy.</p>
<h2>1. Download the Software</h2>
<p>You can download a number of programs to help you code.  The one we recommend (and use here at Untold Entertainment to create all our games) is called FlashDevelop.  It&#8217;s free, but if you like it, consider tossing a few clams to the creators.</p>
<ul>
<li><a href="http://www.adobe.com/products/flash/">Download a free 30-day trial of Adobe Flash</a>
<li><a href="http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page">Download your free copy of FlashDevelop</a> (PC-only)
</ul>
<p>note: other popular programs that professionals use to create their code include <a href="http://www.fdt.powerflasher.com/developer-tools/fdt-3/">FDT</a>, <a href="http://www.adobe.com/products/flashbuilder/">Flash Builder</a> (formerly known as Flex Builder), and even a fancy text editor like <a href="http://notepad-plus.sourceforge.net/uk/download.php">Notepad++</a>.  (i&#8217;m told that if you use a Mac, you&#8217;re iShitOutOfLuck on this front, but perhaps the commenters can recommend something?)</p>
<p>Once you&#8217;re finished downloading the two pieces of software you need, install them into the default directories, and we&#8217;ll get started setting up a project.</p>
<h2>What <em>is</em> Flash?</h2>
<p>The term &#8220;Flash&#8221; is confusing.  You may have heard that some online video &#8220;<em>is</em> Flash&#8221;, that some games you play in the browser &#8220;<em>are</em> Flash&#8221;, that some websites &#8220;<em>use</em> Flash&#8221;, and more recently that Flash <em>runs</em> or <em>doesn&#8217;t run</em> on this device or that device.  The word &#8220;Flash&#8221; is used interchangeably for both the authoring tool, the content that you create with it, and the plugin that displays that content to the end user.</p>
<p>The Adobe Flash software is also called an authoring <em>tool</em>, like a paintbrush or a table saw.  With it, you produce Flash content (games, video players, websites, etc), which a person usually views in a web browser using a plugin called the Flash <em>Player</em>.  Most often, when someone says you need to &#8220;download Flash&#8221;, he means you need to download the Flash Player <em>plugin</em> so that you can view Flash <em>content</em> that was created with the Flash <em>tool</em>.   What you&#8217;re downloading in the link above is the tool, which is sometimes called the Flash IDE (Integrated Development Environment).  i prefer &#8220;tool&#8221;, because it&#8217;s only one syllable, and &#8220;eye dee yee&#8221; is not kind to the ear.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/manyUsesOfFlash.jpg" alt="The Many Uses of the Term 'Flash'"></p>
<p>The term &#8220;Flash&#8221; has multiple uses.
</p></div>
<h2>2. Create and Save a New Actionscript 3 .fla File</h2>
<p>When you open the Flash authoring tool for the first time, you&#8217;ll likely see a splash screen.  To create a new file, etiher click &#8220;Create New Flash File (Actionscript 3.0)&#8221;, or go to File > New and choose Flash File (Actionscript 3.0) from the list, and click OK.</p>
<p>Your Flash file opens up and is displayed in all its glory &#8211; tons of interesting buttons and panels all over the screen.  Don&#8217;t be intimidated. We&#8217;ll explore them in future lessons.</p>
<p>Click File>Save As in the menus.  Flash files have the extension .fla.  Change the name of your Flash working file to myFirstGame.fla (or whatever you like), and save it to a folder somewhere on your computer.  Remember where you saved it!  i recommend against saving it to the desktop &#8211; if you want it there, first create a folder on the desktop called MyFirstGame, and save your .fla file in there.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashIDE.jpg" alt="Adobe Flash IDE"></p>
<p>Look at all this great STUFF to mess around with!  (note: Flash supports all kinds of different layouts &#8211; you may see something different on your screen depending on the version and layout you&#8217;re using)
</p></div>
<h2>3. Create a New FlashDevelop Project</h2>
<ol>
<li>Open FlashDevelop.
<li>In the menus, go to Project > New Project.
<li>Choose a name for your project in the Name field. (You can call it MyFirstGame.  Seems logical.)
<li>Use the Browse button to point to the MyFirstGame folder you created, where saved your .fla file in the last step.
<li>Click OK.
<li>If you get a message saying &#8220;The directory is not empty &#8230;&#8221;, click OK to confirm.
<li>If FlashDevelop asks you the Author name, type your own name or a suitable pseudonym, like &#8220;Cap&#8217;n CrackyPants&#8221;.  All of the code you write will be signed with this name.
<li>Click OK.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/saveFlashDevelopProject.jpg" alt="Creating a new project in FlashDevelop"></p>
<p>Creating a new project in FlashDevelop
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashDevelopAuthor.jpg" alt="FlashDevelop author prompt"></p>
<p>Señor Taco-Taker is like the Mexican Hamburglar
</p></div>
<p>When you&#8217;re finished, check out the Project Panel, at the right side of the screen. (If you can&#8217;t see it, click View > Project Manager in the menus.)  You&#8217;ll see your Project name, MyFirstGame.  Then you&#8217;ll see the .fla file you created in Flash, called myFirstGame.fla.  All of the code files you write to make your game will be listed here, along with any other files you place in the MyFirstGame folder.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashDevelopProjectPanel.jpg" alt="FlashDevelop project panel"></p>
<p>FlashDevelop Project panel
</p></div>
<p>Behind the scenes, FlashDevelop creates an .as3proj file in the folder, which is a metadata file that keeps track of what goes into the folder.  Once you create your Project file, you never have to save it like you would most other files &#8211; you can just open and close different Project files, and they&#8217;ll list the stuff that&#8217;s in the folder with them.</p>
<h2>4. Link FlashDevelop to Flash</h2>
<p>The last thing to do is to make FlashDevelop aware of the Flash authoring tool so that the two can play nicely together.  </p>
<ol>
<li>In the menus, click Tools>Program Settings or press F10.
<li>Click ASCompletion in the sidebar list.
<li>Click in the field labeled &#8220;Path to Flash IDE&#8221;, and navigate to wherever you installed the Flash tool on your computer.  On my Windows system, it&#8217;s here:
<p>C:\Program Files\Adobe\Adobe Flash CS4</p>
<li>Click the Close button.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_05_06/flashDevelopSettings.jpg" alt="FlashDevelop Settings"></p>
<p>FlashDevelop Settings
</p></div>
<h2>Finish</h2>
<p>That&#8217;s it &#8211; you&#8217;re done!  Now you&#8217;re ready to create Flash content using the one-two punch combo of Flash and FlashDevelop.</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%2F05%2F06%2Fcooking-with-flash-basic-flash-project-setup%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F05%2F06%2Fcooking-with-flash-basic-flash-project-setup%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/05/06/cooking-with-flash-basic-flash-project-setup/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2504&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/05/06/cooking-with-flash-basic-flash-project-setup/feed/</wfw:commentRss>
		<slash:comments>17</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>Tutorial: Clickify Your Twitter Feed in Flash</title>
		<link>http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 13:44:03 +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[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2230</guid>
		<description><![CDATA[&#8220;Fwitter&#8221;. Deal with it. Our Pull Twitter Updates Into Flash tutorials (Part 1 and Part 2) continue to be popular! A few folks were asking how i had taken my Twitter feed and rigged it so that people&#8217;s Twitter names and links were clickable. i promised to write tutorials on demand, so here we go. [...]]]></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>&#8220;Fwitter&#8221;.  Deal with it.
</p></div>
<p>Our <b>Pull Twitter Updates Into Flash</b> tutorials (<a href="http://www.untoldentertainment.com/blog/2009/04/02/tutorial-pull-twitter-updates-into-flash/">Part 1</a> and <a href="http://www.untoldentertainment.com/blog/2009/05/14/tutorial-twitter-updates-in-flash-part-2/">Part 2</a>) continue to be popular! A few folks were asking how i had taken my Twitter feed and rigged it so that people&#8217;s Twitter names and links were clickable.  i promised to write tutorials on demand, so here we go.</p>
<h2>Clickify Your Twitter Feed in Flash</h2>
<p>Here&#8217;s the process in pseudo-code:</p>
<ol>
<li>Take the chunk of text and split it up into individual words
<li>With each word, figure out if it starts with &#8220;@&#8221; or &#8220;http:&#8221;
<li>If it does, wrap it in html tags
<li>Re-assemble the string
<li>Set the re-constituted string as your dynamic text field&#8217;s htmlText property
</ol>
<p>So, let&#8217;s start with a simple chunk of Tweet text:</p>
<p>&#8220;Hey everyone &#8211; @untoldent&#8217;s blog is INCREDIBLE!  http://bit.ly/cXX06r&#8221;</p>
<blockquote><p>
Note: this is just a tweet i pulled at random from the Twittersphere.  It could have been anything.  But my blog is usually a trending topic at any given time of the day.</p></blockquote>
<p>We&#8217;ve got two things we want to make clickable: <a href="http://www.twitter.com/untoldent">@untoldent</a>, which should link to untoldent&#8217;s Twitter user page, and <a href="http://bit.ly/cXX06r">http://bit.ly/cXX06r</a>, which should take us to that webpage.</p>
<h2>Splitting the String</h2>
<p>The String class has a built-in method called split.  It takes an argument that lets us carve a string up, and the resulting fragments are put into an array.</p>
<p>So, for example, if we have a comma-delimited string:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> fraggles:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Gobo,Mokey,Wimbley,Boober,Red&quot;</span>;</pre></div></div>

<p>we can split it using the comma as a separator.  The resulting fragments are returned as an array.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> fraggles:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Gobo,Mokey,Wimbley,Boober,Red&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> aFraggles:<span style="color: #0066CC;">Array</span> = fraggles.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// use the comma to split up the string</span>
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>aFraggles.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// 5</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>aFraggles<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Wimbley</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>aFraggles<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Red</span></pre></div></div>

<p>So in the case of our Twitter string, we want to split it up into individual words. The thing that separates one word from another is the space character. So let&#8217;s use space as an argument to split up that string:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> aWords:<span style="color: #0066CC;">Array</span> = twitterString.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// use the space character to split up the string</span></pre></div></div>

<blockquote><p>Note: the split method is slow, so use it sparingly.  There&#8217;s not a lot going on in our Twitter reader, so we can get away with it.</p></blockquote>
<p>Now let&#8217;s loop through each element in the aWords array &#8211; each word in the Twitter post &#8211; and determine if it&#8217;s a Twitter username (which starts with &#8220;@&#8221;), or a URL (which starts with &#8220;http:&#8221;).  We&#8217;ll create an empty string to hold our final output, and run each word through a separate method.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> finalString:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>; <span style="color: #808080; font-style: italic;">// Create a new string to hold our final output</span>
<span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">int</span> = aWords.<span style="color: #0066CC;">length</span>; <span style="color: #808080; font-style: italic;">// store the length of the aWords array to speed up our loop</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Loop through all of the words in our tweet:</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> word:<span style="color: #0066CC;">String</span> = aWords<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// Store an individual word in a variable</span>
    finalString += <span style="color: #ff0000;">&quot; &quot;</span> + checkWord<span style="color: #66cc66;">&#40;</span>word<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Run the word through a checkWord method, and tack the </span>
    <span style="color: #808080; font-style: italic;">// return value onto the end of our finalString (along with a space character, to space the words out)</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<blockquote><p>Note: We&#8217;re storing the length of aWords in a variable, instead of saying i<aWords.length.  This is because the Flash Player has to do more work to look up the length property of aWords every single time we loop.  You can speed up your loops a tiny bit by using this trick. </p></blockquote>
<p>Obviously, what&#8217;s missing is the all-important checkWord() method.  Let&#8217;s build that next.</p>
<h2>The checkWord() Method</h2>
<p>Our checkWord() method is going to do this, in pseudo-code:</p>
<ol>
<li>Accept a word
<li>If the word starts with &#8220;@&#8221; or &#8220;http:&#8221;, wrap it in href tags and return the string
<li>Otherwise, just return exactly what was passed in
</ol>
<p>To check if a string contains a certain other string, use the String.indexOf() method.  indexOf() will return the place in the string where the other string first appears.</p>
<p>For example, let&#8217;s find the indexOf the capital letter &#8220;W&#8221; in the string &#8220;NSFW&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;NSFW&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;W&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 3</span></pre></div></div>

<p>The answer is 3, because &#8220;W&#8221; is at index 3 in the string.  (Remember that strings and arrays are 0-based &#8211; the first element is at index 0, the second element is at index 1, and so on.)</p>
<p>If the thing you&#8217;re looking for can&#8217;t be found anywhere in the string, the indexOf() method will return an index of -1:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;NSFW&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;pictures of adorable kittens&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// -1</span></pre></div></div>

<h2>Substring</h2>
<p>The last thing you need to know how to do is to grab just a piece of a string.  This is because the twitter name comes through with an @ symbol at the front, for example @untoldent.  But we need to link to http://www.twitter.com/untoldent.  Notice there&#8217;s no &#8220;@&#8221; in that URL.</p>
<p>We need to extract just the username from the string, without the &#8220;@&#8221;.  We can use the String.substr (substring) method to do that.</p>
<p>The way String.substr() works is that you pass it a start point and an end point, and it rips that section out of the string and returns it.</p>
<p>So, check it out:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;manboobs&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boobs</span></pre></div></div>

<p>The letter at index 3 of &#8220;manboobs&#8221; is &#8220;b&#8221;.  (Remember &#8211; we start counting at zero.)  We rip the section out up to index 7, which is that last letter, &#8220;s&#8221;.  So what&#8217;s returned are the letters from index 3 to index 7 &#8211; &#8220;boobs&#8221;.</p>
<p>If you want to rip out a chunk of your string from a certain index point to the last letter in the string, you can use the String.length property.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;manboobs&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,str.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boobs</span></pre></div></div>

<p>If you&#8217;re on high alert and paying really close attention, you&#8217;ll notice that str.length takes us one index point past the end of our string.  (&#8220;manboobs&#8221; is 8 characters long, and there IS no letter at index 8.)  Flash just ignores us if we try to grab more of the string than actually exists.  So technically, this is also valid:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;manboobs&quot;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>str.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">5000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boobs</span></pre></div></div>

<p>Armed with this knowledge, we can build our checkWord() method.</p>
<h2>Let&#8217;s Do This Thing</h2>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkWord<span style="color: #66cc66;">&#40;</span>word:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>word.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http:&quot;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
         <span style="color: #808080; font-style: italic;">// This word starts with &quot;http:&quot; at index 0 (the beginning of the word)</span>
&nbsp;
         <span style="color: #808080; font-style: italic;">// Wrap it in html href (anchor) tags:</span>
         <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;a href='&quot;</span> + word + <span style="color: #ff0000;">&quot;'&gt;&quot;</span> + word + <span style="color: #ff0000;">&quot;&lt;/a&gt;&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>word.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;@&quot;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #808080; font-style: italic;">// This word starts with &quot;@&quot; at index 0 (the beginning of the word)</span>
&nbsp;
         <span style="color: #000000; font-weight: bold;">var</span> justTheName:<span style="color: #0066CC;">String</span> = word.<span style="color: #0066CC;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, word.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Remove the &quot;@&quot; from the username</span>
         <span style="color: #808080; font-style: italic;">// We do this because we need to link to http://www.twitter.com/untoldent (for example),</span>
         <span style="color: #808080; font-style: italic;">// not http://www.twitter.com/@untoldent</span>
&nbsp;
         <span style="color: #808080; font-style: italic;">// Wrap it in html href (anchor) tags:</span>
         <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;a href='http://www.twitter.com/&quot;</span> + justTheName + <span style="color: #ff0000;">&quot;'&gt;&quot;</span> + word + <span style="color: #ff0000;">&quot;&lt;/a&gt;&quot;</span>;
   <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// There's nothing special about this word.  Return it as-is.</span>
        <span style="color: #b1b100;">return</span> word;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<blockquote><p>Note: Did you notice the use of single quotation marks and double quotation marks?  You need to wrap you url in quotes, but since we&#8217;re already using double-quotation marks around our string, we need to use single-quotation marks to wrap the url, or they&#8217;ll interrupt our string definition and screw things up.</p></blockquote>
<h2>The Money Shot</h2>
<p>So far, we&#8217;ve turned this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Hey everyone - @untoldent's blog is INCREDIBLE!  http://bit.ly/cXX06r&quot;</span></pre></div></div>

<p>into this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #ff0000;">&quot;Hey everyone - &lt;a href='http://www.twitter.com/untoldent'&gt;@untoldent's&lt;/a&gt; blog is INCREDIBLE!  &lt;a href=''&gt;http://bit.ly/cXX06r&lt;/a&gt;&quot;</span></pre></div></div>

<p>The very last step is to use the htmlText property of your dynamic text box and stuff it with that fancy new string.  Assuming you have a dynamic text box called twitterTxt, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">twitterTxt.<span style="color: #0066CC;">htmlText</span> = finalString;</pre></div></div>

<p>You&#8217;ll now have clickable links inside your text field.</p>
<h2>Extra Credit</h2>
<p>If you&#8217;re super-astute, you&#8217;ve noticed that this code won&#8217;t actually work!  That&#8217;s because the Twitter string says &#8220;@untoldent&#8217;s&#8221;, not &#8220;@untoldent&#8221;.  The apostrophe-s is going to screw your code up, because you&#8217;ll be linking to http://www.twitter.com/untoldent&#8217;s, which is not a valid URL.</p>
<p>Using what you&#8217;ve learned in this tutorial, can you augment your checkWord() method to detect a &#8220;&#8216;s&#8221; at the end of a Twitter user name and rip it out?</p>
<h2>Further Reading</h2>
<p>i hope this tutorial was informative and helpful!  For more Actionscript and Flash tutorials, thrill to our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript  911</a> book.</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%2F02%2F24%2Ftutorial-clickify-your-twitter-feed-in-flash%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F02%2F24%2Ftutorial-clickify-your-twitter-feed-in-flash%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/02/24/tutorial-clickify-your-twitter-feed-in-flash/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2230&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/02/24/tutorial-clickify-your-twitter-feed-in-flash/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>AS3 Pitfalls &#8211; SOUND_COMPLETE Event is Not Firing</title>
		<link>http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 00:25:22 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Pitfalls]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1956</guid>
		<description><![CDATA[PROBLEM: The SOUND_COMPLETE event is not triggered when your sound finishes playing. SOLUTION: There are a few different reasons why this problem may occur, but here&#8217;s how it played out in my project: When you&#8217;re listening for a SOUND_COMPLETE event, you work with two Classes: Sound and SoundChannel. The event listener goes on the SoundChannel [...]]]></description>
			<content:encoded><![CDATA[<p><b>PROBLEM:</b></p>
<p>The SOUND_COMPLETE event is not triggered when your sound finishes playing.</p>
<p><b>SOLUTION:</b></p>
<p>There are a few different reasons why this problem may occur, but here&#8217;s how it played out in my project:</p>
<p>When you&#8217;re listening for a SOUND_COMPLETE event, you work with two Classes: Sound and SoundChannel.  The event listener goes on the SoundChannel instance:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">sound</span>:<span style="color: #0066CC;">Sound</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;allYouNeedIsLove.mp3&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> soundChannel:SoundChannel = <span style="color: #0066CC;">sound</span>.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
soundChannel.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SOUND_COMPLETE</span>, soundIsFinished<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>In my project, i rigged up a &#8220;Pause&#8221; button.  When the player clicks &#8220;Pause&#8221;, i stop the sound and take note of the Sound&#8217;s position.  When the player clicks &#8220;Pause&#8221; again, i unpause the game and tell the Sound to start playing where we left off.</p>
<p>But whenever you paused and unpaused the game, the SOUND_COMPLETE Event wouldn&#8217;t fire any more.</p>
<p>That&#8217;s because whenever you tell a Sound object to play, <em>it returns a new SoundChannel instance</em>.  The old SoundChannel instance &#8211; the one with the event listener on it &#8211; is no longer relevant, so the event doesn&#8217;t fire.  So to get this to work, when the player unpauses the game, you have to add the SOUND_COMPLETE event listener to the <em>new</em> SoundChannel instance that&#8217;s returned when you call Sound.play();</p>
<p><b>THANKS FOR BAILING ME OUT!</b></p>
<p>BIG thanks to user WearDark on the Kirupa forums for clearing this up, and for saving me from hours and hours of bug testing trying to figure out what was going on!</p>
<p>http://www.kirupa.com/forum/archive/index.php/t-263824.html</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%2F10%2F14%2Fas3-pitfalls-sound_complete-event-is-not-firing%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F10%2F14%2Fas3-pitfalls-sound_complete-event-is-not-firing%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/10/14/as3-pitfalls-sound_complete-event-is-not-firing/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1956&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/10/14/as3-pitfalls-sound_complete-event-is-not-firing/feed/</wfw:commentRss>
		<slash:comments>10</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>Saved Game (a Kindisoft secureSWF Review)</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/11/saved-game-a-kindisoft-secureswf-review/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/11/saved-game-a-kindisoft-secureswf-review/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 13:59:14 +0000</pubDate>
		<dc:creator>jeff</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Pitfalls]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1667</guid>
		<description><![CDATA[Full Disclosure: This is a paid review. We are receiving a free license of Kindisoft secureSWF in exchange for evaluating the product and posting our opinion. This has not necessarily effected a positive or negative review; while a post can be bought, our favourable opinion comes at a higher price. (That price, incidentally, is one [...]]]></description>
			<content:encoded><![CDATA[<p><b>Full Disclosure:</b> This is a paid review.  We are receiving a free license of <a href="http://www.kindisoft.com/">Kindisoft secureSWF</a> in exchange for evaluating the product and posting our opinion.  This has not necessarily effected a positive or negative review; while a post can be bought, our favourable opinion comes at a higher price.  (That price, incidentally, is one large basket of Peek Frean&#8217;s chocolate Bourbon Cream cookies, a variety of points cards for leading video game consoles, a 1-year subscription to <a href="http://www.gamesmagazine-online.com/">Games Magazine</a>,  and a commercial-grade Sno Kone machine.) </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/bourbonCream.jpg" alt="Bourbon Cream Cookies"></p>
<p>i would give WISDOM TOOTH EXTRACTION a positive review for a basket of these babies
</p></div>
<p>What follows is a review by Jeff Gold (<a href="http://www.untoldentertainment.com/boards/memberlist.php?mode=viewprofile&#038;u=55">ue_jeff</a>), our indomitable Game Developer and Keeper of All AS3 Knowledge. </p>
<h2>From the Mouths of Jeffs</h2>
<p>Today I set out to evaluate Kindisoft secureSWF, a product designed to help Flash / Flex developers secure and obscure their flash content from prying eyes. Thanks to several products on the market today (I am going to use <a href="http://www.sothink.com/product/flashdecompiler/">SoThink SWF Decompiler</a> as my point of reference here) it is possible, and quite easy, to open up Flash content found online to view and re-use all of the file&#8217;s source code and art assets. There are many reasons why developers would not want their files being decompiled this way. Theft of ActionScript code, theft of art assets, and hacking of controlled systems are just three.</p>
<p>Log in to the MochiMedia community forums and read <a href="https://www.mochimedia.com/community/forum/topic/game-hacked#0">this post</a> about how one developer&#8217;s game was completely ripped off by someone with a swf opener:</p>
<blockquote><p>psyon<br />
Sep 07, 2009</p>
<p>On of my puzzle games was hacked. The person replaced my site logos, but apparently left my mochibot code in place, so I can see where all the hacked copies are. Anyone familiar with the site OyunKolu.com ?</p></blockquote>
<p>SecureSwf was recommended to us by a number of our Twitter friends, including Chris Hughes from <a href="http://www.flashgamelicense.com/">Flash Game License</a>/<a href="https://www.gamersafe.com/">GamerSafe</a>.  We had been evaluating other encryption software  (most notably <a href="http://www.amayeta.com/software/swfencrypt/">Amayeta SWF Encrypt 6.0</a>) and were not entirely impressed with the Actionscript 3 code obfuscation. With Amayeta&#8217;s product, I found that using anything but the most minimal settings made my swf report many errors.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/robot.jpg" alt="Old School Robot"></p>
<p>ERROR!  ERROR!  DOES NOT COMPILE!
</p></div>
<h2>That New Software Smell</h2>
<p>Upon opening SecureSWF I immediately noticed many options compared to the other products we had tried. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/standard_setup.jpg" alt="secureSWF standard setup"></p>
</div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/simple_app.jpg" alt="A simple charting app that we used in Interrupting Cow Trivia"></p>
<p>Our first victim: a pie chart swf that we use in <b><a href="http://www.untoldentertainment.com/blog/2009/06/19/interrupting-cow-trivia-alpha/">Interrupting Cow Trivia</a></b>
</div>
<p>Being momentarily overwhelmed by all the panels in front of me I decided to just try out the default configuration. On the first simple swf I tried to encrypt, the default configuration worked quite well causing no errors while running the swf.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/default_settings.jpg" alt="secureSWF default settings"></p>
<p>secureSWF&#8217;s default settings
</p></div>
<p>Immediately after encrypting my first swf with Swf Encrypt, I busted out my swf decompiler to see the results. Decompiling the swf was still possible ( I think this will be the case with any swf encryption ) , but inspecting the extracted code I could see a fair deal of obfuscation had occurred. (<em>Obfuscation</em> Translation: Code is EFFED-UP.  &#8211; Ryan)  With the default settings, Class names, function names, and class variables are obscured to things such as &#8220;_-V&#8221; or &#8220;_-02&#8243;, making the code very hard &#8211; but not impossible &#8211; to follow. I did notice immediately that variables scoped in functions were not obscured , but were left as var _loc_1 , var _loc_2 etc. as the compiler defines them (which I suppose is already obscure). </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/code_none.jpg" alt="before secureSWF"></p>
<p>Before secureSWF &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/code_default.jpg" alt="before secureSWF"></p>
<p>&#8230; After secureSWF.
</p></div>
<h2>Giving It Some Welly</h2>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/upTo11.jpg" alt="This one goes to 11"></p>
</div>
<p>My next step was to crank up the security settings on the same simple swf. This time around I went to protection options and checked &#8220;Statement-level Randomization&#8221;, Maxed out &#8220;Control Flow Obfuscation&#8221; and &#8220;Dynamic Code Wrapping&#8221;, and checked &#8220;Break function Calls&#8221;.  Honestly I am not 100% sure what all these things are doing, but I just went for overkill settings.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/cranked_settings.jpg" alt="secureSWF cranked settings"></p>
<p>Overkill settings.
</p></div>
<p>The swf still managed to run without any errors, and like last time I was still able to decompile the swf. Taking a look at the code again I immediately noticed quite a bit more obfuscation at the function level, and the code seemed more verbose and harder to follow. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/code_none.jpg" alt="before secureSWF"></p>
<p>Again, the same code before secureSWF &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/code_max.jpg" alt="secureSWF max settings"></p>
<p>.. and after, with settings cranked to the max.
</p></div>
<h2>Enter the Cow</h2>
<p>So after my successful runs on a simple application, I tried to run a complex application through the process (<b><a href="http://www.untoldentertainment.com/blog/2009/06/19/interrupting-cow-trivia-alpha/">Interrupting Cow Trivia</a></b>, our real-time multiplayer trivia game) once again with default settings. At first the swf seemed to work with no errors, but after doing some stress testing I did notice few issues.  After about 20 minutes of tinkering with the many settings available I figured out the culprit: &#8220;Control Flow Obfuscation&#8221;. Turning this one option off and then cranking all the other options managed to produce a 100% functional swf for me, and after decompiling I could not discern much difference from with the option on.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_10/cow.jpg" alt="Spot the difference"</p>
<p>Spot the difference.
</p></div>
<p>secureSWF has some other interesting features that I had not seen in other products (but, to be fair, I haven’t tried <em>every</em> competing product).  These features include &#8220;Encrypted domain locking&#8221; and &#8220;Literal String Encryption&#8221;. I am not sure how the encrypted domain locking works , but it is a nice option and saves you from having to setup your own site locking in ActionScript each time. (Site-locking prevents your swf from being played on any domain other than the one[s] you specify.)</p>
<p>Literal String Encryption is another nice feature as it allows you to encrypt potentially sensitive text in your code. By default, strings will not be encrypted and will be perfectly readable when the code is decompiled. I am sure this process could be reverse engineered by digging through the code, but either way it just adds another barrier to the hacker to keep him from messing with your work.</p>
<p>Overall, Kindisoft secureSWF seems to be a solid product with lots of customizable features &#8211; far more than its competitors &#8211; that we plan to take advantage of for current and future projects.</p>
<h2>End Jeff Transmission</h2>
<p><b>Ryan:</b> Of course, swf encryption is just one side of the story.  If you have a game that posts information to your server to store high score data, to save games, etc, it&#8217;s important to know that all that stuff is wide open.  Just grab <a href="http://fiddler2.com/fiddler/version.asp">Fiddler</a>, a &#8220;web debugging proxy&#8221;, and run it while you play any game that makes server calls.  With an unprotected game, you&#8217;ll be able to sniff out the exact asp or php page where scores are being posted, and you can use that info to trick the server into posting a high score for you.  You can also use an app called <a href="http://cheatengine.org/">Cheat Engine</a> to wreak serious havoc with an online game, Flash or otherwise.  Here&#8217;s how:</p>
<p><center><br />
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/dLO2s7SDHJo&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/dLO2s7SDHJo&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object><br />
</center></p>
<p>You may be wondering why, for the love of all that is Holy, we would be linking to movies and products about how to hack Flash games in a review of encryption software.  It&#8217;s to illustrate the point that no protection solution is 100% effective (which is also why i have two children running around my house).  Another saying i really like is &#8220;locks keep the honest people out&#8221;.</p>
<p>You&#8217;ll never make your game immune to hacking, but you can throw so many locks and contrivances in the way that it might take some serious skill to unlock them &#8230; and it follows that anyone with enough haxxor skillz to get through all those locks has something better to do with his considerable talents than to post a high score to your free-to-play Escape the Room game.  </p>
<h2>Win a Copy of secureSWF</h2>
<p>Kindisoft is offering a 25% discount on secureSWF through various Flash game development sites, including <a href="http://www.flashgamelicense.com/sponsor_pages/kindisoft.php">FlashGameLicense</a>.</p>
<p>For more articles about Flash and Actionscript, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.</p>
<p>Moo.</p>
<p><center><br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_blog_promo_bar_914868794"
			class="flashmovie"
			width="545"
			height="185">
	<param name="movie" value="http://www.untoldentertainment.com/games/interruptingCowTrivia/img/promo/blog_promo_bar.swf" />
	<param name="wmode" value="transparent" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.untoldentertainment.com/games/interruptingCowTrivia/img/promo/blog_promo_bar.swf"
			name="fm_blog_promo_bar_914868794"
			width="545"
			height="185">
		<param name="wmode" value="transparent" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><br />
</center></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%2F11%2Fsaved-game-a-kindisoft-secureswf-review%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F11%2Fsaved-game-a-kindisoft-secureswf-review%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/11/saved-game-a-kindisoft-secureswf-review/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1667&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/11/saved-game-a-kindisoft-secureswf-review/feed/</wfw:commentRss>
		<slash:comments>14</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>Should You Sell Your Source Code?</title>
		<link>http://www.untoldentertainment.com/blog/2009/08/31/should-you-sell-your-source-code/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/08/31/should-you-sell-your-source-code/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 13:24:45 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bidness]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1598</guid>
		<description><![CDATA[The Not-So-Golden Archbob A thread erupted on the FlashGameLicense community boards surrounding this post by free-to-play Flash game portal owner Yinan Chen, AKA &#8220;Archbob&#8221;, who runs FlashGameNinjaClan (which we &#8220;featured&#8221; in our recent post Why Don&#8217;t You Host Your Own Flash Game Portal?): Mochi Leaderboards and Mochi Coins Understandably, members of the developer community took [...]]]></description>
			<content:encoded><![CDATA[<h2>The Not-So-Golden Archbob</h2>
<p>A thread erupted on the <a href="http://www.flashgamelicense.com">FlashGameLicense</a> community boards surrounding <a href="http://www.fncgamesblog.com/2009/07/29/mochi-leaderboards-and-mochi-coins/">this post</a> by free-to-play Flash game portal owner Yinan Chen, AKA &#8220;Archbob&#8221;, who runs FlashGameNinjaClan (which we &#8220;featured&#8221; in our recent post <a href="http://www.untoldentertainment.com/blog/2009/08/21/why-dont-you-host-your-own-flash-game-portal/">Why Don&#8217;t You Host Your Own Flash Game Portal?</a>):</p>
<p><a href="http://www.fncgamesblog.com/2009/07/29/mochi-leaderboards-and-mochi-coins/">Mochi Leaderboards and Mochi Coins</a></p>
<p>Understandably, members of the developer community took exception to a few things Archbob had to say:</p>
<blockquote><p>
I don’t care how much the developers earn. The ones that make good games can easily make several thousand or tens of thousands per game.
</p></blockquote>
<p>and this gem from another unnamed portal owner:</p>
<blockquote><p>
I am sure Flash Game Developers make a lot of money already. [Microtransactions are] kind of greedy.
</p></blockquote>
<p>Hulk &#8230; rage &#8230; building.  Sigh.  Don&#8217;t get me started.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/oliver.jpg" alt="Oliver Twist"></p>
<p>Greedy??  i think Oliver Twist pulls down more in tuppence and gruel in a fortnight than most indie Flash developers see in as many months
</p></div>
<p>In the article, Archbob does voice some valid concerns about the implementation of MochiCoins and competing services from his industry perspective.  He also goes on to shock some Flash game devs by detailing his practice of paying developers $100-200 for full source code to a game.  This has led to a number of threads and posts on the subject of selling source, not least of all this post from the Vortix Games Blog:</p>
<p><a href="http://blog.vortixgames.com/thinking-about-selling-your-source-code">Thinking about selling your source code?</a></p>
<h2>Your Question, Answered</h2>
<p>At the end of the day, Archbob is just a bidnessman looking to make a buck and doing it the most efficient way possible.  If some devs are willing to part with their code for a pat on the back and some lunch money, it&#8217;s on them, not on Archbob.</p>
<p>But now that we&#8217;re all here, Flash game devs, let&#8217;s talk about this whole source code issue.  What&#8217;s the deal, anyway?  Should i sell my source code?</p>
<p>The short answer to that question is &#8220;no.&#8221;</p>
<p>The long answer is &#8220;no.&#8221;</p>
<p>The answer, translated into French, is <em>non</em> which, loosely translated back to English, means &#8220;no.&#8221;</p>
<p>The answer in the African click language Mbukushu is &#8220;nglOCK&#8221;,  which &#8211; you guessed it &#8211; means &#8220;no&#8221;.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/tribesman.jpg" alt="tribesman"></p>
<p>N&#8217;guthu doesn&#8217;t think you should sell your source code either.
</p></div>
<p>So a new question arises: have <em>we</em>, Untold Entertainment, ever sold our source?  The answer there is actually &#8220;yes&#8221;, but to understand why, i have to take you back to a simpler time.</p>
<h2>Let&#8217;s Do the Time Warp Again</h2>
<p>Picture it: the year is 2007.  George W. Bush is in the White House.  &#8220;Fergalicious&#8221; by pop chanteuse Fergie (featuring will.i.am) tops the Billboard Hot 100, a popular music chart of the day.  And the star of the Flash game development scene is a high-level programming language called ActionScript 2.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/avianFlu.jpg" alt="Avian Flu"></p>
<p>Fergailcious and avian flu, all crammed into one sickening year
</p></div>
<p>&#8220;AS2&#8243;, as it was known in jive-talking developer circles, was not a full Object-Oriented programming language.  You could, with a little facial straining and a lot of undocumented wizardry, shoehorn it more towards an OOP mould, but most of us with a day job and one-week-long game production deadlines couldn&#8217;t be arsed.  Those days, creating a new game meant coding it largely from scratch, or keeping a folder full of code snippets to copy-paste into a new file.  Reusable code was a tale told by futurists and mad-men.  Chaos and disorder ruled the day.</p>
<p>When our clients would request our AS2 source code for a week-long game like <b><a href="http://www.untoldentertainment.com/blog/2008/11/28/eye-in-the-sky/">Eye in the Sky: Spot the Differences</a></b>, it was hard to kick up a fuss.  Many of our clients are kids&#8217; television production companies who convince themselves that they&#8217;ll have the time and money to repurpose these games later, but nothing ever comes of it.  If a different client were to come to us and ask for a nearly identical spot-the-differences game, i&#8217;d probably code it from scratch.  The new client&#8217;s concept would probably be just subtly different enough to warrant a re-write.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/client.jpg" alt="Client"></p>
<p>Okay &#8211; we want a spot-the-differences game EXACTLY LIKE THAT ONE, except instead of spotting differences, you&#8217;re flinging Cinammon Munchy Cheerios, and instead of using the mouse, you&#8217;re using a mind control helmet.  Plus, there are ninjas.
</p></div>
<p>But then along came a stranger to the scene, a wild-eyed and vivacious newcomer with a mane of saffron-coloured hair and a tight pair of rhinestone-studded spandex pants.  &#8220;Actionscript 3&#8243; was his name, and the industry would be changed forever.</p>
<p>The difference between AS2 and AS3 is that Actionscript 3 has really lived up to its promise of code reuse.  Problems that used to take us days or weeks are reduced to literally a single hour, leaving us to catch up on our knitting in those long, tranquil stretches of Wednesday afternoons.   AS3 has also changed the way we negotiate our contracts with clients.</p>
<h2>AS3&#8242;s Impact on Contract Negotiations</h2>
<p>The clients will always ask for source code, and we will always say no, for these very valid reasons:  </p>
<ol>
<li>By selling your source code, you are selling the right to use that code.  You can&#8217;t (contractually) sell your code to someone and then turn around and re-use it in a new game.
<li>With each new project, we re-use and improve upon libraries that we used in past projects.  If we sold the code in those libraries, we couldn&#8217;t (contractually) use them in our next project, so our profit margins would shrink because we&#8217;d have to [technically] re-write the code.
<li>The time and effort we put into some of our libraries is very valuable to us, and can represent literally years of development.  We&#8217;re not going to let that fly out the door for the cost of a few weeks&#8217; development.
</ol>
<p>To use an industrial analogy, selling your source code isn&#8217;t like selling a car.  It&#8217;s not even like selling the plans or the autoCAD blueprints for the car.  It&#8217;s like selling a machine which, when you push a button, <em>makes a car</em>.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/carPlant.jpg" alt="Car Plant"></p>
<p>Here, client. Would you like to buy our assembly line for two hundred bucks?
</p></div>
<p>So no, clients cannot have our source code.  But this presents us with a problem:  what happens when the client&#8217;s database guy needs to integrate the game into the high scores system, but he&#8217;s only available in November, and we&#8217;re finished with the game in October?   Or what happens when the game is a component of a much larger website, and the client requires source code to integrate the game?  Or what happens when the style bible changes and the client wants to make a character&#8217;s hat blue?</p>
<p>These are all real-world examples from our actual experiences.  You hard-lined developers might say &#8220;if they want the hat blue, they can pay us to make that change.&#8221;  But our policy has always been to enable the client to make reasonable changes after the fact, without having to pay an expensive Flash developer for simple modifications.  That&#8217;s why we try to link copy decks in xml files outside the project.  The client shouldn&#8217;t have to pay us more money because they want the Registered Trademark symbol added to a product name. (And, honestly, we don&#8217;t really want to halt production on a new project to make such a small change to an older project anyway).</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/oldMan.jpg" alt="legacyCode"></p>
<p>Staring into the cold, dead eyes of legacy code.
</p></div>
<p>We&#8217;re currently building a music game.  What if the client finds out down the road that the rights to the songs have changed?  Should they have to pay us to put different songs in the games?  In my opinion, no.  So we&#8217;re building the game so that it&#8217;s easy to change the playlist externally.</p>
<p>And the hard-line approach does nothing to solve the problem of site integration.  Clients simply <em>need</em> source code in that case. </p>
<p>So what <em>is</em> the solution?</p>
<h2>Enter the Shared Source Agreement</h2>
<p>Since we started developing games in Actionscript 3, we&#8217;ve signed a Shared Source Agreement on nearly all of our projects.  That&#8217;s where we can:</p>
<ul>
<li>Re-use
<li>Re-configure
<li>Re-sell
<li>Re-skin
</ul>
<p>the source code, and our clients cannot.  But we <em>do</em> give our clients the source code, with a license to modify that code within the current skin.  That means that if we&#8217;re making a game about bears who love the delicious crunchy taste of Goorman&#8217;s Cookies, the client can&#8217;t turn around use the source code to create a game about a bee who can&#8217;t get enough of the satisfying crackle of BingBong&#8217;s Peanut Brittle.  They can do whatever they want with it, as long as they stick to <em>that</em> bear with <em>those</em> cookies.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_30/peanutBrittle.jpg" alt="peanut brittle"></p>
<p>Is brittle?  Is problem.
</p></div>
<h2>The Honour System</h2>
<p>&#8220;But if you give the client your code, aren&#8217;t they just on the honour system?&#8221; you ask.  Sure they are.  The client could turn around and do a variety of things with the code that weren&#8217;t contractually permitted.  But as human beings, we rely on the honour system a great deal.  The honour system is what keeps you from walking down the sidewalk and stabbing someone in the face.  It&#8217;s what dissuades you from veering your car over the yellow line on the road and into oncoming traffic.  i like the honour system.  Let&#8217;s stick with it.</p>
<p><center><br />
<object width="320" height="265"><param name="movie" value="http://www.youtube.com/v/BGPcSd7DDLk&#038;hl=en&#038;fs=1&#038;color1=0xe1600f&#038;color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BGPcSd7DDLk&#038;hl=en&#038;fs=1&#038;color1=0xe1600f&#038;color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object><br />
</center></p>
<p>If it helps you with your own contracts, or if you&#8217;re one of our competitors looking to get an edge on us, here&#8217;s the kind of wording we use when we work with a client on a fee-for-service basis:</p>
<blockquote><p>
The source code for this project is licensed to [Client] under a Shared Source agreement.  Untold Entertainment Inc. retains the exclusive right to modify, reuse, re-skin and sublicense the source code, and any non-brand-identifying multimedia assets.  [Client] may modify the source code solely for the purposes of [project].  Untold Entertainment retains the know-how and materials to this project.
</p></blockquote>
<h2>Know-How! (Contrariwise!)</h2>
<p>When i first saw a client try to retain &#8220;know-how&#8221;, i actually had it stricken from the contract. How could someone else own my knowledge of how i did something?  Did i have to erase my memory or try to blot knowledge from my mind?  (Of course not.  Apparently, i&#8217;m pretty good at <em>naturally</em> forgetting how to do things.)</p>
<p>i came to understand that retaining &#8220;know-how and materials&#8221; is strictly legal language pertaining to a research grant (SR&#038;ED) here in Canada.  He who writes that language into the contract gets to apply for the grant.</p>
<h2>Let Us Retire to the Sitting Room</h2>
<p>So this post is meant to help you out if you&#8217;re looking to do some fee-for-service work (we geev you the Flash, you geev us the monay) and you need this kind of help with your contract.  i hope this post doesn&#8217;t serve as bait for an army of hobbyists telling me how great it is to sell your source code.  As always, i welcome lively discussion either here or <a href="http://www.untoldentertainment.com/boards/">on the boards</a>.  And if this post has taught you nothing and you&#8217;re still interested in selling your source code to me for a few hundred bucks, please drop me a line.  :)
<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%2F31%2Fshould-you-sell-your-source-code%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F31%2Fshould-you-sell-your-source-code%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/31/should-you-sell-your-source-code/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1598&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/08/31/should-you-sell-your-source-code/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Flash AS3: The Only Math You&#8217;ll Ever Need</title>
		<link>http://www.untoldentertainment.com/blog/2009/08/27/flash-as3-the-only-math-youll-ever-need/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/08/27/flash-as3-the-only-math-youll-ever-need/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 13:25:48 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1576</guid>
		<description><![CDATA[i write this post at the risk of being laughed out of the industry by eggheads &#8230; but one of our goals is to help Flash game developers produce the very best content possible. So if this post helps you, we&#8217;re happy to be of service. So let the eggheads laugh. Go ahead and laugh, [...]]]></description>
			<content:encoded><![CDATA[<p>i write this post at the risk of being laughed out of the industry by eggheads &#8230; but one of our goals is to help Flash game developers produce the very best content possible.  So if this post helps you, we&#8217;re happy to be of service.  So let the eggheads laugh.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/nerd.jpg" alt="Nerd"></p>
<p>Go ahead and laugh, Poindexter.  i&#8217;m not afraid of you.
</p></div>
<h2>It&#8217;th a Total Mathacre</h2>
<p>Are you like me?  Are you terrified of numbers?  Does the thought of performing a simple math equation give you dry mouth and sweaty palms? Do fractions give you restless leg syndrome?  And have you been afraid to really dive into programming because you&#8217;re worried about the amount of math it requires?</p>
<p>If you&#8217;ve even dipped so much as a <em>toe</em> in programmatic waters, i hope you&#8217;ve discovered that programming is more about logic than it is about math.  You can create quite sophisticated games with branching storylines and interactivity without ever touching a floating point number (whatever <em>that</em> is).</p>
<p>i&#8217;ll level with you here: math scares the Hell out of me. But somehow, i&#8217;ve been building Flash games professionally for nearly ten years, and my arithmophobia hasn&#8217;t stopped me.  What i&#8217;ve noticed in all my years of game development is that there&#8217;s one simple piece of math from back in grade school that i&#8217;ve used over and over again.  I&#8217;ve probably used it in every single game i&#8217;ve ever written.  But i remember a time when i couldn&#8217;t wrap my mind around what i was trying to do, because i hadn&#8217;t committed those certain math operations to memory.</p>
<p>So i&#8217;ll share them with you today, and i&#8217;ll share the cases where the math comes in handy.</p>
<p>The bit of math i&#8217;m talking about is just simple ratio stuff.  A ratio is a relationship between numbers.  Let&#8217;s jump into a real-world example right away.</p>
<h2>Health Bar &#8211; Timeline</h2>
<p>You have a game character, and he has a health bar.  In code, the character&#8217;s health is expressed as a percentage: your character is at 70% health, let&#8217;s say.</p>
<p>You&#8217;ve built a graphic to depict this on the screen to your player.  Here&#8217;s the absolute easiest way to go about displaying the health properly:</p>
<ol>
<li>Make sure your health bar MovieClip has exactly 101 frames
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/101frames.jpg" alt="101 Frames"></p>
<p>(image truncated to fit on-screen)
</p></div>
<li>Inside the health bar, create a rectangle.  This will be your character&#8217;s health indicator.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/bar.jpg" alt="bar"></p>
</div>
<li>Press F8 and turn the rectangle into a MovieClip symbol.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/symbolize.jpg" alt="symbolize the bar"></p>
<p>Use that little grid to put the registration point at the left edge of the bar
</p></div>
<li>Select frame 101 and press F6 to add a keyframe there on the bar&#8217;s layer.
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/101keyframe.jpg" alt="keyframe on frame 101"></p>
</div>
<li>On frame 1, set the width of the bar to 0.1.  On frame 101, stretch the bar out to its full width. (note:  you may need to turn on the snap tool and move the white registration dot in the middle of the bar to its left edge on both keyframes)
<li>Right-click frame 1 and select &#8220;Create Motion Tween&#8217; in CS3 or &#8220;Create Classic Tween&#8221; in CS4
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/createMotionTween.jpg" alt="create motion tween"></p>
</div>
<li>When you need to display the character&#8217;s health, tell the bar to gotoAndStop(health + 1).  So if health is 0, the bar goes to the first frame. If health is 100%, the bar goes to the 101st frame.  If health is 50%, the bar goes to the middle.   (note: MovieClips don&#8217;t have a frame zero, which is why we&#8217;ve offset everything by 1 frame)
</ol>
<p>So that&#8217;s the old-school way of doing it.  And if you&#8217;re starting out, it&#8217;s still a very viable way of doing things.  Don&#8217;t let the eggheads laugh at you because you are using timeline animation.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/futureSplash.jpg" alt="FutureSplash"></p>
<p>There was a time, long long ago, when the timeline meant something
</p></div>
<p>But what about cases where you want to do everything with code?</p>
<h2>Health Bar &#8211; Cheaterpants Mathless Method</h2>
<p>What we&#8217;re trying to do is to change the width of the health bar using Actionscript 3 code, so that it matches the player&#8217;s health value.  Here&#8217;s the easiest way to do that:</p>
<ol>
<li>Make sure your graphical health bar, at full health, is 100 pixels wide
<li>When you want to update the player&#8217;s health, set the width property of the bar to the health value.  At 50%, it&#8217;s 50 pixels wide. At 100%, it&#8217;s 100 pixels wide.
</ol>
<p>Sounds great, yeah?  We dodged the math bullet for another day.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/dodgeBullet.jpg" alt="Dodge a bullet"></p>
<p>i&#8217;m pretty conviced that, like the Matrix, math is meant to distract us from realizing that we&#8217;re all leading sedentary lives getting our essence sucked out of us
</p></div>
<h2>Health Bar &#8211; Still No Math Required</h2>
<p>But wait &#8230; what if a 100 pixel-wide health bar won&#8217;t fit in your interface?  What if it&#8217;s too big or two small?</p>
<p>Resist the math at all costs!  Just do this:</p>
<ol>
<li>Scale your health bar.  Even if you squish the MovieClip, the width of the bar inside is relative.  It&#8217;ll still be 100px wide at full health, but it&#8217;s inside a clip that&#8217;s getting squished or stretched to make it <em>look</em> like it&#8217;s shorter or longer than it actually is.  This will work out very well if your bar is a vector graphic, because you won&#8217;t get any jaggies when you scale it.
</ol>
<p>Um &#8230; Ryan?  My health bar isn&#8217;t vector.</p>
<p>OR</p>
<p>Let&#8217;s just cut to the chase and use some of that ratio math, huh?</p>
<h2>Biting the Math Bullet</h2>
<p>Alright &#8211; we&#8217;ve done everything we can to avoid using math to solve this problem.  Let&#8217;s take a look at the math that we need.</p>
<p>First, let&#8217;s gather some information.  Let&#8217;s identify what we know, and what we don&#8217;t know. </p>
<p>What do we know?</p>
<ol>
<li>We know that our character&#8217;s health is 70%
<li>We know that 70% is a number out of 100.  If we write it as a fraction, seventy percent is 70/100 (or &#8220;70 <em>over</em> 100&#8243;, or even &#8220;seventy one-hundredths&#8221;)
<li>We know how wide our health meter can be  (let&#8217;s say 360 pixels)
</ol>
<p>What <em>don&#8217;t</em> we know?</p>
<ol>
<li>We don&#8217;t know how wide the bar inside the health meter is supposed to be
</ol>
<p>So that leaves three knowns, and one unknown.  In algebra, our unknown value is often written as <em><b>x</b></em>.</p>
<h2>Finding <em>x</em></h2>
<p>Ah, <em><b>x</b></em>.  My old nemesis.  How you&#8217;ve plagued me with nightmares over the years.  But this time, i have nothing to fear, because i&#8217;m armed with knowledge, and i&#8217;ve been drinking Scotch all afternoon.</p>
<p>A ratio is a relationship between numbers.  Do you see the relationship between the number 70 seated <em>inside</em> the number 100, and the width of your health bar seated <em>inside</em> the maximum possible width of the health bar?  70/100 is the same thing as <em>something</em>/360. (remember: 360 is the maximum width our health meter can be.)</p>
<p>So to put that a more mathy way, 70/100 = x/360.</p>
<p>Or, to express it as something you&#8217;d see in a grade 5 math textbook:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/equation.jpg" alt="Equation"></p>
<p>Freaked out yet?
</p></div>
<p>Oh teh noes!  We&#8217;re in Math Land now!  Everyone hold on to the rope, and don&#8217;t stray off the path.  We&#8217;re gonna get through this thing.</p>
<h2>Ninja Moves, Bitches</h2>
<p>There&#8217;s a bunch of stuff that we can and <em>need</em> to do to this math thing &#8230; this <em>statement</em> &#8230; in order to figure out what <em><b>x</b></em> is.   Anything we do to this statement is called an <em>operation</em>.  You can think of operations as <em>ninja moves</em>.  Let&#8217;s learn the punches, kicks, and spine cracks we need to extract the info we need &#8211; <em><b>x</b></em>.  </p>
<p>Let&#8217;s see if we can beat the crap out of the other numbers to <em>isolate</em> <em><b>x</b></em>.  We want <em><b>x</b></em> on one side of the statement, and every other number on the other side. Then we&#8217;ll wind up with a statement that says &#8220;<em><b>x</b></em> = <em>some number</em>&#8220;.  That&#8217;s the number we want!</p>
<p><b>Ninja Move #1:</b> In an equality statement like this, we&#8217;re saying that the thing on the left is the same as the thing on the right.  So we can swap sides any time we like.  Let&#8217;s swap sides now.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/swap.jpg" alt="Equation Swap"></p>
<p>Heee-YAH!!
</p></div>
<p><b>Ninja Move #2:</b> Just the same, we can flip the fractions upside down, as long as we do it to both sides at the same time.  Let&#8217;s do it!!   </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/flip.jpg" alt="Equation Flip"></p>
<p>WICHIKAAAAPOW!!!
</p></div>
<p><b>Ninja Move #2.5</b> Ok, that kinda sucks.  It&#8217;s easier if <em><b>x</b></em> stays on top of the fraction.  Let&#8217;s reverse that last ninja move!   </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/flip2.jpg" alt="Equation Flip"></p>
<p>ZAMMYFLAMM!
</p></div>
<p><b>Ninja Move #3</b> It looks like we can do anything we want to one side, as long as we do it to the other.  <em><b>x</b></em>/360 is another way of saying &#8220;<em><b>x</b></em> <em>divided by</em> 360&#8243;.  If you take a number and divide it by something, and then you multiply it by the <em>same something</em>, you wind up with your original number.  Let&#8217;s see that in action with some easy math:</p>
<p>6 / 3 * 3 = 6; </p>
<p>(Six divided by three equals two.  Two times three equals six &#8211; the same number we started with.)</p>
<p>And again:</p>
<p>2 / 4 * 4 = 2;</p>
<p>(Two times four equals zero-point-five.  Zero-point-five times four equals two &#8211; the same number we started with.)</p>
<p>So at the moment, our math equation says <em><b>x</b></em>/360.  If we multiply <em><b>x</b></em> by 360, we&#8217;ll get <em><b>x</b></em> all on its own.  It&#8217;s like we&#8217;ve made that /360 part disappear.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/x3601.jpg" alt="Multiply by 360"></p>
<p>WAPAPOW!
</p></div>
<p>BUT REMEMBER: This is an <em>equality</em> statement.  If we kick one side in the face, we have to kick the other side equally in the face. We multiplied the left side by 360 to get rid of the divided-by-360 part, so we need to multiply the <em>other</em> side by 360 as well.  Here we go:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/x3602.jpg" alt="Multiply by 360"></p>
<p>HAPOOO!!
</p></div>
<p>How awesome is that?  <em><b>x</b></em> is all alone on one side of the equals sign.  Now we just do the math on the other side, and we have our answer.</p>
<p><em><b>x</b></em> = 70 /100 * 360<br />
<em><b>x</b></em> = 0.7 * 360<br />
<em><b>x</b></em> = 252</p>
<p>So there&#8217;s our answer!  If the character&#8217;s health is at 70%, and the maximum width of our health bar is 360 pixels, we need to change the width of the health bar to 252 pixels to make it look right.</p>
<p>This is just one real-world example where ratio math comes in handy.  Here are a few more examples where knowing the <em>same</em> ninja moves helps again and again and again:</p>
<h2>Mini-Map!</h2>
<p>You have a character on a big scrolling background, and you want to show a dot on a mini-map in the corner of the screen.</p>
<p>The character&#8217;s x position within the width of the background <em>is the same as</em> the x position of the dot within the width of the mini-map.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/miniMap.jpg" alt="Ratio math helps with your mini-map"></p>
<p>(the background extends past the viewable game area)
</p></div>
<p>character.x/background.width == <b>dot.x</b>/miniMap.width;  // dot.x is our unknown!</p>
<p><b><font color = "#FF0000">Ninja move!  Flip sides!</font></b></p>
<p><b>dot.x</b>/miniMap.width == character.x/background.width;</p>
<p><b><font color = "#FF0000">Ninja move!  Isolate the unknown!</font></b></p>
<p>dot.x/miniMap.width<b>*miniMap.width</b> == character.x/background.width<b>*miniMap.width</b>;  // multiply both sides by miniMap.width to get rid of it on the left side!</p>
<p><b><font color = "#FF0000">Ninja move! Dividing a number by something, and then multiplying by the same something is the same as doing nothing!</font></b></p>
<p>dot.x == (character.x / background.width) * miniMap.width;</p>
<p><b><font color = "#FF0000">Solve for x!</font></b></p>
<p>dot.x = 57;  // (this is after substituting nubmers for character.x, background.width and miniMap.width)</p>
<h2>Colour Slider Control Handle!</h2>
<p>You have a colour slider bar.  The player can crank it up and down to adjust a colour swatch from 0-255.  You want to know the colour value out of 255 based on where the slider was cranked.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/colorSlider.jpg" alt="Ratio math helps with your color slider"></p>
<p>The highest red value is 255
</p></div>
<p>sliderTab.y/maximumSliderRange == <b>x</b>/255;<br />
<b>x</b>/255 == sliderTab.y/maximumSliderRange; // swap sides<br />
x = (sliderTab.y/maximumSliderRange)*255; // get rid of the fraction on the left side<br />
x = 42; // (again, we dropped in the number values for sliderTab.y and maximumSliderRange to arrive at this number)</p>
<h2>Flash Video Progress Bar!</h2>
<p>You have a progress bar on your flv video player.  You want to know how far along to position the progress marker based on how much of the video has played.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/videoPlayer.jpg" alt="Ratio math helps with your video player"></p>
<p>Play us out, Keyboard Cat
</p></div>
<p>marker.x = movie.position / movie.duration * totalWidthOfTheProgressTrack;</p>
<h2>Shooter Game Accuracy Rating!</h2>
<p>You have a shooter game. The player scored 37 hits out of a possible 553 targets.  You want to express that as a hit accuracy percentage when the level ends.</p>
<p>accuracy = 37 / 553 * 100;</p>
<h2>See, Mr. Sprague?  That&#8217;s How It&#8217;s Done</h2>
<p>i was never really terrible at math &#8211; i just didn&#8217;t enjoy it.  i didn&#8217;t like the fact that there was only one right answer, and that in early grades, we were timed.  In the first grade, we had to compete &#8220;Minute Math&#8221; sheets &#8211; one minute to burn through a page full of sums.  i appreciate the thinking behind this, but wtf?  There was no such thing as &#8220;Minute English&#8221; (write the alphabet as fast as possible) or &#8220;Minute Art&#8221; (string together a painted macaroni necklace in under a minute or face failure).</p>
<p>In high school, i finally failed my first math course &#8211; grade 12 finite.  One of the reasons i never really took to math &#8211; and it&#8217;s a big one &#8211; is that i never had a math teacher who taught it to me like it needed to be taught.  If my high school math teacher had told me that by learning physics, i could make a video game character jump and react to gravity and push boxes around, or that by learning trigonometry i could code my own billiards game, i would have lapped it up.  As it stands, i still struggle with my fear of numbers in my professional career &#8211; the fear of failure, the constant imagined tick of the clock while i try to figure something out.</p>
<p>Just talk about ninja moves and video games, and everything will be fine.  Ratio equations may not, honestly, be the only math you&#8217;ll ever need, but if you have a bad history with numbers like i do, it&#8217;s a decent start.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_27/ninjas.jpg" alt="LolCat Ninja"></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%2F2009%2F08%2F27%2Fflash-as3-the-only-math-youll-ever-need%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F27%2Fflash-as3-the-only-math-youll-ever-need%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/27/flash-as3-the-only-math-youll-ever-need/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1576&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/08/27/flash-as3-the-only-math-youll-ever-need/feed/</wfw:commentRss>
		<slash:comments>9</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
