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

<channel>
	<title>untoldentertainment.com &#187; AS3 Helper Class</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/as3-helper-class/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>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</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>AS3 Helper Class &#8211; RandomBoolean</title>
		<link>http://www.untoldentertainment.com/blog/2008/12/25/as3-helper-class-randomboolean/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/12/25/as3-helper-class-randomboolean/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 17:16:42 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=768</guid>
		<description><![CDATA[We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful. RandomBoolean() 1 2 3 4 5 6 7 8 9 10 public class Helper &#123; /** * Returns Boolean [...]]]></description>
			<content:encoded><![CDATA[<p>We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.</p>
<p><big><strong>RandomBoolean()</strong></big></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Helper
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**
 	* Returns Boolean true or false, randomly.
 	*/</span>		
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> RandomBoolean<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span>  <span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#40;</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;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>		
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><big><strong>Usage</strong></big></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> coinTossIsHeads:<span style="color: #0066CC;">Boolean</span> = Helper.<span style="color: #006600;">RandomBoolean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//coin toss will randomly be heads or tails</span></pre></td></tr></table></div>

<p><big><strong>Comments</strong></big></p>
<p>Sometimes you just need to randomize between true or false, like a coin toss.  Since the code is only one line, it may seem extravagant to make a method out of it, but i find it easy to use this way.</p>
<p>For more AS3 Helper Classses 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%2F2008%2F12%2F25%2Fas3-helper-class-randomboolean%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F12%2F25%2Fas3-helper-class-randomboolean%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/2008/12/25/as3-helper-class-randomboolean/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=768&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/12/25/as3-helper-class-randomboolean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AS3 Helper Class &#8211; RadiansToDegrees and DegreesToRadians</title>
		<link>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-radianstodegrees-and-degreestoradians/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-radianstodegrees-and-degreestoradians/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 03:23:29 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=682</guid>
		<description><![CDATA[We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful. FindAngle() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class [...]]]></description>
			<content:encoded><![CDATA[<p>We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.</p>
<p><big><strong>FindAngle()</strong></big></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
</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> Helper
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**
 	* These companion methods will convert radians to degrees
 	* and degrees to radians.
 	*/</span>		
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> RadiansToDegrees<span style="color: #66cc66;">&#40;</span>radians:<span style="color: #0066CC;">Number</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> radians <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">180</span> <span style="color: #66cc66;">/</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> DegreesToRadians<span style="color: #66cc66;">&#40;</span>degrees:<span style="color: #0066CC;">Number</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> degrees <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">180</span>;
	<span style="color: #66cc66;">&#125;</span>		
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><big><strong>Usage</strong></big></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Find the angle between the mouse cursor and someClip in degrees.</span>
<span style="color: #808080; font-style: italic;">// Refer to our Helper.FindAngle method for that method:</span>
<span style="color: #000000; font-weight: bold;">var</span> mousePoint:Point = <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">mouseX</span>, <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">mouseY</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> somePoint:Point = <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>someClip.<span style="color: #006600;">x</span>, someClip.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> angleInRadians:<span style="color: #0066CC;">Number</span> = Helper.<span style="color: #006600;">FindAngle</span><span style="color: #66cc66;">&#40;</span>mousePoint, somePoint<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// What the Hell is a radian?  Let's convert that nonsense:</span>
<span style="color: #000000; font-weight: bold;">var</span> angleInDegrees:<span style="color: #0066CC;">Number</span> = Helper.<span style="color: #006600;">RadiansToDegrees</span><span style="color: #66cc66;">&#40;</span>angleInRadians<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><big><strong>Comments</strong></big></p>
<p>So here are some more trigonometry-related methods.  If you use the <a href="http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-findangle/">Helper.FindAngle</a> method, you&#8217;ll get the result in radians.  i for one don&#8217;t really know what a radian is, but i do know it&#8217;s one of the beasties that scared me away from taking trig in high school.</p>
<p>Let&#8217;s say you have a top-down tank in your game.  You used Helper.FindAngle to find the angle between the tank turret and the target.  Then you want to rotate the tank turret towards the target.  But turret.rotation needs an angle in degrees, not radians.  Use the RadiansToDegrees method to make everything right with the world.</p>
<p>For more AS3 Helper Classses 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%2F2008%2F12%2F08%2Fas3-helper-class-radianstodegrees-and-degreestoradians%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F12%2F08%2Fas3-helper-class-radianstodegrees-and-degreestoradians%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/2008/12/08/as3-helper-class-radianstodegrees-and-degreestoradians/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=682&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-radianstodegrees-and-degreestoradians/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AS3 Helper Class &#8211; FindAngle</title>
		<link>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-findangle/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-findangle/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 03:00:01 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=674</guid>
		<description><![CDATA[We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful. FindAngle() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Helper [...]]]></description>
			<content:encoded><![CDATA[<p>We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.</p>
<p><big><strong>FindAngle()</strong></big></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="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Helper
<span style="color: #000000;">&#123;</span>
	<span style="color: #3f5fbf;">/**
 	* This method accepts two
 	* Point objects and returns
 	* the angle between them
 	* in radians.
 	*/</span>		
	<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> FindAngle<span style="color: #000000;">&#40;</span>point1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">,</span> point2<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Point</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> dx<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = point2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> point1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> dy<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = point2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> point1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>dx<span style="color: #000066; font-weight: bold;">,</span>dy<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><big><strong>Usage</strong></big></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// Find the angle between the mouse cursor and someClip:</span>
<span style="color: #6699cc; font-weight: bold;">var</span> mousePoint<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Point</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> somePoint<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Point</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>someClip<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> someClip<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = Helper<span style="color: #000066; font-weight: bold;">.</span>FindAngle<span style="color: #000000;">&#40;</span>mousePoint<span style="color: #000066; font-weight: bold;">,</span> somePoint<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p><big><strong>Comments</strong></big></p>
<p>This is a great method that will help you program many different games in Flash.  Applications are wide-reaching.  Picture a billiards game where you have to find the angle between two balls.  Or a space shooter where you need to find the angle between a laser turret and some invading UFOs.</p>
<p>The return value is in radians, which you may not be familiar with if (like me) you stopped taking math courses in the eleventh grade.  If you want to convert the result to degrees, refer to our <a href="http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-radianstodegrees-and-degreestoradians/">Helper.RadiansToDegrees</a> method.</p>
<p><b>IMPORTANT:</b> The order in which you pass the points makes a difference.  You&#8217;ll get a completely different angle by reversing the order of the parameters.</p>
<p>For more AS3 Helper Classses 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%2F2008%2F12%2F08%2Fas3-helper-class-findangle%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F12%2F08%2Fas3-helper-class-findangle%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/2008/12/08/as3-helper-class-findangle/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=674&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-findangle/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AS3 Helper Class &#8211; ShowDisplayList()</title>
		<link>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-showdisplaylist/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-showdisplaylist/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 02:34:16 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=537</guid>
		<description><![CDATA[We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful. Wrap() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [...]]]></description>
			<content:encoded><![CDATA[<p>We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.</p>
<p><big><strong>Wrap()</strong></big></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="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Helper
<span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.*;</span>
<span style="color: #3f5fbf;">/**
 * This method accepts a
 * DisplayObjectContainer instance
 * and lists the children in its
 * display list by depth, name and
 * type.
 */</span>		
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> ShowDisplayList
	<span style="color: #000000;">&#40;</span>theDisplayObjectContainer<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObjectContainer</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> len<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = theDisplayObjectContainer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">numChildren</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span>len<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> theChildObject<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span> =
			theDisplayObjectContainer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
		<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;level &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> i <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot;:&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> theChildObject<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span> <span style="color: #000066; font-weight: bold;">+</span>
			<span style="color: #990000;">&quot; &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> theChildObject<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #3f5fbf;">/* output example:
		level0:spaceship [object MovieClip]
		level1:asteroid0 [object MovieClip]
		level2:asteroid1 [object MovieClip]
	*/</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><big><strong>Usage</strong></big></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="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> theParentClip<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// create a clip that will hold other clips</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// create 20 MovieClips and add them to</span>
	<span style="color: #009900; font-style: italic;">// theParentClip's display list</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> theChildClip<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	theChildClip<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;clip&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> i<span style="color: #000066; font-weight: bold;">;</span>
	theParentClip<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>theChildClip<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
Helper<span style="color: #000066; font-weight: bold;">.</span>ShowDisplayList<span style="color: #000000;">&#40;</span>theParentClip<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// display a report on what's in</span>
	<span style="color: #009900; font-style: italic;">// theParentClip's display list</span></pre></td></tr></table></div>

<p><big><strong>Comments</strong></big></p>
<p>Whenever i teach an intro Flash/Actionscript lesson to someone, and i want to stress the importance of the trace(); command, i say that there&#8217;s usually a difference between what you think Flash is doing, and what <em>Flash</em> thinks Flash is doing.  trace(); is a fast, easy tool to help you find a discrepency between your understanding of what&#8217;s going on, and what&#8217;s actually happening!</p>
<p>ShowDisplayList() helps you quickly and easily visualize what&#8217;s in your DisplayObjectContainer object and at which depth levels.  It&#8217;s very handy, because looks can be deceiving &#8211; when you look at graphics on the screen, you can&#8217;t always tell when there are two or more instances of the same MovieClip sitting on top of each other, or if the clip that didn&#8217;t show up is actually sitting behind another clip, etc etc.  This method sorts that all out for you, and has already saved my bacon on a few projects.</p>
<p>For more AS3 Helper Classses 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%2F2008%2F12%2F08%2Fas3-helper-class-showdisplaylist%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F12%2F08%2Fas3-helper-class-showdisplaylist%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/2008/12/08/as3-helper-class-showdisplaylist/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=537&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/12/08/as3-helper-class-showdisplaylist/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AS3 Helper Class &#8211; Fence()</title>
		<link>http://www.untoldentertainment.com/blog/2008/11/11/as3-helper-class-fence/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/11/11/as3-helper-class-fence/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 14:30:04 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=529</guid>
		<description><![CDATA[We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful. Fence() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [...]]]></description>
			<content:encoded><![CDATA[<p>We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.</p>
<p><big><strong>Fence()</strong></big></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</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> Helper
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #006600;">Point</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #006600;">Rectangle</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * This method confines a Point
	 * within a Rectangle.  Useful for
	 * games where the player and
	 * enemy characters have to stay
	 * within a boundary (ie the screen!).
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> Fence<span style="color: #66cc66;">&#40;</span>point:Point,
				theRect:Rectangle<span style="color: #66cc66;">&#41;</span>:Point
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Make sure the point</span>
		<span style="color: #808080; font-style: italic;">// is horizontally within limits</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>point.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&amp;</span>gt; theRect.<span style="color: #0066CC;">right</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			point.<span style="color: #006600;">x</span> = theRect.<span style="color: #0066CC;">right</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>point.<span style="color: #006600;">x</span> <span style="color: #66cc66;">&amp;</span>lt; theRect.<span style="color: #0066CC;">left</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			point.<span style="color: #006600;">x</span> = theRect.<span style="color: #0066CC;">left</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Make sure the point</span>
		<span style="color: #808080; font-style: italic;">// is vertically within limits</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>point.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&amp;</span>lt; theRect.<span style="color: #006600;">top</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			point.<span style="color: #006600;">y</span> = theRect.<span style="color: #006600;">top</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>point.<span style="color: #006600;">y</span> <span style="color: #66cc66;">&amp;</span>gt; theRect.<span style="color: #006600;">bottom</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			point.<span style="color: #006600;">y</span> = theRect.<span style="color: #006600;">bottom</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">return</span> point;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><big><strong>Usage</strong></big></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Imagine an Asteroids-style game
where you need to confine your spaceship
within the boundaries of a movieClip
instance called &quot;spaceClip&quot;
*/</span>
<span style="color: #000000; font-weight: bold;">var</span> spaceBoundaries:Rectangle = <span style="color: #000000; font-weight: bold;">new</span> Rectangle<span style="color: #66cc66;">&#40;</span>spaceClip.<span style="color: #006600;">x</span>,
					  spaceClip.<span style="color: #006600;">y</span>,
					  spaceClip.<span style="color: #0066CC;">width</span>,
					  spaceClip.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Store the outer space boundaries</span>
<span style="color: #000000; font-weight: bold;">var</span> pos:Point = <span style="color: #000000; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span>spaceship.<span style="color: #006600;">x</span>, spaceship.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Store the ship's co-ordinates</span>
<span style="color: #000000; font-weight: bold;">var</span> xSpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">30</span>;
<span style="color: #000000; font-weight: bold;">var</span> ySpeed:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">50</span>;
	<span style="color: #808080; font-style: italic;">// Set thrusters to &quot;go!&quot;</span>
pos.<span style="color: #006600;">offset</span><span style="color: #66cc66;">&#40;</span>xSpeed, ySpeed<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// add the xSpeed/ySpeed to the ship's current position</span>
pos = Helper.<span style="color: #006600;">Fence</span><span style="color: #66cc66;">&#40;</span>pos, spaceBoundaries<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// make sure the ship doesn't escape outer space!</span>
spaceship.<span style="color: #006600;">x</span> = pos.<span style="color: #006600;">x</span>;
spaceship.<span style="color: #006600;">y</span> = pos.<span style="color: #006600;">y</span>;
	<span style="color: #808080; font-style: italic;">// position the ship on the screen</span></pre></div></div>

<p><big><strong>Comments</strong></big></p>
<p>Keeping game characters and objects from leaving the play field is one of those stock-standard things you do in many, many different games &#8211; from keeping the pigs inside the sty, to keeping the ball inside the tennis court, to keeping the <em>whatever</em> inside the viewable screen limits. We&#8217;re sure to get a lot of mileage from the Fence() method in our future projects.</p>
<p>For more AS3 Helper Classses 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%2F2008%2F11%2F11%2Fas3-helper-class-fence%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F11%2F11%2Fas3-helper-class-fence%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/2008/11/11/as3-helper-class-fence/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=529&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/11/11/as3-helper-class-fence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AS3 Helper Class &#8211; Wrap()</title>
		<link>http://www.untoldentertainment.com/blog/2008/11/09/as3-helper-class-wrap/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/11/09/as3-helper-class-wrap/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 05:54:04 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Helper Class]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=524</guid>
		<description><![CDATA[We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful. Wrap() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [...]]]></description>
			<content:encoded><![CDATA[<p>We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.</p>
<p><big><strong>Wrap()</strong></big></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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Helper
<span style="color: #000000;">&#123;</span>
	<span style="color: #3f5fbf;">/**
	 * This method reigns in a value to keep it between
	 * the supplied minimum and maximum limits.
	 * Useful for wrapping number values 
	 * (particularly paginated interfaces).
	 * Returns the reigned-in result.
	 */</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> Wrap<span style="color: #000000;">&#40;</span>theValue<span style="color: #000066; font-weight: bold;">:*,</span> 
				  minLimit<span style="color: #000066; font-weight: bold;">:*,</span> maxLimit<span style="color: #000066; font-weight: bold;">:*</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:*</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> result<span style="color: #000066; font-weight: bold;">:*</span> = theValue
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>theValue <span style="color: #000066; font-weight: bold;">&lt;</span> minLimit<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			result = maxLimit<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>theValue <span style="color: #000066; font-weight: bold;">&gt;</span> maxLimit<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			result = minLimit<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> result<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Usage</strong></h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> minLimit:uint = <span style="color: #cc66cc;">0</span>;
<span style="color: #000000; font-weight: bold;">var</span> maxLimit:uint = <span style="color: #cc66cc;">20</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> pageIndex:uint = <span style="color: #cc66cc;">20</span>;
&nbsp;
pageIndex = Helper.<span style="color: #006600;">Wrap</span><span style="color: #66cc66;">&#40;</span>++pageIndex, minLimit, maxLimit<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// pageIndex = 0.  Hooray!  That's a Wrap!</span></pre></td></tr></table></div>

<p><big><strong>Comments</strong></big></p>
<p>i find myself doing this kind of thing all the time: checking to see if a number has gone out of bounds, and wrapping it back to the minimum or maximum limit i&#8217;ve set.  i build many, many projects with paginated interfaces, where you click the Back or Next arrow buttons to flip the page.  Clicking Next on the last page sends you to the first page, while clicking Back on the first page wraps you back around to the last page.   The Wrap method helps reduce some code clutter and a bit of typing.</p>
<p>Of course, it&#8217;s not bulletproof.  i cheated a bit by using the * wildcard for the datatypes.  i did this so that the method will tolerate various numeric datatypes without complaining.  Pass a string, a boolean and an array to this method all at once to see it break.</p>
<p>For more AS3 Helper Classses 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%2F2008%2F11%2F09%2Fas3-helper-class-wrap%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F11%2F09%2Fas3-helper-class-wrap%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/2008/11/09/as3-helper-class-wrap/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=524&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/11/09/as3-helper-class-wrap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

