<?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/"
	>

<channel>
	<title>Ghostmonk Journal &#187; ActionScript 3.0</title>
	<atom:link href="http://journal.ghostmonk.com/category/actionscript3/feed/" rel="self" type="application/rss+xml" />
	<link>http://journal.ghostmonk.com</link>
	<description>Pontificate, Obfuscate, Simplify and Reveal</description>
	<lastBuildDate>Mon, 18 Jan 2010 02:15:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Particle Play With Perlin Noise</title>
		<link>http://journal.ghostmonk.com/design/perlin_noise_particles/</link>
		<comments>http://journal.ghostmonk.com/design/perlin_noise_particles/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 00:00:36 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Interactive]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[experiment]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=202</guid>
		<description><![CDATA[Particle system controlled with perlin data. A control panel allows access to a number of input variables to control the perlin data and particle output.]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
swfobject.embedSWF("/wp-content/themes/default/flash/PerlinNoisePlay.swf", "perlinParticles", "470", "470", "9.0.0", "", {}, { bgcolor:"#000000" });
// --></script></p>
<div id="perlinParticles">
<p>You need Flash 9 to see this awesome Perlin noise particle system!</p></div>
<p>This is an early iteration of the system but I think it&#8217;s at a good point to publish. I just saw Mario Klingerman present at the FITC, and now I have a ton of new ideas to continue exploring and experimenting. If only I had some more free time.</p>
<p>Eventually I plan to release the code, but not until I&#8217;ve explored the possibility a little further.</p>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/design/perlin_noise_particles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>To bracket or not to bracket, making sense of single line conditionals</title>
		<link>http://journal.ghostmonk.com/ether/no-single-line-conditionals/</link>
		<comments>http://journal.ghostmonk.com/ether/no-single-line-conditionals/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 10:47:41 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Code Guidlines]]></category>
		<category><![CDATA[Flex Builder]]></category>
		<category><![CDATA[Lost in the Ether]]></category>
		<category><![CDATA[Writing Code]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=134</guid>
		<description><![CDATA[It&#8217;s taking me about 3 months to finally come up with a strong opinion about single line conditionals, but I now have a firm position: &#8220;In the long run, it&#8217;s a bad idea to drop the brackets and put a conditional or iterator on a single line.&#8221; Essentially sort of thing: public function testResults(score:Number):void { [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s taking me about 3 months to finally come up with a strong opinion about single line conditionals, but I now have a firm position:</p>
<p>&#8220;In the long run, it&#8217;s a bad idea to drop the brackets and put a conditional or iterator on a single line.&#8221;</p>
<p>Essentially sort of thing:</p>
<pre>public function testResults(score:Number):void
{
    if(score &lt; MIN_PASS) sendNotification(YOU_FAIL)
    else sendNotification(YOU_PASS)
}</pre>
<p>Should always be this:</p>
<pre>public function testResults(score:Number):void
{
    if(value &lt; MIN_PASS)
    {
        sendNotification(YOU_FAIL)
    }
    else
    {
        sendNotification(YOU_PASS)
    }
}</pre>
<p>Even when writing in a language that doesn&#8217;t use brackets, i.e. so called pure block languages like Python and Visual Basic, proper formatting should be used rather than single lines. If a language does use brackets, there is no excuse not to use them.</p>
<p>My argument follows this logic:</p>
<blockquote><p>&#8220;Since debugging code is 20 times harder than writing it, by that definition you won&#8217;t be smart enough to find errors in clever code.&#8221;</p></blockquote>
<p><span id="more-134"></span>Single line conditionals are essentially clever ways to save line space and avoid using brackets. As such, they cause an extra level of structured complexity that can be unfamiliar and confusing in a fully realized code based.</p>
<p>There are a lot of coders who might disagree with this, but I have good reason for taking this approach. First of all, one line statements are dense, hard to read and uncommon enough to be awkward.</p>
<p>That&#8217;s not to say they don&#8217;t look great, because they do. But they are a guilty pleasure to write because they belie the complexity of the code. And as a number of seasoned professionals might tell you, readable code always super cedes pretty layout.</p>
<p>Certainly the instance above is a modest example, but think of what happens when these are spread freely in classes or routines a couple hundred lines long. The logic is literally packed into dense statements whose size hides the real length of the program.</p>
<p>The reader tricks themselves into believing that because line-for-line the statement is smaller that it is easier to read. However, in reality the layout removes all visual cues that tell a programmer they are dealing with a control structure.</p>
<p>To state it firmly, the number one objective of any coder should be to reduce complexity, and this does the exact opposite.</p>
<p>I was attracted originally attracted to the single line approach because the extra lines and brackets can be annoying. I even started using it for looping statements*. But I noticed what was happening to my code, and even my own behaviour.</p>
<p>Brackets in this instance act as a natural defense to the proliferation of ugly, unplanned, haphazard code. Single line statements, because the mistakenly feel concise actually encourage bad coding practices.</p>
<p>Conditionals are twisting logic that arrest the normal intellectual flow of a program in the writer&#8217;s mind. They are necessary to be sure, but should be used as sparingly as possible to help simply the code base.</p>
<p>Because single line conditionals are so easy to write, they create an easy road that is often all to tempting when the logic gets tough, and you begin to realize you&#8217;ve been writing too many conditional control structures.</p>
<p>My new favourite quote is Anonymous:</p>
<blockquote><p>Code as if whoever maintains your program is a violent psychopath who knows where you live.</p></blockquote>
<p>*Side Note: Interestingly enough, Flex or Eclipse will not provide code completion for lopping variables not included in brackets.</p>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/ether/no-single-line-conditionals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Olympic Widget for the CBC</title>
		<link>http://journal.ghostmonk.com/actionscript3/olympic-widget/</link>
		<comments>http://journal.ghostmonk.com/actionscript3/olympic-widget/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 23:52:32 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[2008 Beijing Olympics]]></category>
		<category><![CDATA[Widget]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=79</guid>
		<description><![CDATA[With Indusblue, I wrote a program to gather, read, translate and write results for the Olympic on CBC. I used PHP to handle the hardcore data on the server (30,000+ file, 306 events&#8230; etc), but used a ActionScript 3 to create a library so our front end developer could integrate it into the chrome. Check [...]]]></description>
			<content:encoded><![CDATA[<p>With Indusblue, I wrote a program to gather, read, translate and write results for the Olympic on CBC. I used PHP to handle the hardcore data on the server (30,000+ file, 306 events&#8230; etc), but used a ActionScript 3 to create a library so our front end developer could integrate it into the chrome.</p>
<p>Check it out below. You can get your own, and embed it into your site at <a href="http://www.cbc.ca/olympics">CBC.</a> Or just check out the &#8220;get and share&#8221; button on the widget.</p>
<div align="center"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="300" height="250" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="id" value="w489a17e5b5b4b251489ccfa013431723" /><param name="wmode" value="transparent" /><param name="flashvars" value="pid=489ccfa013431723&amp;homepage=home&amp;favCountry=CAN&amp;sportCode=hra0hr_hra0hr_hra0hr_35r" /><param name="allowNetworking" value="all" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://www.cbc.ca/olympics/widget/BeijingWidget.swf" /><embed id="w489a17e5b5b4b251489ccfa013431723" type="application/x-shockwave-flash" width="300" height="250" src="http://www.cbc.ca/olympics/widget/BeijingWidget.swf" allowscriptaccess="always" allownetworking="all" flashvars="pid=489ccfa013431723&amp;homepage=home&amp;favCountry=CAN&amp;sportCode=hra0hr_hra0hr_hra0hr_35r" wmode="transparent"></embed></object></div>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/actionscript3/olympic-widget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MSN Autos &amp; Mitsubishi: Contest Micro Site</title>
		<link>http://journal.ghostmonk.com/actionscript3/msn-autos-and-contest-site/</link>
		<comments>http://journal.ghostmonk.com/actionscript3/msn-autos-and-contest-site/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 04:27:59 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=56</guid>
		<description><![CDATA[About three weeks ago Indusblue launched a new contest site for MSN Autos. This was actually the first project with my new employer, and a doozy at that. I was the principle programmer, and there was no mercy spared as I was even forced to roll back the clock and program this is in the [...]]]></description>
			<content:encoded><![CDATA[<p>About three weeks ago <a href="http://www.indusblue.com">Indusblue</a> launched a new contest site for MSN Autos. This was actually the first project with my new employer, and a doozy at that. I was the principle programmer, and there was no mercy spared as I was even forced to roll back the clock and program this is in the dreaded AS2.</p>
<p>Well, it was baptism by fire, but the whole experience was well worth it.  <a href="http://www.onemethod.com/msnAutos">The site</a> is getting incredible traffic, (projected at over 500,000 before contest end in early March), and it really forced me to stretch my programming muscles to achieve some of the insane effects demanded by the art directors and designers.</p>
<p><a href="http://www.onemethod.com/msnAutos"><img src="http://journal.ghostmonk.com/wp-content/uploads/2008/02/msnautos.jpg" alt="msnautos.jpg" /></a></p>
<p><span id="more-56"></span>Beyond this, I learned a great deal about Object Oriented Design, thanks to a grizzled 10 year vet. In fact, I would have to say that though this project likely aged me 5 years, it did increase my skills as a developer at least two-fold.</p>
<p>Since completing this project, I&#8217;ve already finished and started two more with Indusblue. So I&#8217;d have to say, thus far, the future with this company is looking bright. &#8220;knock on wood!&#8221;</p>
<p>If you have a second, take a run through the site. The goal is to find car parts, and assemble them together so you can win a new Mitsubishi Spyder. It&#8217;s a novel idea, and amazingly, the completion rate of 90 percent is much higher than expected.</p>
<p>The air freshener was a good time, and with some help from a very talented <a href="http://matt-rix.com/">colleague</a>, I was able to include a spray paint application using filters, blends and drawing to a bitmap by tracking the user&#8217;s mouse events.</p>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/actionscript3/msn-autos-and-contest-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Internal Build Error: Flash -&gt; SWC + Flex = BitmapData Insanity</title>
		<link>http://journal.ghostmonk.com/actionscript3/internal-build-error-flash-swc-flex-bitmapdata-insanity/</link>
		<comments>http://journal.ghostmonk.com/actionscript3/internal-build-error-flash-swc-flex-bitmapdata-insanity/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 15:12:57 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flex Builder]]></category>
		<category><![CDATA[Object Oriented Design]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=55</guid>
		<description><![CDATA[I thought I would present a problem I seem to be having consistently in Flex builder, and see if anyone is searching for like issues. For some reason, Flex Builder is not handling Bitmap Data with any sort of consistency when it is included as a class in a SWC exported from Flash. In my [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I would present a problem I seem to be having consistently in Flex builder, and see if anyone is searching for like issues.</p>
<p>For some reason, Flex Builder is not handling Bitmap Data with any sort of consistency when it is included as a class in a SWC exported from Flash. In my opinion, using SWCs to import visual content from the Flash IDE, is for the most part a fluid and excellent work flow. However, every once in a while, in seemingly inexplicable fashion, I get internal build errors.</p>
<p><span id="more-55"></span>The insanity of this is that I can&#8217;t find a pattern. It happens mainly on library objects exported as BitmapData, but not consistently. In one project for instance, I received an internal build error, and was able to whittle the problem down to one tiny BitmapData culprit in a library of about 40 like classes.</p>
<p>By eliminating the offending class, my Flex ActionScript Project compiles&#8230;. if I don&#8217;t, Flex blows up and I get an &#8220;internal build error.&#8221; There are annoying workarounds, like embedding the Bitmap into the ActionScript Class, but this ruins the fluidity of a work-flow between Flash and Flex.</p>
<p>Anyway, the whole thing throws a monkey wrench into the system, and is overwhelmingly irritating. I wish I could post a solution, but I haven&#8217;t been able to nail down the real problem. Also, there seems to be next to nothing concerning this problem on the web.</p>
<p>A colleague mentioned that it might have something to do with the sensitivity of the Eclipse Framework, but god only knows at this point. So, there you have it. I may be the only one wrestling with this issue, and I may never receive a comment on this post, but hopefully someone out there knows what I&#8217;m talking about and can share in my pain.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>This is a simplified version of including a SWC file in a flex project&#8230; you can read the complete document at <a href="http://livedocs.adobe.com/labs/flex3/html/help.html?content=projects_7.html">Adobe&#8217;s LiveDocs</a></p>
<p><strong>Add an SWC file to the library path</strong></p>
<ol>
<li>With a project selected in the Flex Navigator view, select Project &gt; Properties &gt; Flex Build Path.</li>
<li>Click on the Library tab.</li>
<li>Select the <dfn class="term">Add SWC option</dfn></li>
<li>Enter or browse to and select the location of the SWC file, project, or folder. Click OK.The SWC file, library project, or folder is added to the library path.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/actionscript3/internal-build-error-flash-swc-flex-bitmapdata-insanity/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Champagne Valentine Launched !!!</title>
		<link>http://journal.ghostmonk.com/actionscript3/champagne-valentine-launched/</link>
		<comments>http://journal.ghostmonk.com/actionscript3/champagne-valentine-launched/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 14:22:44 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=54</guid>
		<description><![CDATA[Ok, so I&#8217;m totally stoked about launching Champagne Valentine&#8217;s new site. It was about a month&#8217;s worth of spare time development, but I think the end result is class A. Which is important as Champagne Valentine is run by some of the most original and unique people on the web. At this point there are [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://journal.ghostmonk.com/wp-content/uploads/2007/11/champg.jpg" alt="Champagne Valentine" /></p>
<p>Ok, so I&#8217;m totally stoked about launching <a href="http://www.champagnevalentine.com">Champagne Valentine&#8217;s</a> new site. It was about a month&#8217;s worth of spare time development, but I think the end result is class A. Which is important as Champagne Valentine is run by some of the most original and unique people on the web.</p>
<p>At this point there are still some minor bugs to work out, but I&#8217;m really loving what&#8217;s going on here. Everything was programmed from scratch using an Object Oriented Architecture in ActionScript 3.0. I refused to use any components, and most of the content is run on separate XML and HTML files.</p>
<p>In the near future I have plan to add some more functionality and to create a content management system. This will likely be done in PHP, but I may get adventurous and skin it with flash!</p>
<p>So if you have a second, why don&#8217;t you go over and visit the folks at <a href="http://www.champagnevalentine.com">Champagne Valentine</a> and drop them some comments on the site.</p>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/actionscript3/champagne-valentine-launched/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Globally set your tweens man!</title>
		<link>http://journal.ghostmonk.com/actionscript3/globally-set-your-tweens-man/</link>
		<comments>http://journal.ghostmonk.com/actionscript3/globally-set-your-tweens-man/#comments</comments>
		<pubDate>Fri, 23 Nov 2007 17:26:27 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Object Oriented Design]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=49</guid>
		<description><![CDATA[Lately I&#8217;ve been using the Tween class included in the flash ActionScript 3.0 for Flash CS3 library, and I noticed some peculiar behavior with respect to the motion of the Tweens. Often, especially when the program was undergoing points of extreme activity, my tweens would not complete their motions. It was perplexing, and my first [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://journal.ghostmonk.com/?page_id=52" title="Vicious Gnawing Worms"><img src="http://journal.ghostmonk.com/wp-content/uploads/2007/11/viciousworms_thumb.jpg" alt="Vicious Worms" border="0" /></a></p>
<p>Lately I&#8217;ve been using the Tween class included in the flash ActionScript 3.0 for Flash CS3 library, and I noticed some peculiar behavior with respect to the motion of the Tweens. Often, especially when the program was undergoing points of extreme activity, my tweens would not complete their motions. It was perplexing, and my first attempt to fix the bug was to simply to listen for the tween to stop</p>
<pre>(tween.addListener(TweenEvent.MOTION_FINISH,
                   fixPosition)</pre>
<p>, and then to force the asset to its intended end point. This worked on a simple application: <a href="http://selectedwork.ghostmonk.com">Selected Works Site</a>, but it completely failed when I tried it on one with heavy processor activity.</p>
<p>So, after trawling the internet in search of others experiencing the same problem, I believe I found the solution in the comments at a blog called <a href="http://alvinzhang.info/?p=4">Everything Cool</a>. (I aslo found they talked about it on the live docs). Here I&#8217;ll explain what the problem is, how to solve it, and at the same time show you how to use the fl.transitions.Tween class in Flash CS3.</p>
<p><span id="more-49"></span>A quick tutorial on the Tween Class.<br />
First a few points.</p>
<ol>
<li>You always need to import the tween class when using tweens, even when coding on the timeline of an fla</li>
<li>Tweens can be used on almost any kind of object, unlike the transition manager which forces your target to be a MovieClip</li>
<li>The Flex framework has its own class of Tweens that work differently than the ones I&#8217;m describing here. You will need to search the web to figure out how to use these.</li>
</ol>
<p>Ok, so I&#8217;m providing generalized code. The first 2 lines are the import statements, the 3rd is the variable declaration, and the remaining lines will be the actual tween itself, inside of a function that you can call anywhere. A tween automatically starts when it is called.</p>
<pre>
import fl.transitions.Tween;
import fl.transitions.easing.*;

var simpleMovement:Tween

private function startTween(e:MouseEvent):void{
   simpleMovement = new Tween(targetObject, "y",
 		      Strong.easeOut,
 		      targetObject.y,
 		      destinationValue,
 		      1, true);
}</pre>
<p>You will see that the tween called &#8220;simpleMovement&#8221; takes 7 arguments. They are as follows (in order):</p>
<ol>
<li>Object to be tweened</li>
<li>Value to be tweened. (Can be pretty much anything you desire&#8230; even filter values and sound volume)</li>
<li>Easing method(you must import the easing class as supplied)</li>
<li>Start value (supplied in this example as wherever the target object is currently located)</li>
<li>End Value</li>
<li>Length of tween in seconds. (a highly precise number&#8230; can use 0.3482)</li>
<li>Use seconds&#8230; either true or false.</li>
</ol>
<p>Now the problem I was having had to do with the scope of declaring my Tween object. Out of simple cohesion, or laziness I often put my tween declarations and the instantiations in the same function.</p>
<pre>private function startTween(e:MouseEvent):void{
   var simpleMovement:Tween = new Tween(
                      targetObject, "y",
 		      Strong.easeOut,
 		      targetObject.y,
 		      destinationValue,
 		      1, true);
}</pre>
<p>Well therein was my problem. In ActionScript 3.0, the garbage collector runs whenever there is a high number of discarded objects in the cue. (The garbage collector deletes objects with no reference) Because my tween was declared locally, and thus a weak reference, if the application went through an episode of high activity, it was highly likely that it would be discarded by the garbage collector before it could complete.</p>
<p>So eureka man, all I had to do was declare my Tween globally, and thus ensure the garbage collector wouldn&#8217;t mark it for deletion.</p>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/actionscript3/globally-set-your-tweens-man/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Accessors and Mutators</title>
		<link>http://journal.ghostmonk.com/actionscript3/accessors-and-mutators/</link>
		<comments>http://journal.ghostmonk.com/actionscript3/accessors-and-mutators/#comments</comments>
		<pubDate>Sat, 25 Aug 2007 18:39:52 +0000</pubDate>
		<dc:creator>ghostmonk</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Object Oriented Design]]></category>

		<guid isPermaLink="false">http://journal.ghostmonk.com/?p=25</guid>
		<description><![CDATA[**Warning: The following post is about Object Oriented Design, and may be VERY annoying for those who don&#8217;t spend a STUPID amount of time in front of the computer reading the driest material ever known to mankind. ** For those into OOD/OOP, please read on&#8230; This is my attempt to make lucid some very spiraling [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://journal.ghostmonk.com/?page_id=31" title="mutator thumb"><img src="http://journal.ghostmonk.com/wp-content/uploads/2007/08/thumb.jpg" alt="mutator thumb" align="left" hspace="10" vspace="10" /></a><em>**Warning: The following post is about Object Oriented Design, and may be VERY annoying for those who don&#8217;t spend a STUPID amount of time in front of the computer reading the driest material ever known to mankind.  **</em></p>
<p>For those into OOD/OOP,  please read on&#8230; This is my attempt to make lucid some very spiraling concepts. I&#8217;m no expert, I&#8217;m a budding programmer, so as such I may be able to present these ideas with a new perception. At any rate&#8230; getting them down on paper solidifies the methods in my mind. That&#8217;s why I&#8217;m sparse on code, and big on concept. So if I get anything wrong, feel free to send evocative comments, I love the sweet vigor of a verbal joust.</p>
<p>Note: All OOD jargon has been <em>italicized and colored</em> for no good reason at all.</p>
<p><span id="more-25"></span></p>
<p><strong>Summary</strong></p>
<p><em>Getter</em> and <em>setter methods</em> in ActionScript3 differ from regular methods, and resemble <em>characteristics</em> of <em>objects</em>. They do not use regular <em>call statement</em> form, but use <em>dot notation</em> (as a <em>variable</em> would). To use them, you simply call an <em>instantiated object</em> through its <em>variable name</em> and append the <em>getter or setter method</em> minus the <em>function call operator</em>.  It&#8217;s a prety nice and succinct way to modify states of an object.</p>
<p><strong>Defining The Class </strong></p>
<p>Let&#8217;s assume we have a <em>class</em> called <em>&#8220;humanEntity()&#8221;</em>, and in this <em>class</em> we declare a <em>private instance variable</em> called <em>&#8220;lbs&#8221;</em> describing its size. Through the <em>constructor method</em>, we pass a <em>parameter</em> called <em>&#8220;mass&#8221;</em> without an <em>initializer</em> so it is required in the <em>instantiation</em> of the object. In the <em>constructor</em> we assign the value of <em>&#8220;mass&#8221;</em> to the class&#8217;s <em>instance variable &#8220;lbs&#8221;</em> with an <em>assignment operator.</em></p>
<p>Great, now all is set to define our <em>getter </em>and <em>setter </em>methods. (These are also loosely known as <em>mutators, accessors, modifiers and retrievers</em>&#8230;. what a roguish set of descriptors). Good getters and setters have identical method names and are defined not as actions (as a normal method might be) but as <em>characteristics</em>. So instead of using <em>&#8220;changeWeight&#8221;</em> or <em>&#8220;returnWeight&#8221;</em> we would use one variable called <em>&#8220;weight&#8221;</em>. Through these methods, we can change and return the <em>private variable &#8220;lbs&#8221;</em> to a main program that uses the <em>&#8220;humanEntity()&#8221; object</em>. By definition, a getter method explicitly returns a value&#8230; in this case <em>&#8220;lbs&#8221;</em>, and a setter method alters that value. Setters automatically return values, but it is illegal to use a <em>return statement </em>in a setter method.</p>
<p>They are defined like this.</p>
<pre>function get weight():Number{
return lbs;
}</pre>
<pre>function set weight(newLbs:Number):void{
lbs = newLbs;
}</pre>
<p>Note that the method name is exactly the same in both. It is the <em>keywords &#8220;get&#8221; and &#8220;set&#8221;</em> that differentiate them.</p>
<p><strong>Recap</strong></p>
<p>So to recap, we have a class named <em>&#8220;humanEntity()&#8221;</em> with a <em>private instance variable</em> named <em>&#8220;lbs&#8221;.  &#8220;lbs&#8221;</em> is assigned through the <em>constructor method</em> with a <em>required parameter </em>called <em>&#8220;mass&#8221;</em>. There is one <em>getter method</em> and one <em>setter method</em>, both named <em>&#8220;weight&#8221;</em>.</p>
<p><em>Just as an aside, you might want to create another method in this class that would set sensible default values to &#8220;mass&#8221; if someone tried to pass a value  into the object that was too high or low.  You would use a conditional statement of some sort. (I guess that&#8217;s for another discussion).</em></p>
<p><strong>Instantiating the Object</strong></p>
<p>Alright, so now we are using the <em>&#8220;humanEntity()&#8221; class</em> in our application, and we will assign the object to a variable called <em>&#8220;conradVonDuchance&#8221;</em>. Based on everything I have written thus far, it is <em>instantiated</em> into our main class (or whatever class) thusly.</p>
<pre>private var conradVonDuchance:humanEntity
= new humanEntity(200);</pre>
<p><strong>The Crux </strong></p>
<p>Sweet, now for the crux of this entire article.</p>
<p>What makes getter and setter methods so cool, is that you can now both reset and return <em>&#8220;lbs&#8221;</em> (the private variable) by using the method <em>&#8220;weight&#8221;</em>.</p>
<p><strong>Here are two Examples:<br />
</strong>Setting a dynamic style variable to the <em>&#8220;weight&#8221;</em> getter of our object:</p>
<pre>var conradSize:Number = conradVonDuchance.weight;
return conradSize;</pre>
<p>To reset <em>&#8220;weight&#8221;</em>:</p>
<pre>conradVonDuchance.weight = 220;</pre>
<p><strong>Why this is good</strong></p>
<p>At first glance this might seem like a ton of work to accomplish, but I feel it vastly improves your class&#8217;s <em>API</em>.</p>
<p>The idea of setting and returning a characteristic of an object should be as intuitive as possible, and having to call two different methods with different names and  <em>function call operators</em>, to accomplish this is a little cumbersome and ugly. Using the methods <em>&#8220;getWeight()&#8221;</em> and <em>&#8220;setWeight()&#8221;</em>, do makes sense but do not feel streamlined and sexy.</p>
<p>And after all&#8230; isn&#8217;t that what programming is all about&#8230; making the most streamlined sexy block of code possible so you can appeal to the opposite (or same) sex.</p>
]]></content:encoded>
			<wfw:commentRss>http://journal.ghostmonk.com/actionscript3/accessors-and-mutators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
