<?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; Actionscript</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/actionscript/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 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>Flash Gaming Summit 2010</title>
		<link>http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 09:09:38 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2277</guid>
		<description><![CDATA[The Flash Gaming Summit, a (now) annual event sponsored by the usual suspects in the Flash gaming world, has come and gone. Scheduled strategically a day before the Game Developers Conference begins, the mini-con fills an auditorium with everyone who&#8217;s anyone in the Flash gaming scene, from solo hobbyist developers to extremely successful yet Satanic [...]]]></description>
			<content:encoded><![CDATA[<p>The Flash Gaming Summit, a (now) annual event sponsored by the usual suspects in the Flash gaming world, has come and gone. Scheduled strategically a day before the Game Developers Conference begins, the mini-con fills an auditorium with everyone who&#8217;s anyone in the Flash gaming scene, from solo hobbyist developers to extremely successful yet Satanic Facebook game developers. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/zynga.jpg" alt="zynga"></p>
<p>Zynga actually killed this dog, because no one would gift it to any of their Facebook friends.
</p></div>
<p>i spent the past month tweeting rotten poetry to the FGS Twitter account.  The entry fee for the conference wasn&#8217;t very expensive, and the OMDC (Ontario Mosquito Death Camp) is footing the bill for half of our activities in San Francisco this week through the Export Fund.  But if i see a chance to get something for free, whether it&#8217;s a conference pass or a Scientological stress test, i jump at the chance.  And lo and behold, at the eleventh hour, @FGS comped me a pass &#8230; but not before GamerSafe/Flash Game License comped me another one.  i felt like the <em>one</em> girl in grade eight who had breasts &#8230; so much unwarranted attention!  Thanks, everyone.  i was very happy to be there and to meet everyone in person.</p>
<p>If you couldn&#8217;t make it, you missed a pretty solid show.  i almost wish that GDC was one day only, and FGS dragged on for a month or more.  Here&#8217;s a run-down of who i saw and what i learned.</p>
<h2>Bidness</h2>
<p>The show was split into two tracks: business on the upper floor, and creative on the lower floor.  It was the conference equivalent of a mullet.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/mullet.jpg" alt="mullet"></p>
<p>Flash Gaming Summit: Business on the main floor, party in the basement.
</p></div>
<p>i stayed upstairs the entire time.  Adam did a great job with <b>Canabalt</b>, but i&#8217;d much rather learn how to make <em>myself</em> money than to hear about how he built a walk-in freezer made of money to store all his money, or whatever. </p>
<h2>Opening Keynote &#8211; Jameson Hsu</h2>
<p>Jameson&#8217;s a good guy, but a somewhat nervous presenter.  He announced a few new initiatives from his company, MochiMedia: the Mochi Social Platform, and the Mochi GAME Developer Fund.  i have NO IDEA why they capitalized &#8220;game&#8221; &#8230; maybe to differentiate it from their Mochi ACCOUNTING SOFTWARE Developer Fund?  Not sure.  The idea is that Mochi&#8217;s new supreme Chinese overlords, Shanda Games, who recently bought Mochi for $80 million, would put up $10 million to help developers make games.  i didn&#8217;t quite find out what the catch was &#8230; obviously, Mochi wants any games it funds to tie into as many of its own initiatives as possible &#8211; microtransactions, ads, and the new Mochi Social Platform.</p>
<p>The Platform didn&#8217;t really seem like a new or Earth-shattering announcement &#8211; you could see them building up to this at least two years out.  Mochi is creating an easy-to-implement solution where you can leverage social media &#8211; Facebook, MySpace, YourPants, etc &#8211; with a single, simple API.  It&#8217;s nice, but like many of the other services Mochi offers, you can roll your own and have a lot more control, but at a longer development time and greater financial risk.  With Mochi&#8217;s solution, you gain ease of use but forfeit control.</p>
<p>i was pretty happy to see the developer fund, because it was a little tiring to keep hearing Mochi and pals chanting the mantra &#8220;Make multiplayer games!  They rake in a lot more dough!&#8221;  They also <em>cost</em> a big wad more to develop.  Our first foray into a multiplayer game, <b><a href="http://www.interruptingcowtrivia.com">Interrupting Cow Trivia</a></b>, means that my daughters won&#8217;t be able to get braces and will look like snaggle-toothed freaks the rest of their lives.  The game was expensive to build, in other words.  Mochi&#8217;s fund, they say, aims to mitigate the risk of that more expensive development, and i think it&#8217;s a step in the right direction.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/badTeeth.jpg" alt="bad teeth"></p>
<p>Sorry, sweety &#8211; daddy had to build a multiplayer game.
</p></div>
<p>They&#8217;re still, however, giving away MochiCoins to players, which the players then use to buy virtual items in your microtransaction-enabled games, which is kind of like the government approving citizens to walk out of your shop with a free television or whatever, so Mochi remains on my handle-with-care list.</p>
<p>Mochi&#8217;s Ada Chen said she was afraid to open her mouth around me because she never knew whether i&#8217;d write something negative about the company.  Roll with the punches, Ada!  If you didn&#8217;t have critics, you&#8217;d go mad with power.  i&#8217;m just here to keep you honest.</p>
<h2>4 Keys to a Successful Social Game that All Game Developers Should Know</h2>
<p>On the panel:</p>
<ul>
<li>Dan Fiden &#8211; Playfish
<li>David Stewart &#8211; Playdom
<li>Gavin Barrett &#8211; Crowdstar
<li>Mark Skaggs &#8211; Zynga
</ul>
<p>This panel was stacked with Zynga, Playdom, Playfish and Crowdstar, the companies who &#8211; God love em &#8211; have actually turned a buck on Flash &#8230; and a BIG buck at that.  When the same companies spoke at Casual Connect in Seattle last year and dropped the bomb about how much they were raking in on Facebook, i could tell the whole conference was freaking out and trying to figure out how to get some of that action.  i also knew that by then, it was too late &#8211; these guys had sewn it up, a fact that they repeated often throughout this panel.</p>
<p>This panel, and nearly every other at the conference, was plagued with some uniformly terrible moderation.  Moderator Sana Choudary, and every panel moderator at the show, pulled the same rookie error of asking very broad questions, tiptoeing around controversy, and ending on the same ridiculously vague question &#8220;Where do you think the future of _____ is going?&#8221;  Oh <em>maaaaaaan</em> &#8230; if i have to hear that question one more time, i&#8217;m positively going to <em>bite my pillow</em>.</p>
<p>The moderator asked the audience to tweet questions.  The first one i came up with was &#8220;Are you making games or slot machines??&#8221;  Not very original, i know, but i have a hunch i wasn&#8217;t the only one thinking it.  i could only hear these companies talk about the fun, original and interesting <em>games</em> they were making for so long before i really had to give my head a shake.  At least one other person in the peanut gallery had tweeted the same question.  Unfortunately, the moderator censored the questions, and it was as if she&#8217;d been coached to do so &#8211; like when you interview a celebrity and you&#8217;re not allowed to ask about her recent divorce or that thing on her neck.  Zynga was here, and they were rich, and they were only going to deign to visit us from Mount Olympus if we were all on our best behaviour.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/quietPanel.jpg" alt="Quiet Panel"></p>
<p>Um &#8230; are any of you guys actually going to say anything?
</p></div>
<p>The end result was that the questions were vague, the answers were <em>more</em> vague, and at least two of the panelists were ignorant enough to clam up about numbers and say &#8220;it&#8217;s all on the Internet &#8211; go do some research and look it up.&#8221;  No, fellas &#8211; i didn&#8217;t fly from Toronto to California and haiku my way to a free pass to be told to go Google my questions about your companies.  You&#8217;re on the panel for a reason, and i&#8217;m in the audience for the same reason. Me: questions. You: answers. Telling people to &#8220;just Google it&#8221; is ignorant.</p>
<p>The conclusion the panel came to was that yes, you can make lots of money on Facebook &#8230; <em>if you&#8217;re Zynga or Playdom or Playfish or Crowdstar.</em>.  A few other points of interest:</p>
<ul>
<li>Games need to be designed with social hooks from the ground-up. Retrofitting doesn&#8217;t work.
<li>Mark &#8211; if your numbers are going down, it’s time for a sale!
<li>Mark &#8211; &#8220;Zynga collects 5 terabytes of data a day. (Ryan &#8211; holy SH*T!)  Don’t underestimate the data side of this business.&#8217;
<li>Gavin &#8211; &#8220;The support of your community and your interaction with them is the most important thing you can do.&#8221;
<li>Mark &#8211; (answering &#8220;what&#8217;s a minimum bar for success?&#8221;) &#8220;5 million daily active users.&#8221; (Ryan &#8211; DOUBLE holy sh*t!)
</ul>
<p>In conclusion: if you can collect and parse 5 terabytes of data a day, pull in and retain 5 million daily active users, and hook grandmas to your virtual slot machine like they&#8217;ve got a crack habit, you too could be the next Zynga.  It&#8217;s <em>that easy</em>.</p>
<h2>Money in Flash: Next Gen Monetization of Flash Games</h2>
<p>On the panel:</p>
<ul>
<li>Chris Hughes &#8211; flashgamelicense.com
<li>Jim Greer &#8211; Kongregate
<li>Justin Wong &#8211; Mochi Media
<li>Vikas Gupta &#8211; Social Gold
</ul>
<p>Initially, i was a bit annoyed during this panel.  The fellas weren&#8217;t talking about some space-aged monetization techniques <em>from the future</em> that i&#8217;d never heard of.  They were talking about microtransactions, mostly &#8211; <em>current gen</em> stuff, not next-gen stuff like the topic promised.  Still, it&#8217;s not fair for me to demand new monetization ideas when i (and many, many others i know) haven&#8217;t even made good on the current techniques.  </p>
<p>Here were a few nuggets from this panel:</p>
<ul>
<li>Jim says Kongregate&#8217;s best multiplayer game is raking in 5x more cash than their best single-player game
<li>Justin reiterated the thing that most successfully drives microtransactions: &#8220;Getting the player to the point of need.&#8221;  That&#8217;s where, for example, the next level is going to SLAUGHTER the player, and he knows it, but hey!  He can buy a bigger gun for two bucks.  It&#8217;s a lot like putting a condom machine in the bathroom of a cougar bar.  The point of need drives more sales.  (That&#8217;s my analogy, not Justin&#8217;s.)
<li>Jim says Kongregate is making about 1/3 of its revenue from virtual goods, and 2/3 from advertising.  He expects that to level out to about 50/50 in the next year.
<li>Jim added that Kongregate makes about half of its ad revenue in the 4th quarter, when its advertisers need to sell video games to people for Christmas
<li>Justin says Mochi makes about 15%-ish of its revenue from virtual goods sales
<li>Echoing the sentiments of the previous panel, the lads emphasized that games need to be built with microtransactions in mind from square one.  Retro-fitting an older game with new microtransactions is not as effective.
<li>Jim says analytics are important, and adds that you should &#8220;watch what [players] do, not what they say.&#8221;  i&#8217;ve heard this before &#8211; believe it or not, the two can be completely different.
<li>Jim says &#8220;The vast vast majority of revenue comes from credit cards and Pay Pal.&#8221; &#8220;$100 at a time is the most common.&#8221;  (Surprising! Higher than i expected.)
<li>Chris and Vikas agreed that once you get a player over the initial hurdle of paying the first time, it&#8217;s much easier to get that player to continue paying.  Chris added &#8220;If you can sell a dollar to a user, you can sell fifty dollars to a user.&#8221;
<li>Vikas offers that the best way to use subscriptions is in conjunction with virtual goods payments.  You offer virtual goods deals or bundles with subscriptions that end up saving the player money.  He says the two are a fantastic combination.
<li>Justin says games that used Facebook Connect saw a 30% jump in gameplays.
</ul>
<p>One of the most interesting insights i got out of this panel came about when one of the guys from Yummy Interactive took the Q&#038;A mic for the first of two somewhat obnoxious self-promoting speeches.  He pointed out that you could also sell games for a flat fee (which conveniently ties into Yummy&#8217;s model of selling a Flash wrapper for downloadables).  What ensued was a tense, almost exasperated exchange that gave me an Aha! moment.</p>
<p>Casual downloadable titles from sites like Big Fish Games started out at $20.  The price has dropped over the year, thanks to market interference by folks like Amazon, to the point where it&#8217;s now at about $7.  Flash games started at zero dollars, and have been struggling to increase.  So you have this race to the bottom in the casual downloadable space, and a race to the top in the Flash games space.  Is the point at which the two segments meet the perfect price to charge for an online video game?  Or will Flash games prove that microtransactions in multiplayer games suck way more money out of people&#8217;s pockets than a $20 price point ever did?</p>
<h2>Adobe Tools and Services for Flash Games</h2>
<p>It was more than a little embarrassing that Adobe, the people who <em>make</em> the software that all the conference attendees were meeting and speaking about, had such an inept presentation.  Technically, it was like giving a camcorder to a spider monkey and expecting it to take coherent pictures.  The presenters tried to show off Flash Player 10.1 running on a number of smartphones with a very dim presentation camera, and one monitor cable that they constantly had to pull out of the camera and plug into the laptop when switching between demos and slides.  The demos of games like Bloons on the various handheld devices ran disappointingly slowly.  One of the presenters swallowed her mic or something by the end of the talk, and had to lean into her fellow presenter&#8217;s chest and talk into his lavalier.  Just awful.</p>
<p>The whole time, i felt very bad for them, struggling like they did with bad tech, bad demos and no new information to share, like the release date for Flash CS5.  It was the <em>Flash</em> Gaming Summit, Adobe.  Throw us a frickin&#8217; bone.</p>
<p>One demo that did pique my curiosity showed a Flash game online where the player could log in using Facebook, MySpace or YourPants, and then another player could log into the same game on the iPhone, and the two could play the game on the same network.  This was a demonstration of a new initiative called Adobe Services.  i need to ask them about it at GDC, but i can&#8217;t imagine what they&#8217;d charge for that setup.  My guess is $lots.</p>
<h2>The Mochis Award Show, Sponsored by Kongregate</h2>
<p>This was a chance to see the usually serious-faced Jim Greer from Kongregate have fun and lampoon himself a bit, which was nice.  The award winners all look like they deserved their hardware, but i was dismayed to discover i hadn&#8217;t heard of most of the games in the running.  i knew <b>Canabalt</b> <b>Machinarium</b>, and had only heard of <b>Time Fcuk</b> because of a podcast i did with the developer, Edward McMullen.  It underscored the fact that this past year has been non-stop work for me, and that i need to lighten up and start playing more games.  i shouldn&#8217;t be such a Jim Greer all the time.</p>
<p>(i keed, Jim!  i keed!  Don&#8217;t hate.)</p>
<h2>Monetizing Your Game Outside of Sponsorship</h2>
<p>This panel consisted of:</p>
<ul>
<li>Colin Northway, Fantastic Contraption
<li>Daniel James, Three Rings
<li>Sian Yue Tan, Rocketbirds
<li>William Stallwood, Cipher Prime
</ul>
<p>This was a GREAT panel.  i absolutely loved that it inadvertently pitted three young bucks, whose recent successes had not yet been proven as deliberate business savvy or lucky strikes, against Daniel James, who&#8217;s had a chance to very publicly succeed and fail over the years he&#8217;s helmed Three Rings.</p>
<p>When asked how they chose a price point for their games, the answers were naive:</p>
<p>Colin &#8211; &#8220;Well, <b>World of Goo</b> cost $20, and i figured my game was half as good as <b>World of Goo</b>, so i charged $10.&#8221;</p>
<p>Sian Yue &#8211; &#8220;We looked at $10 and $20, and went with $15 cuz it was about halfway.&#8221;</p>
<p>Adorable.</p>
<p>i could see Daniel biting his tongue, but knowing him, it wasn&#8217;t going to last long.  Finally, he said in his refined British accent &#8220;So &#8230; you guys basically pulled your numbers out of your butts?&#8221;</p>
<p>Soon after, Sian Yue sagely added &#8220;We chose a price and we stuck with it.  Moving your price point around is never a good idea.&#8221;</p>
<p>i could see Daniel facepalm using <em>only his face</em>.  Having been to his monetization roundtables at GDC for the past few years, and knowing how much they monkey with their payment systems and pricing strategies, i knew he was about to assplode.  Finally, he couldn&#8217;t resist speaking up: &#8220;Actually, there&#8217;s a lot of scientific research that proves you <em>should</em> move your price point around.&#8221;</p>
<p>Oh <em>snappeth</em>!  Thou didst <em>not</em>!  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/daniel.jpg" alt="Daniel James and Friends"></p>
<p>Daniel James surreptitiously checks the walls for &#8220;Exit&#8221; signs.
</p></div>
<p>The old dog had schooled the young pup.  Here&#8217;s where Daniel&#8217;s wisdom and experience shone through.  He said it was alright to make that initial guess, but that you should &#8220;be diligent about testing your hypotheses.&#8221;  He went on to explain that his company had spent 5 million dollars on <b>Whirled</b>, only to see a $300k return.  He said they could have done the same learning in far less time with far less money by testing their hypothesis early and often.</p>
<p><center><br />
<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/IZLz04Hu0_U&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IZLz04Hu0_U&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object><br />
</center></p>
<p>i had read Daniel&#8217;s blog where he explained the lesson learned from <b>Whirled</b>.  He said that Three Rings was now testing Facebook games in small doses to see what would take off.  i had no idea what this meant.  Were they building portions of the games?  How could you know if a game would do well if you only built a fraction of it?</p>
<p>He clarified the strategy:  they were putting together highly-target Facebook banner ads for games that didn&#8217;t exist yet.  Each ad would lead to a landing page with a button where the potential player could sign up to be notified about the beta, which may not ever happen.  Daniel said that one of the theoretical games out-performed the others 5 to 1 in terms of interest, so that&#8217;s the one they&#8217;re going to build.</p>
<p>They even did granular testing on THAT game.  They did at least two ads with the same artwork and a different game name.  One name tested better than the other, so that&#8217;s the name they&#8217;re gonna use.</p>
<p><em>Brillant</em>, as any true Brit would say.</p>
<h2>Everything About Sponsorship &#038; Licensing</h2>
<p>What a panel!</p>
<ul>
<li>Greg McClanahan, Kongregate
<li>Joel Breton, AddictingGames.com
<li>Lars Jornow, King.com
<li>Robin Yang, Candystand
</ul>
<p>Of all the panels, this was probably both the worst and the most delightful.  Let me explain.</p>
<p>i&#8217;m told that AddictingGames.com were harangued by a developer last year for encouraging the Flash devs in the room to submit their games for free to that portal.  It was really nice to see how, after one year, the tide had turned, thanks in no small part to the effort of Chris Hughes and the Flash Game License team.  FGL clearly became the Great Equalizer in the war between the portals, who want as much content as possible for as little money as possible, and the developers, who deserve to be paid for their work.</p>
<p>You could tell that everyone on the panel, now matter how much they downplayed it, used FGL to find the developers they sponsored.  Robin, the most intolerable panelist, along with Joel, continually made the appeal for developers to forge a relationship with the portals, ostensibly so that devs would skip FGL and continue to deal directly with Candystand.  The advantage, of course, is that Candystand avoids a bidding war on FlashGameLicense.</p>
<p>Greg from Kongregate cottoned on to this real quick and said &#8220;what&#8217;s the advantage to the developer of going to you first?&#8221;  Robin and Joel each tried to spin a yarn about how they know their audience best and can give developers suggestions about how to build their games (uh &#8230; great?), and how they&#8217;ll put all kinds of money and promotion into a sequel if the first game does really well.  With a huge grin, Greg said &#8220;ANYONE  will give the dev all kinds of money and promotion for a sequel to a successful game!&#8221;  He put a fine point on it by saying that devs should go with the highest bidder, period.  Robin&#8217;s angle was so transparent that i could hardly believe her audacity.  The closest parallel that came to mind was when George Bush went on teevee right after the Shock and Awe bombing campaign in Iraq and said &#8220;Now, Iraqis, please stop setting your oil fields on fire.  Those are precious, and they&#8217;re the inheritance of the Iraqi people.&#8221;  </p>
<p>Really, Bush?  You don&#8217;t want the Iraqis to set their fields on fire because it&#8217;s for THEIR own good?  Aren&#8217;t you the least bit interested in maybe having a little bit of that oil for yourself?  Not even the <em>teensiest</em> amount?</p>
<p>It was justice, to see the reps from those two big portals twist and squirm before an audience that, in the course of a year, had begun to turn the tables on them.  The white masters were suddenly <em>so concerned</em> for the welfare of their newly-emancipated slaves.</p>
<p>Ok &#8211; i&#8217;m probably going overboard with these analogies.  The Iraq War?  Slavery?  i&#8217;m a step away from Hitler.</p>
<p>&#8230;.</p>
<p>Oh, what the Hell?  <em>Candystand and AddictingGames are Hitler</em>.  There.  i&#8217;m officially a hack.</p>
<h2>Turbulence Ahead: The Ups and Downs of Getting a Premium Flash Game to Success</h2>
<p>The last session of the day was a solo flight by Tim Flowers of Gabob LLC, who made six figures on <b>Now Boarding</b>.  i was initially worried that, like many developer talks, Tim&#8217;s would have a very narrow focus with very few take-homes and loads of inapplicable advice, like &#8220;then my auntie died and i got some money, so i was able to fund the next portion of my game.&#8221;</p>
<p>i found myself actually hanging on every word of what Tim said.  His whole plan for his game had been the exact plan i&#8217;d had for <b><a href="http://www.untoldentertainment.com/blog/kahoots-designer-diary">Kahoots</a></b> &#8211; push the game out to casual downloadable portals, who take an 80% cut.  His art style, liberally borrowed from <b>Catch Me If You Can</b>, reminded me of the pop-art style we adopted for <b><a href="http://www.interruptingcowtrivia.com">Interrupting Cow Trivia</a></b>.  i wrote down everything Tim said voraciously, and after asking him a few questions at the mic, returned to my seat and somehow lost my notebook in the process.  He said some absolutely smart things that are now lost to time &#8230; or to the two video cameras that taped the session.  Know this: daddy wants the video tape of Tim&#8217;s session.  i suggest that if it&#8217;s available, you watch it too!</p>
<h2>NICE TO MEEEET YOOOUUUU!!!!!  (WHAAAAT???)</h2>
<p>All in all, it was a fantastic day, and i didn&#8217;t want it to end &#8230; until the after-party started, and the organizers thought it would be a simply smashing idea to blast incredibly loud club music into everyone&#8217;s ears at a <em>networking event</em>.  i DO NOT understand why companies do that.  After i grew tired of yelling into people&#8217;s faces for hours, i left the party and started writing this postmortem back at my hotel.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_09_09/party.jpg" alt="party"></p>
<p>You can almost hear how loud it was from looking at this picture.
</p></div>
<p>GDC starts tomorrow, and i&#8217;ll do my level best to pussy out of Tuesday&#8217;s parties to write more for your enjoyment.</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%2F09%2Fflash-gaming-summit-2010%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F03%2F09%2Fflash-gaming-summit-2010%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/09/flash-gaming-summit-2010/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2277&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/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>What&#8217;s Wrong with Ontario Colleges? Part 2</title>
		<link>http://www.untoldentertainment.com/blog/2010/02/23/whats-wrong-with-ontario-colleges-part-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2010/02/23/whats-wrong-with-ontario-colleges-part-2/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 21:52:12 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Bizarre]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Preschool]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=2224</guid>
		<description><![CDATA[This series is called &#8220;What&#8217;s Wrong with Ontario Colleges?&#8221; A number of you have pointed out, on Twitter and elsewhere, that what i&#8217;m describing is what&#8217;s wrong with all colleges. But now, i want to shine the spotlight on perhaps an unexpected target, and suggest that not only are colleges flawed, but so too are [...]]]></description>
			<content:encoded><![CDATA[<div class="invisible">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/fail.jpg">
</div>
<p>This series is called &#8220;What&#8217;s Wrong with Ontario Colleges?&#8221;  A number of you have pointed out, on Twitter and elsewhere, that what i&#8217;m describing is what&#8217;s wrong with <em>all</em> colleges.  But now, i want to shine the spotlight on perhaps an unexpected target, and suggest that not only are colleges flawed, but so too are their customers.  </p>
<h2>Part 2: The Students</h2>
<p>In order to discover why Ontario colleges can&#8217;t seem to produce workplace-ready graduates for the casual games/rich media content industry, i went deep undercover as a fledgling teacher at a Toronto college that shall remain nameless: Hernando Velasquez School for the Digitally Inclined.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/campus.jpg"></p>
</div>
<p>i went to &#8230; oh, damn.  i just said the name, didn&#8217;t i?  Unfortunately, the backspace key on my keyboard has been rigged to issue a low-grade electrical shock every time i press it, so i have no choice but to speak to you about this frankly and honestly.</p>
<p>So i completed 200 one-armed push-ups on my knuckles, and then accepted a part-time position at the school.  i&#8217;m teaching a second-semester Flash course. Owing to my sense of self-preservation, i won&#8217;t identify any one student.  There&#8217;s enough trouble to go around for me to treat the entire student body as one collective asspain.  Game Development is the type of program that attracts a certain type of person, and the blame for that is shared by both the institution and its customers.</p>
<h2>Everyone Can Get a Job Making Games</h2>
<p>Colleges are businesses first and foremost. They need to offer desirable products. The perception is that purchasing their product will provide you with sufficient training to seek and  (hopefully) land a position in that field.  This is not the stated goal of <em>all</em> colleges, mind you &#8211; i remember clearly that when i recommended to Durham College as a member of their advisory panel that they increase their Flash offering to improve their students&#8217; employability, the school&#8217;s teacher rep said &#8220;oh &#8211; we&#8217;re not here to get the students <em>jobs</em>.  We&#8217;re here to facilitate their exploration of their <em>art</em>, of their chosen pursuit.&#8221;  Yes &#8211; that actually happened.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/hippies.jpg"></p>
<p>Get out of my education system, you fekkin&#8217; hippies.
</p></div>
<p>But Hernando Velasquez School for the Digitally Inclined is a different story.  They proudly proclaim in their literature that 90% of their grads get jobs within the first six months of graduating.  Note that they don&#8217;t say 90% of their grads get jobs <em>in the industry for which HVSD trained them</em>.  They just claim that the students were employed within six months.  Now, really, since we all need to get a job doing <em>something</em>, this is actually an alarming stat:  Hernando Velasquez is tacitly admitting that 6 months after graduating, 10% of their graduates are either unemployed or <em>dead</em>.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/tombstone.jpg"></p>
<p>(i suppose a few could be idle rich, but it takes the bite out of my punchline.)
</p></div>
<p>So the college is under pressure to put together an attractive offering in its course calendar.  Nothing&#8217;s hotter than a job in the oft-heralded video game industry, so colleges across the province (country, world) are now purporting to train students in the video game industry.</p>
<h2>Who Applies?</h2>
<p>Now, let&#8217;s assemble a profile of the average teenaged male in high school to whom this offering might appeal.  He&#8217;s tall.  He&#8217;s gawky.  He plays video games all the time.  He masturbates to the underwear pages in the Sears fliers.  He&#8217;s not bright enough to be a doctor, or he&#8217;d apply for in pre-med.  He&#8217;s not bright enough to go to University at all, in fact.  Ontario high schools are usually streamed, and it&#8217;s generally accepted that kids in the upper stream go on to University, and kids who take the lower general-level courses wind up in either college or prison.  This is not by rule, but by reputation.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/gameGeek.jpg"></p>
<p>ima make gaymezzors when iz grow&#8217;d up!!
</p></div>
<p>So this college-bound gamer has two options to him: he can enroll in the college&#8217;s programming course, or he can take their video game development program.  Programming likely has grade 12 math prerequisites, and he&#8217;s not nearly smart enough for that.  The video game program is an <em>art</em> program.  So is this guy a fabulous artist?  Probably not, or else he&#8217;d be taking a fine arts program somewhere.  So he&#8217;s a gawky, hairy-palmed male gamer with perhaps no remarkable drawing skills and no great ambitions to use his grey matter in post-secondary education.  This &#8211; THIS is the student who enrolls in the game development course at XYZ college.  And THIS is the only type of kid who gets a shot at learning Flash, because we&#8217;re not teaching Flash very much in University, and we&#8217;re likely glossing over it in college-level programming.  </p>
<p>And THIS explains why most of the Flash shops i know are trying to hire, with no luck.  As i mentioned in the <a href="http://www.untoldentertainment.com/blog/2010/02/18/whats-wrong-with-ontario-colleges-part-1/">previous post</a>, it&#8217;s a ten-year-old problem.</p>
<h2>The No-Fail Generation</h2>
<p>There&#8217;s another important thing to understand about our game dev program applicant.  He hearkens from a generation of kids who, as of the late 1990&#8242;s, were unable to fail.  It&#8217;s true: changes to the high school curriculum brought about by the Ministry of Education forbade teachers from flunking grade nine students.  No matter how truant, lazy, or downright <em>dumb</em> a student was, he would sail on straight through the ninth grade.  In my experience working as a part-time youth pastor at my church, i found there are even more cracks for these kids to slip through.  i&#8217;ve known more than a few kids who should be failing, should be held back, but are repeatedly promoted to the next grade by an education system that doesn&#8217;t want to bruise their egos.  Anecdotally, my friend who works at a major Canadian chain of retailers for young people tells me that when these kids get part-time jobs, screw up, and get fired, it&#8217;s an absolute shock to the system.  They&#8217;ve <em>never failed</em>.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/ralph.jpg"></p>
<p>Me fail Burger King?  That&#8217;s unpossible!
</p></div>
<p>i was invited by colleagues of mine to lecture at a game development program at Humber College here in Ontario.  The course outcome was to complete a Flash game. The class was divided into two groups of about eight students.  The students had four months to collectively complete a flip n&#8217; match memory game in Flash.  And they were struggling.</p>
<p>Let me just punctuate that for you:  it wasn&#8217;t one game per student.  It was eight students working as a team to complete one game.</p>
<p>They were taking the typical college-level token Flash Actionscript 3 course along side their game dev course, and were swearing a blue streak at what they called an impossible task.  A game, they said?  A <em>full game</em> in four months with only <em>eight people??</em>  They told me it couldn&#8217;t be done.</p>
<p>i told them that for an experienced <em>solo</em> Flash developer, a flip n&#8217; match memory game was the work of a single afternoon.  They didn&#8217;t believe me.  So for the next three hours, i sat down and walked them through the process of building the game from scratch. Along the way, i pointed out all kinds of programming shortcuts they could take, dropped sparkling gems of advice that would speed up their workflow, and built a functioning flip n&#8217; match game before their very eyes.</p>
<p>Or it <em>would have</em> been before their very eyes, if any of them had been watching.  For the most part, they futzed around on their computers with other projects, chatted to their friends on Windows Messenger, or surfed the underwear pages of the online Sears catalogue.</p>
<p>One particularly slimy student who had been glued to Facebook for the entire lecture slithered up to me after class and held out his keydrive. Like a greasy lounge lizard trying to pick up a chick in a low-rent bar, he said &#8220;Yyyyyeah, uh &#8230; do you suppose i could just &#8230; put that finished game on my kkkeydrive?&#8221;</p>
<p>i had two words for him.  The second word was &#8220;you&#8221;.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/gorilla.jpg"></p>
<p>He was lucky to escape the room without any poo-flinging.
</p></div>
<h2>Playing Games vs. Making Games</h2>
<p>This week, i arrived early to teach class.  The group has another class before mine in the neighbouring room.  That room was blasting with machine gun fire, swearing, and the stench of gym class.  All of the students were in there playing games.  i wondered where the teacher was.  One of the students told me he was a no-show.  So, of course, that&#8217;s how they decided to spend those three hours &#8211; playing games.</p>
<p>Every time there&#8217;s the briefest pause during my class when i go to help a flailing student, the monitors light up with <b>Team Fortress 2</b> and <b>Quake</b> and online web games.  So a few weeks ago, i dropped this truth-bomb on them:</p>
<blockquote><p>Listen, everyone.  i know you probably go home to Chatham or Barrie or wherever it is you&#8217;re from and brag to your dumb buddies that you play games at school all day, but that&#8217;s not why you&#8217;re here.  You&#8217;re in a game <em>development</em> program, not a game <em>playing</em> program.  You&#8217;re a different breed of person now.  You&#8217;re behind the scenes, not in front of them.  You&#8217;re a creator, not a consumer.  </p>
<p>It&#8217;s like you&#8217;re trying to get a job in a cake factory.  Cakes are fun and enjoyable and people like to eat them, but a factory job is a factory job like any other.  You don&#8217;t get a cake factory job to sit around and eat cakes all day.  Turn the games off.  It&#8217;s time to put some blood, sweat and tears into learning how to <em>make</em> cakes.</p></blockquote>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/cake.jpg"></p>
<p>The cake: with a little actual effort, it&#8217;s no lie.
</p></div>
<h2>Email: The Insurmountable Challenge</h2>
<p>i had to write a mid-term exam for the students.  When i asked him to show me the ropes, the guy who teaches the same course to three other classes imparted some advice: the best thing to do, if i want to make sure i get all of their finished tests, is to pass around a keydrive.  Whenever a student finishes his exam, you pass him the keydrive and he puts his files on it.  i asked why the students couldn&#8217;t just email their files.  He said that when you ask the students to email their completed exam files to you, there are problems.  They type your email address incorrectly, they send you shortcut files as attachments, and they forget to include files.  </p>
<p>And my response?  Forget it.  Not on my watch. </p>
<p>If you can&#8217;t competently email an attachment with your name on it, you&#8217;re not only going to fail my course, but you&#8217;re going to fail <em>life in modern Western civilization</em>.  To make sure that everyone knew the score, i told the students in no uncertain terms that i expected a zip file containing their completed exam files with their first initial and last name emailed to me at the correct address. Then i would go down the class list and start checking off names.  If i didn&#8217;t receive their file, they&#8217;d flat-out fail the test.</p>
<p>There&#8217;s one exception:</p>
<p>No &#8211; i&#8217;m just yanking your chain.  There are NO exceptions.  No email, no mark.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/fail.jpg"></p>
</div>
<p>So we actually took 15-20 minutes out of the class to learn how to right-click a folder, add it to a zip file, and attach it to an email.  It was embarrassing.  i was embarrased.</p>
<p>At break, one of the students piped up to tell me that in another class, their teacher had asked for the same thing &#8211; zip files with students&#8217; names on them.  He provided a sample naming convention &#8211; the teacher&#8217;s name was Gord Smith, so he wrote gsmith.zip on the whiteboard as the example. </p>
<p>And what do you think happened?  Dear friends, his inbox filled up with multiple files called gsmith.zip.</p>
<h2>The Chain of Irresponsibility</h2>
<p>i don&#8217;t actually blame this all on the students.  Somewhere, someone let them down.  If these kids don&#8217;t know how to use email, it&#8217;s not the Colleges&#8217; fault.  That burden is squarely on the high schools. So in conclusion, the problem with Ontario Colleges is not the students, but the high schools.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/highSchoolMusical.jpg"></p>
<p>Problem solved.
</p></div>
<p>EXCEPT that i recently had dinner with a high school English teacher.  She has to administer the grade 10 standardized testing to her class.  In order for her school to score highly on the testing (and it does), she is encouraged by her department head to hand out IEPs &#8211; Individualized Education Plans &#8211; to students left, right and center.  It&#8217;s a loophole.  With an IEP, any student who <em>wants</em> an extra hour on the standardized test, <em>gets</em> an extra hour on the standardized test.  (Make no mistake &#8211; the department head can and should be fired for this.)</p>
<p>So the English teacher is no longer able to teach high school English.  She has to teach to the standardized test.  If you&#8217;ve seen the excellent HBO series <b>The Wire</b>, the same shenanigans went down in that show. The burden of standardized testing put on the high schools is the Ministry&#8217;s fault.</p>
<p>So the problem with Ontario Colleges, conclusively, is the Ministry of Education.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/LeonaDombrowsky.jpg"></p>
<p>Please direct all calls and emails to Minister of Education Leona Dombrowsky.
</p></div>
<p>BUT &#8230; what about the fact that these teachers are at the mercy of the students&#8217; parents?  Everyone in high school gets a passing grade these days, and that&#8217;s largely because if you try to give a student <em>less</em> than a passing grade, you find yourself on the phone having to justify your decision to the kid&#8217;s parent.  i have many friends who are teachers, and the stories they tell about parental interference could curdle your milk.  The CBC recently ran a documentary about these people called <a href="http://www.cbc.ca/documentaries/doczone/2010/hyperparents/index.html">Hyper Parents &#038; Coddled Kids</a>.  You can watch it on their site for free.  It talks about, among other things, parents who call up their kids&#8217; places of employment to negotiate their pay raises. </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/soccerMom.jpg"></p>
<p>The problem is clearly soccer moms.
</p></div>
<h2>In &#8230; Conclusion?</h2>
<p>i&#8217;ve traced the problem with Ontario Colleges through the institution to the students, back to the high schools, up to the Ministry of Education, and back around to the kids&#8217; parents, who demand it be that way in the first place.  These parents, to have teenaged kids, were likely born some time in the 60&#8242;s.  So my penultimate conclusion is that the problem with Ontario Colleges is children of the 60&#8242;s.</p>
<p>Because i&#8217;m a lousy researcher, the trail runs cold there. If you want to take up the torch, i welcome you to it.  Here&#8217;s where we left off:  what the Hell is wrong with children of the 60&#8242;s, and are THEY the reason that nobody in Toronto knows how to make games in Flash?</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2010_02_23/hippies.jpg"></p>
<p>Discuss.
</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%2F02%2F23%2Fwhats-wrong-with-ontario-colleges-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2010%2F02%2F23%2Fwhats-wrong-with-ontario-colleges-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/2010/02/23/whats-wrong-with-ontario-colleges-part-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=2224&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2010/02/23/whats-wrong-with-ontario-colleges-part-2/feed/</wfw:commentRss>
		<slash:comments>42</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: 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_1795495755"
			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_1795495755"
			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>Tutorial: Understanding Classes in AS3 Part 1</title>
		<link>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 13:31:29 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1458</guid>
		<description><![CDATA[If you were there during the early days of the telephone, wouldn&#8217;t you have loved to have provided input? Maybe suggest to Alexander Graham Bell that telephones should issue low-grade electric shocks to teenage girls who talk on the device for more than half an hour? Or suggest a magnetic socket to Edison so that [...]]]></description>
			<content:encoded><![CDATA[<p>If you were there during the early days of the telephone, wouldn&#8217;t you have loved to have provided input?  Maybe suggest to Alexander Graham Bell that telephones should issue low-grade electric shocks to teenage girls who talk on the device for more than half an hour?   Or suggest a magnetic socket to Edison so that we could avoid all those inane &#8220;screw in a lightbulb&#8221; jokes for the rest of our lives?</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_10/slotCars.jpg" alt="Slot Cars">
</p>
<p>Wouldn&#8217;t cars be better if they were on giant slots with computer guidance systems?  You could punch in your destination and fall asleep at the wheel, with no whammies.
</p></div>
<p>If you&#8217;re a Flash game developer, you&#8217;re in at the ground floor of a new service: payment systems for Flash games.  These systems make it easier for game developers to charge money both for their games, and for things <em>within</em> their games.  Here&#8217;s how it works:</p>
<ol>
<li>Player pays real money to buy fake money through one of these systems.
<li>Player spends fake money on virtual stuff.  As a game dev, you can technically charge for whatever you like: level packs, hats, extended versions/director&#8217;s cuts, etc etc.  The sky&#8217;s the limit.
</ol>
<h2>It&#8217;s So Workable, It Just Might Work</h2>
<p>i&#8217;ve been following the microtransaction model for a number of years.  It&#8217;s been crazy popular in places like Korea for a good long time, and it was amusing to see the initial resistance and resentment in North America to the idea.  Panels at the Game Developers Conference were filled with folks nibbling their fingernails and asking &#8220;Will it really work over here?&#8221; and &#8220;Won&#8217;t players be angry with us?&#8221;, with at least a few devs boldly insisting that micropayments are strictly a Southeast Asian cultural anomaly, and the system won&#8217;t work here.  Meanwhile, in the other room at the <a href="http://www.worldsinmotion.biz">Worlds in Motion</a> (virtual worlds) summit, early North American pioneers of those systems were running panels titled &#8220;Can You Believe We&#8217;re Making All This Money?&#8221;  and &#8220;Who Wants a House?  Cuz I&#8217;ve Got a Bunch of Em&#8221;.  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_10/goldToilet.jpg" alt="Gold Toilet">
</p>
<p>No &#8211; for real, guys.  i&#8217;m, like, SO rich.
</p></div>
<p>Of course, virtual currency systems <em>do</em> work here, as evidenced by Microsoft&#8217;s successes with its GamerPoints (AKA &#8220;BillyBucks&#8221;), enabling the creators of <b>Rock Band</b> and others to pocket <a href="http://www.bdgamers.net/2009/03/27/rock-band-earns-1b-in-america.html">obscene amounts of cash</a> in dribs and drabs for virtual whatsits.  Microsoft&#8217;s new fall Xbox 360 seems to exist only to take more money from people in the form of digital doodads for their avatars.  Proprietary systems have been rolled out in numerous other games and portals, including Three Rings (OOO) <b><a href="http://www.puzzlepirates.com">Puzzle Pirates</a></b> with its dual-currency system, and the <a href="http://www.wildgames.com/?dp=ppc&#038;gclid=CMXFme66mZwCFQ8MDQodhhevcw">WildTangent</a> game portal, where players can spend virtual coins to &#8220;rent&#8221; games.  But no one has thought to capitalize on the literal <em>kerfillions</em> of players in the Flash casual games space.  Until now.</p>
<p>There are three companies i&#8217;m aware of who are rolling out virtual payment systems for Flash games: <a href="http://www.mochimedia.com">MochiMedia</a>, <a href="http://www.gamersafe.com">GamerSafe</a> and <a href="http://www.heyzap.com">HeyZap</a>.  Please let me know if there are others.  They all work roughly the same way: pay real money for fake money, and spend fake money for fake things in fake games for real thrills.  One of the key take-aways for me from GDC 07, by the folks running the &#8220;Seriously. My Pants Are Woven From Hundred Dollar Bills&#8221; panel, was this: <b>do whatever it takes to enable your players to give you money.</b></p>
<p>What they meant was that you should provide as many payment methods as possible if you want to take as much money as possible from your players.  This came up in the context of the myriad wild and wooly ways that Europeans pay for things online.  (The French, for example, pay by cheque. True story.)  The speakers advocated pay-by-phone, PayPal, credit cards, debit cards, SMS, and a number of crazy payment methods i&#8217;d never even heard of.  (Pay with your own hair?  What the heck is that about?)</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_10/iHasAMoney.jpg" alt="i has a money">
</p>
</div>
<h2>Untold Entertainment Enters the Fray</h2>
<p>So here we are, poised to release a few games in the no-longer-free-to-play ecosystem. These are early days, and i have no idea which microtransaction system will take the biggest piece of the pie: MochiCoins, GamerSafe or HeyZap.  And frankly, i don&#8217;t care.  Why should i have to choose between them?  Here&#8217;s what i want to do:</p>
<p><b>ME:</b> Hey Player!  Wouldn&#8217;t this game be more fun if your character was wearing <em>SexyPants</em>??</p>
<p><b>BUTTON:</b> <em>Hell yes!</em></p>
<p><b>ME:</b> Great!  A pair of SexyPants will cost you 95 cents.</p>
<p><b>BUTTON:</b> Pay via HeyZap!<br />
<b>BUTTON:</b> Pay with GamerGold!<br />
<b>BUTTON:</b> Pay with MochiCoins!</p>
<p>Sounds good, right?  i&#8217;m not shutting anyone out.  i&#8217;m not preventing the GamerGold folks from buying SexyPants.  i don&#8217;t particularly <em>care</em> which system the player supports &#8211; i just want to take his money.</p>
<h2>However</h2>
<p>The scenario i described above <em>can&#8217;t happen</em> at present, because MochiMedia has written into their terms of service that devs shall not hook multiple transaction systems into their games.  GamerSafe and HeyZap have not made this stipulation.  So i can have a game that either allows MochiCoin payments exclusively, or i can have a game that allows for GamerSafe <em>and</em> HeyZap payments.  And that, in my professional opinion, stinks.</p>
<p>This type of exlusivity is NOT analgous to going into a restaurant and ordering a Coke, and the waitress says &#8220;Is Pepsi okay?&#8221; because the restaurant has an exclusive arrangement with PepsiCo.  No &#8211; this is much more like eating your meal (Coke or Pepsi nothwithstanding), and trying to pay with your VISA card, but the restaurant only takes MasterCard and American Express.  If i walk into a store and they don&#8217;t make it convenient for me to pay with a commonly accepted system, i walk out of that store and i don&#8217;t come back &#8230; but not before i punch someone <em>right in the face</em>, because that&#8217;s how angry it makes me.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_10/punchOut.jpg" alt="Mike Tyson's Punch-Out!">
</p>
<p>(spoiler alert)
</p></div>
<h2>Three Facts About Payment System Exclusivity</h2>
<p><b>MochiMedia&#8217;s exclusivity clause is not good for developers.</b>  We want to lower the barrier to entry for our players, especially since getting people to buy goods in the formerly-free-to-play space is already an uphill battle.</p>
<p><b>MochiMedia&#8217;s exclusivity clause is not good for players.</b>  It&#8217;s forcing players to wait until a clear winner emerges in the Flash virtual goods space.  Why would i sink my money into GamerGold only to find that every single game supports HeyZap or MochiCoins?  i&#8217;d better play it safe and let early adopters figure it out for me. When a leader emerges, i&#8217;ll start spending my money.</p>
<p>No, <b>Mochi&#8217;s exclusivity clause is only good for Mochi.</b>  It&#8217;s a clear attempt to be the only game in town, and to monopolize this service in its infancy.  And we all know what happens with monopolies, don&#8217;t we?  You end up rolling a &#8220;3&#8243; and landing on Park Place with a hotel, and then you get reamed up the pucker.  </p>
<p>Thankfully, it&#8217;s still early enough in the make your voice heard about how this stuff will work.  If you think Mochi should play nicely with others, why not toss them an email here?</p>
<p><a href="mailto:team@mochimedia.com">team@mochimedia.com</a></p>
<p>Or, if you think they&#8217;re making the right decision, give them a call and let them know:</p>
<p>(415) 680-3740</p>
<p>Or, you can just voice your opinions in a comment on this blog and bathe me in sweet, delicious Internet traffic.</p>
<p>For my part, i believe they&#8217;re hurting players and devs right out of the gate in an early, unnecessary bid for domination. Given the choice, i&#8217;d rather support two systems than one &#8211; HeyZap and GamerSafe.  Ideally, i want to support all three, along with any other system that enters the space.  So i&#8217;m making a public appeal to you, Team Mochi, to rethink your policy.  i&#8217;ll even use your first and last names here so that your Google vanity searches will bring you to this article.</p>
<blockquote><p>
On George Garrick! On Jameson Hsu! on Bob Ippolito! On Vixen!<br />
On Comet!  On Cupid!  On Justin Wong! On Eric Boyd!<br />
To the top of the porch! To the top of the stair!<br />
Renounce this proviso, and please grow a pair!
</p></blockquote>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F10%2Fmochicoins-exclusivity%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F10%2Fmochicoins-exclusivity%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/10/mochicoins-exclusivity/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1458&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/08/10/mochicoins-exclusivity/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>One Step Forward, Nine Miles Back</title>
		<link>http://www.untoldentertainment.com/blog/2009/05/22/one-step-forward-nine-miles-back/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/05/22/one-step-forward-nine-miles-back/#comments</comments>
		<pubDate>Fri, 22 May 2009 05:23:15 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Kahoots]]></category>
		<category><![CDATA[Original Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1303</guid>
		<description><![CDATA[If you&#8217;ve been following along at home, you&#8217;ll know that we&#8217;ve been ramping up to iPhone development for the past &#8211; oh, six months or so. We bought an iMacBookUnibodyPowerProMini, or whatever the Hell it&#8217;s called. i dunno. It&#8217;s white. We picked up an iPod Touch. We took an &#8220;iPhone for Flash Developers&#8221; course. We [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been following along at home, you&#8217;ll know that we&#8217;ve been ramping up to iPhone development for the past &#8211; oh, six months or so.  We bought an iMacBookUnibodyPowerProMini, or whatever the Hell it&#8217;s called.  i dunno.  It&#8217;s white.  We picked up an iPod Touch.  We took an &#8220;iPhone for Flash Developers&#8221; course.  We bought into the Apple iPhone Developer Program, and were approved.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_05_21/noCavities.jpg" alt="No Cavities">
</p>
<p>We&#8217;re still waiting for Apple to send our Secret Decoder Ring.
</p></div>
<p>We&#8217;ve been building <b><a href="http://www.untoldentertainment.com/blog/kahoots-designer-diary">Kahoots™</a></b>, our fun crime-themed puzzle game (which we&#8217;ve modeled entirely in clay) to the iPhone/iPod Touch screen spec, which is approximately 478 x <em>effin&#8217; tiny</em>.  i even went out and got one of those Apple tattoos, because that&#8217;s supposed to count for something.  The one thing we haven&#8217;t done is to actually learn Objective-C and deploy something to the iPhone.  Pfft.  A minor detail.</p>
<p>With every step i take towards learning this new (to me) technology, it feels like a wall erupts out of the ground and up into my face:</p>
<ol>
<li>An industry pal told me that the CoreAnimation libraries for the iPhone were very, very slow, and that even the simplest game slowed to a crawl.  i would need to learn to program in openGL-ES.
<li>Terrified by the prospect of having to learn the square bracket-crazy OBJ-C <em>and</em> openGL-ES (whatever THAT was), i was seduced by the apparent C#/javascript-based simplicity of Unity3D, a game engine for creating browser-based and iPhone games
<li>While evaluating Unity, every time i tried to access the software&#8217;s online documentation, their website was down
<li>With Unity&#8217;s site proving unreliable, i resolved again to learn OBJ-C
<li>At a recent iPhone developers meeting here in Toronto, devs told me that OBJ-C is mixed in with C++, so i should learn C++ while i&#8217;m at it
<li>At the same meeting, other devs mentioned that the whole thing is hung on C.  Now i was faced with the prospect of learning Objective-C, C++, C and openGL-ES (whatever THAT was)
</ol>
<p>Fine.  If that was my lot in life, then so be it.  i&#8217;m not a stupid guy.  i can learn things, like words and junk.  And sometimes stuff.  Sometimes i can learn stuff.  And i wanted to program things for the iPhone, so if i had to learn four new languages in my off-hours, i was prepared to do that.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_05_21/midnightOil.jpg" alt="Midnight Oil">
</p>
<p>If &#8220;burning the Midnight Oil&#8221; refers to setting this guy on fire, i&#8217;m all for it.
</p></div>
<h2>Free Smarts from the Stanf&#8217;</h2>
<p>Fellow devs mentioned repeatedly that i could audit a new publicly-available Stanford iPhone development course.  The lectures are all recorded and posted for free on iTunes, and the assignments are all available online.  Great!  And the only pre-requisite was familiarity with Object-Oriented Programming, which i have under my belt  since learning ActionScript 3 this past year.  So off i went.</p>
<p>i watched the first two lectures, and then turned my attention to the assignments.  Assignment #1a had me dragging things around the screen and breathing with my mouth open.  No problem.  i sailed right through it.</p>
<p>Assignment #1b asked me to write a few methods.  Okay &#8230; no biggie.  i know what a method is.  i just didn&#8217;t know the OBJ-C format for writing a method.  i know it starts with a minus sign, and you put the return type in there first, or some junk &#8230;</p>
<p>Assignment #1b asked me to open the documentation.  So i did.  Assignment #1b wanted me to look up the path-finding method for an NSString.  i wanted to know how to write a method first.  So i searched &#8220;method&#8221;.  And then &#8220;method structure.&#8221;  Nothing.  Of course not &#8211; language references don&#8217;t exist to tell you how to <em>build</em> a car, or even to provide pictures of what a finished car looks like.  They&#8217;re just encyclopedias of nuts and bolts you&#8217;ve never heard of.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_05_21/nutsAndBolts.gif" alt="Nuts and Bolts">
</p>
<p>I wouldn&#8217;t exactly say i &#8220;admire&#8221; people who can learn this way. &#8220;Fear them&#8221; is more like it.
</p></div>
<h2>To Google!</h2>
<p>i started looking up OBJ-C code examples.  Everything i found was needlessly complex.  i just wanted the basic structure for a basic method with no arguments and no return types, and Google was all &#8220;but then you can flim-wang your fozzbuster!&#8221;  And i was all &#8220;no thanks, Google!  Just passing through.&#8221;  And then i backed away slowly, trying not to make any sudden movements.</p>
<p>What was my last resort for help, short of asking my 60+-year-old mother for programming advice?</p>
<h2>To IRC!</h2>
<p><b>Internet Relay Chat</b> is a magical place filled with verbally-abusive and generally terrible people who will sometimes help you with your problems if you appeal to their supposedly superior intellect and delusions of grandeur.  i asked some folks in a related channel if they could refer me to a clean code example that would show me how to write a function.  They copy/pasted exactly what i had written in XCode, which was throwing me errors.</p>
<p>One IRC person said that knowledge of OBJ-C was a pre-req for the Stanford course.  i respectfully disagreed: the course syllabus only required a knowledge of Object Oriented Programming.  Then he said something unkind about my 60+-year-old mother.   i won&#8217;t repeat it.</p>
<p>Another person suggested that i go through Apple&#8217;s tutorial.  So off i went to the Apple Developer Site, and found  the <b>Your First iPhone Application</b> tutorial.  Now i was getting somewhere!</p>
<h2>&#8220;First&#8221; Apparently Has a Number of Meanings</h2>
<p>The preamble to the tutorial mentioned that i should have both Objective-C knowledge AND Object-Oriented 
