<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>ActionScript 3.0 Design Patterns</title>
	
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Mon, 23 Apr 2012 15:45:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/as3dp/gQKb" /><feedburner:info uri="as3dp/gqkb" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><image><link>http://oreilly.com/catalog/9780596528461/</link><url>http://oreilly.com/catalog/covers/9780596528461_cat.gif</url></image><item>
		<title>ActionScript 3.0 Bitmaps, Clones and ColorTransforms</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/5XBPrSw2T1w/</link>
		<comments>http://www.as3dp.com/2012/04/actionscript-3-0-bitmaps-clones-and-colortransforms/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 22:07:22 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[OOP for Artists]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6582</guid>
		<description><![CDATA[Less is More Ever since I started working with mobile devices with Flash and ActionScript 3.0, I&#8217;ve been looking for faster ways to move my objects through those little windows on iPhones, Androids and Blackberries. Lag time is huge and the action looks geriatric. I&#8217;ve seen some interesting things done with the bitmapped classes, and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/wp-content/uploads/2012/04/ColorBitTransform.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/04/ColorBitTransform.png" alt="" title="ColorBitTransform" width="250" height="145" class="alignleft size-full wp-image-6584" /></a><strong>Less is More</strong></p>
<p>Ever since I started working with mobile devices with Flash and ActionScript 3.0, I&#8217;ve been looking for faster ways to move my objects through those little windows on iPhones, Androids and Blackberries. Lag time is huge and the action looks geriatric. I&#8217;ve seen some interesting things done with the bitmapped classes, and in previous posts we&#8217;ve discussed what may be helpful. However, since I haven&#8217;t used them extensively (or fully), I thought it&#8217;d be a good idea to walkthrough my thinking on the topic. For a while now I&#8217;ve been working on a a Samurai action game, and while I&#8217;ve been making progress, I keep looking for speed tweaks. In general my thoughts regarding bitmapped graphics and Bitmap classes can be summed up in Figure 1. If I create a single Bitmap object using the BitmapData objects (or convert graphic files into BitmapData elements) and then clone the object, I should be able to save processing time.<br />
<div id="attachment_6589" class="wp-caption alignright" style="width: 371px"><a href="http://www.as3dp.com/wp-content/uploads/2012/04/CloneTransforms.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/04/CloneTransforms.png" alt="" title="CloneTransforms" width="361" height="120" class="size-full wp-image-6589" /></a><p class="wp-caption-text">Figure 1: Cloned and Colored</p></div> Of course, I have to assume a good deal. First, I assume that processing a single instance of an object and cloning it is more efficient than creating multiple instances of the same object. I got the idea from how Flash originally used symbols over the Internet. You create one symbol and then re-used it. My idea is pretty much the same. Create once; clone many.</p>
<p>Second, in order for an object to look like more than a single object, I want to change something about the cloned object. With my simple samurai fighter, I can change its color because it is a Bitmap object. So, first we&#8217;ll look at how to create a bitmapped object from scratch using both the Bitmap and BitmapData objects.</p>
<p><strong>Making Bitmapped Objects from Code</strong></p>
<p>I like drawing bitmapped objects or using digital photography. However, in order to best understand what&#8217;s going on, let&#8217;s start from scratch with BitmapData objects. Figure 2 shows the fundamentals of creating a bitmapped data object:<br />
<div id="attachment_6595" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/04/bmapData.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/04/bmapData.png" alt="" title="bmapData" width="500" height="100" class="size-full wp-image-6595" /></a><p class="wp-caption-text">Figure 2: Implementing BitmapData Object</p></div></p>
<p>The ActionScript 3.0 BitmapData object contains a 32-bit integer made up of four 8-bit values (0-255). The first parameter expects a width, the second height—this is the Bitmap shape; a rectangular data matrix. The third parameter is the objects transparency, which has a default value of <strong>true</strong> (<em>it is transparent</em>). By setting the transparency argument  to <strong>false</strong>, you make the object opaque. The 32-bit integer is the final parameter with the first two hexadecimal values being the alpha value with the final six values expressing an RGB color. The following class provides a basic example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6582code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65825"><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" id="p6582code5"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">BitmapData</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Basic <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> burntOrange:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> burntOrangeClone:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bmClone:Bitmap;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Basic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			burntOrange = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">50</span>, <span style="color: #000000; font-weight: bold;">false</span>, 0x00CC5500<span style="color: #66cc66;">&#41;</span>;
			bm=<span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>burntOrange<span style="color: #66cc66;">&#41;</span>;
			bm.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,bm.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s really pretty simple. All you have to do is to define the BitmapData object and then use it as an argument in creating a new Bitmap object. At this junction we&#8217;re just making squares; however, from previous posts that have converted bitmap files into BitmapData objects, we can generate far more complex figures. For now, though, we&#8217;ll stick with the squares. When you test the code, you&#8217;ll get a nice burnt orange square. Try changing the size of the square and experiment with different colors and transparency levels.</p>
<p><strong>A Clone of a BitmapData Object</strong></p>
<p>The next step is to copy the implemented BitmapData object. Fortunately, the BitmapData object contains a clone() method so it&#8217;s also easy to do as you can see in the following example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6582code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65826"><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
</pre></td><td class="code" id="p6582code6"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">BitmapData</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Clone <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> burntOrange:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> burntOrangeClone:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bmClone:Bitmap;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			burntOrange = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">50</span>, <span style="color: #000000; font-weight: bold;">false</span>, 0x00CC5500<span style="color: #66cc66;">&#41;</span>;
			bm=<span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>burntOrange<span style="color: #66cc66;">&#41;</span>;
			bm.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,bm.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			burntOrangeClone=burntOrange.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			bmClone=<span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>burntOrangeClone<span style="color: #66cc66;">&#41;</span>;
			bmClone.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,bmClone.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bmClone<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><div id="attachment_6601" class="wp-caption alignleft" style="width: 208px"><a href="http://www.as3dp.com/wp-content/uploads/2012/04/clones.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/04/clones.png" alt="" title="clones" width="198" height="73" class="size-full wp-image-6601" /></a><p class="wp-caption-text">Original and Cloned Objects</p></div> The two boxes are the results. In looking at the code you can see that the clone is based on the data from the original BitmapData object. However, because a second bitmap object must be created to use the bitmapdata, is there really any savings memory-wise? In other words would we be just as well off just creating a new object from the initial class, Basic as in creating a clone of the object?</p>
<p><strong>Using BitmapData.colorTransform()</strong></p>
<p>The real value in using BitmapData objects, whether clones are quicker to create than new objects, is the colorTransform() method in the BitmapData API. For this post, I am only touching on one aspect of using the method with a ColorTransform object, and that is changing colors by using the ARGB color multipliers. If I create a BitmapData object, and clone it with different colors, I&#8217;ve effectively created a new useful object. If I change the multiplier value (0-1) for the alpha channel, I change the tint of the color. Likewise, if I change the RGB values, I change the colors in those channels.</p>
<p>The <strong>BitmapData.colorTransform()</strong> method has two parameters, <strong>Rectangle</strong> and <strong>ColorTransform</strong>, objects available in the <strong>flash.geom</strong> package. As a result both must be imported. Both the <strong>ColorTransfor</strong>m and <strong>Rectangle</strong> classes may be used on their own, but here they are employed as part of a BitmapData class method with the name <strong>colorTransform()</strong>, which may be a little confusing; so bear with me here. The full format of <strong>BitmapData.colorTransform()</strong> is <strong>BitmapData.colorTransform(Rectangle, ColorTransform)</strong>. The following program will be the starting point:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6582code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65827"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p6582code7"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">BitmapData</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #006600;">Rectangle</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #006600;">ColorTransform</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CloneColorTransform <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> burntOrange:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> burntOrangeClone:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bmClone:Bitmap;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clrTransform:ColorTransform;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rect:Rectangle;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CloneColorTransform<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			burntOrange = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #000000; font-weight: bold;">false</span>,0x00CC5500<span style="color: #66cc66;">&#41;</span>;
			bm = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>burntOrange<span style="color: #66cc66;">&#41;</span>;
			bm.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">100</span>,bm.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			burntOrangeClone = burntOrange.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			clrTransform=<span style="color: #000000; font-weight: bold;">new</span> ColorTransform<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			clrTransform.<span style="color: #006600;">alphaMultiplier</span>=<span style="color: #cc66cc;">0.75</span>;
			<span style="color: #808080; font-style: italic;">//clrTransform.redMultiplier=0.0;</span>
			<span style="color: #808080; font-style: italic;">//clrTransform.greenMultiplier=0.0;</span>
			<span style="color: #808080; font-style: italic;">//clrTransform.blueMultiplier=0.0;</span>
			rect=<span style="color: #000000; font-weight: bold;">new</span> Rectangle<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
			burntOrangeClone.<span style="color: #006600;">colorTransform</span><span style="color: #66cc66;">&#40;</span>rect,clrTransform<span style="color: #66cc66;">&#41;</span>;
			bmClone = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>burntOrangeClone<span style="color: #66cc66;">&#41;</span>;
			bmClone.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">200</span>,bmClone.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bmClone<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.as3dp.com/wp-content/uploads/2012/04/colorTransform.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/04/colorTransform.png" alt="" title="colorTransform" width="234" height="118" class="alignright size-full wp-image-6607" /></a>In the code, notice that the BitmapData and Rectangle instances both have the same 50 x 50 size. That is very important lest you end up with strange results. The first two parameters of the Rectangle object are the <strong>x</strong> and <strong>y</strong> position values. The results (seen at the right) illustrate how to change a cloned object&#8217;s tint based on the original tint. Try changing the alpha multiplier to see how to tint the color darker or lighter. Also note that even thought the clone is identical except for the tint, it is clearly distinguishable as different even though the same BitmapData values were employed in both.</p>
<p><strong>Changing Colors of Images from Bitmap Files</strong></p>
<p>The real payoff for using BitmapData objects is that a colorful object can be changed in different useful ways. In the paired images at the top of this post, each has a different set of colors. By changing color multipliers, you can change individual colors in a single multicolored file.  For example, take the simple Adobe logo. Support Adobe decided to open a branch office on the moon and wanted some changes to indicate that the office was off-earth. They may want to make some subtle changes so that the logo is instantly recognizable but clearly different. Consider the following:<br />
<div id="attachment_6614" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/04/logoClone.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/04/logoClone.png" alt="" title="logoClone" width="500" height="268" class="size-full wp-image-6614" /></a><p class="wp-caption-text">Making a special logo for Adobe&#039;s office on the moon</p></div></p>
<p>The following program was employed in creating all of the different images. Its design allows you to put in any file you want; so use your own if you like.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6582code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65828"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code" id="p6582code8"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">BitmapData</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #006600;">Rectangle</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">geom</span>.<span style="color: #006600;">ColorTransform</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BitMapColorCloneTransform <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileData:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileDataClone:BitmapData;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> file2Bit:Bitmap;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> file2BitClone:Bitmap;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imageLoader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rect:Rectangle;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clrTransform:ColorTransform;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BitMapColorCloneTransform<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;">//url=&quot;samGif.gif&quot;;</span>
			<span style="color: #808080; font-style: italic;">//url=&quot;trans.png&quot;;</span>
			<span style="color: #0066CC;">url</span>=<span style="color: #ff0000;">&quot;logo.png&quot;</span>;
&nbsp;
			imageLoader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			imageLoader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, onComplete<span style="color: #66cc66;">&#41;</span>;
			imageLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onComplete<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			fileData = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">content</span>.<span style="color: #006600;">bitmapData</span>;
			file2Bit = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>fileData<span style="color: #66cc66;">&#41;</span>;
			file2Bit.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>, file2Bit.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">20</span>;
			addChild<span style="color: #66cc66;">&#40;</span>file2Bit<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			fileDataClone=fileData.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			rect=<span style="color: #000000; font-weight: bold;">new</span> Rectangle<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,file2Bit.<span style="color: #0066CC;">width</span>,file2Bit.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
			clrTransform=<span style="color: #000000; font-weight: bold;">new</span> ColorTransform<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//clrTransform.alphaMultiplier=0.75;</span>
			clrTransform.<span style="color: #006600;">redMultiplier</span>=<span style="color: #cc66cc;">0.75</span>;
			<span style="color: #808080; font-style: italic;">//clrTransform.greenMultiplier=1.0;</span>
			clrTransform.<span style="color: #006600;">blueMultiplier</span>=<span style="color: #cc66cc;">0.0</span>;
			fileDataClone.<span style="color: #006600;">colorTransform</span><span style="color: #66cc66;">&#40;</span>rect,clrTransform<span style="color: #66cc66;">&#41;</span>;
			file2BitClone = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>fileDataClone<span style="color: #66cc66;">&#41;</span>;
			file2BitClone.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">270</span>, file2BitClone.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">20</span>;
			addChild<span style="color: #66cc66;">&#40;</span>file2BitClone<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>An important element to note in the program is that the Rectangle object is the same dimensions as the BitmapData object that was loaded from a file. Using the <strong>width</strong> and <strong>height</strong> properties of the BitmapData object for the width and height of the Rectangle object simplified the process.</p>
<p><strong>More on Bitmapped Objects and their Properties</strong></p>
<p>I want to create a program using a design pattern that allows experimentation with the different properties of Bitmap and BitmapData objects and look further into ColorTransform. All of this attention is to see if using clones with different colors can add some speed to the process of object creation and differentiation on mobile devices. Also, I hope to see if we can find a design pattern to accommodate the development time in changing and maintaining larger programs designed for the mobile user. </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F04%2Factionscript-3-0-bitmaps-clones-and-colortransforms%2F&amp;title=ActionScript%203.0%20Bitmaps%2C%20Clones%20and%20ColorTransforms" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=5XBPrSw2T1w:37p7IvcCAWY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=5XBPrSw2T1w:37p7IvcCAWY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=5XBPrSw2T1w:37p7IvcCAWY:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=5XBPrSw2T1w:37p7IvcCAWY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=5XBPrSw2T1w:37p7IvcCAWY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=5XBPrSw2T1w:37p7IvcCAWY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/5XBPrSw2T1w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/04/actionscript-3-0-bitmaps-clones-and-colortransforms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/04/actionscript-3-0-bitmaps-clones-and-colortransforms/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 Bitmap Classes: The Bitmap Factory Method At Work</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/qNRu60Y-QVc/</link>
		<comments>http://www.as3dp.com/2012/04/actionscript-3-0-bitmap-classes-the-bitmap-factory-method-at-work/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 00:53:56 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Factory Method]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6549</guid>
		<description><![CDATA[Building a Bitmap Factory The idea of taking a perfectly good graphic file and taking it apart and putting it back together again has a surprising appeal. You may wonder why one would want to do that? For me, it&#8217;s a chance to explore the Bitmap classes built into ActionScript 3.0 and see if there [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/wp-content/uploads/2012/03/BigPix.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/03/BigPix.png" alt="Bitmap" title="BigPix" width="250" height="144" class="alignleft size-full wp-image-6565" /></a><strong>Building a Bitmap Factory</strong></p>
<p>The idea of taking a perfectly good graphic file and taking it apart and putting it back together again has a surprising appeal. You may wonder why one would want to do that? For me, it&#8217;s a chance to explore the Bitmap classes built into ActionScript 3.0 and see if there is some way to manage the pixel set better and learn as much as possible about the behavior of bitmap data, filters and anything else that might be useful for speeding things up.</p>
<p>More importantly for design pattern development is creating bitmap data objects and moving them around between participants in a pattern and making changes and additions to a program. In creating this pattern, I found it relatively easy to create a concrete product class from an abstract class used in loading and changing standard bitmap files into ActionScript 3.0 bitmap code; however, I had to spend some time calling for a bitmap object through a creator. In other words, getting something up on the stage using a Factory Method design pattern was more than just duplicating what I had done before with the Loader class and bitmap graphic files.</p>
<p>To get started, download the files used and take a look at the outcome by pressing the Play button.<br />
<a href="http://zoe.mwd.hartford.edu/homes/wsanders/bitmap/index.html" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="play button" title="play" width="136" height="48" class="alignleft size-full wp-image-6372" /></a><a href="http://zoe.mwd.hartford.edu/homes/wsanders/bitmap/BitmapSamurai.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="download this sucker" title="download" width="136" height="48" class="alignnone size-full wp-image-6426" /></a></p>
<p>You will find examples from three types of bitmapped graphics: GIF, JPEG and PNG. Identical code was used to break them into bitmap objects from ActionScript 3.0 classes.</p>
<p><strong>The Bitmap Product</strong></p>
<p>The design participants to see first are the abstract and concrete bitmap products (BitMapProduct, BitMapSam). The abstract class contains the properties used in both loading the bitmaps and working with Bitmap and BitmapData properties and methods.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6549code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p654912"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p6549code12"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Product</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">BitmapData</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BitMapProduct <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> imageLoader:Loader;
		protected <span style="color: #000000; font-weight: bold;">var</span> prodData:BitmapData;
		protected <span style="color: #000000; font-weight: bold;">var</span> prodBit:Bitmap;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doBitMap<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Product</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>; 
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BitMapSam <span style="color: #0066CC;">extends</span> BitMapProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> doBitMap<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			imageLoader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			imageLoader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, onComplete<span style="color: #66cc66;">&#41;</span>;
			imageLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onComplete<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			prodData = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">content</span>.<span style="color: #006600;">bitmapData</span>;
			prodBit = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>prodData<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>prodBit<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Note that the Product (BitMapProduct) extends Sprite and the Concrete Product (BitMapSam) extends the Product. As a result the concrete product is a Sprite. </p>
<p><strong>The Bitmap Factory</strong></p>
<p>The Creator (factory) elements consist of the abstract factory (BitMapCreator) and the concrete factory (BitmapSamMaker). The Creator holds a reference to the Product (abstract factory) and the factory method, but that&#8217;s it. The concrete factory returns an instance of the Product using lazy instantiation.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6549code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p654913"><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
</pre></td><td class="code" id="p6549code13"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Creator (Factory)</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BitMapCreator
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> product:BitMapProduct;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:BitMapProduct
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Creator</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BitMapSamMaker <span style="color: #0066CC;">extends</span> BitMapCreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:BitMapProduct
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> BitMapSam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			product.<span style="color: #006600;">doBitMap</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, both Creator classes are pretty simple. Most of the work is done in the concrete product, and in it you can see the way in which ActionScript 3.0 Bitmap objects can be employed.</p>
<p><strong>The Client</strong></p>
<p>It took a while to work out how to bring in the loaded bitmap data through the Client, and once resolved, it turned out to be fairly simple. The Client must pass a string with the name of the bitmap graphic to load. I used three buttons to load an example of each type of graphic to illustrate its functionality with more than a single kind of bitmap graphic.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6549code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p654914"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code" id="p6549code14"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> uifactory.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SamBitClient <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bitMapSam:BitMapCreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bitMapSamNow:BitMapProduct;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnMk:UICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileType:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileNow:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnG:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnP:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnJ:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SamBitClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnMk = <span style="color: #000000; font-weight: bold;">new</span> SpriteButtonMaker  ;
			<span style="color: #808080; font-style: italic;">//(for label,x,y,color,width,height)</span>
			btnG = btnMk.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Bitmap GIF&quot;</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,0x456173,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			btnG.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,chooseSam<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnG<span style="color: #66cc66;">&#41;</span>;
			btnP = btnMk.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Bitmap JPG&quot;</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">35</span>,0x456173,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			btnP.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,chooseSam<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnP<span style="color: #66cc66;">&#41;</span>;
			btnJ = btnMk.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Bitmap PNG&quot;</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">60</span>,0x456173,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			btnJ.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,chooseSam<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnJ<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> chooseSam<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bitMapSamNow<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span>bitMapSamNow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			fileType = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">text</span>;
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>fileType<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Bitmap GIF&quot;</span> :
					fileNow = <span style="color: #ff0000;">&quot;samGif.gif&quot;</span>;
					<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Bitmap JPG&quot;</span> :
					fileNow = <span style="color: #ff0000;">&quot;samJpg.jpg&quot;</span>;
					<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Bitmap PNG&quot;</span> :
					fileNow = <span style="color: #ff0000;">&quot;samPng.png&quot;</span>;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			bitMapSam = <span style="color: #000000; font-weight: bold;">new</span> BitMapSamMaker  ;
			bitMapSamNow = bitMapSam.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bitMapSamNow<span style="color: #66cc66;">&#41;</span>;
			bitMapSamNow.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">130</span>,bitMapSamNow.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">20</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With all of that code, the whole operation in the Client is only the following two lines:</p>
<p>  <code>bitMapSam = new BitMapSamMaker;<br />
        bitMapSamNow = bitMapSam.factoryMethod(fileNow);</code></p>
<p>The rest is UI. The UIFactory is the one used in the <a href="http://www.as3dp.com/2012/03/actionscript-3-0-factory-method-expansion-the-ui-factory/">previous post</a> with minor modifications in color and font. (It is included in the download.)</p>
<p><strong>More On Bitmaps</strong></p>
<p>I&#8217;d like to follow up this post with another on ActionScript 3.0 Bitmap objects. However, instead of creating Bitmaps from files, I&#8217;d like to explore creating them using the BitmapData object and some of the other Bitmap-related classes in ActionScript 3.0. As always, we&#8217;re interested in your feedback.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F04%2Factionscript-3-0-bitmap-classes-the-bitmap-factory-method-at-work%2F&amp;title=ActionScript%203.0%20Bitmap%20Classes%3A%20The%20Bitmap%20Factory%20Method%20At%20Work" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=qNRu60Y-QVc:gPb88J21dls:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=qNRu60Y-QVc:gPb88J21dls:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=qNRu60Y-QVc:gPb88J21dls:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=qNRu60Y-QVc:gPb88J21dls:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=qNRu60Y-QVc:gPb88J21dls:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=qNRu60Y-QVc:gPb88J21dls:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/qNRu60Y-QVc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/04/actionscript-3-0-bitmap-classes-the-bitmap-factory-method-at-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/04/actionscript-3-0-bitmap-classes-the-bitmap-factory-method-at-work/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 Factory Method Expansion: The UI Factory</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/_sjifRn_AiY/</link>
		<comments>http://www.as3dp.com/2012/03/actionscript-3-0-factory-method-expansion-the-ui-factory/#comments</comments>
		<pubDate>Sun, 25 Mar 2012 19:08:40 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Lazy instantiation]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6526</guid>
		<description><![CDATA[Adding New Product to Existing Factory In creating the Button Factory example (see previous post), I wanted to add more to it for a full UI factory. The problem is that the Creator participant returned a Sprite and I wanted to be able to assign text values to instances the factory created. So I needed [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/wp-content/uploads/2012/03/YouEyeFactory.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/03/YouEyeFactory.png" alt="" title="YouEyeFactory" width="250" height="250" class="alignleft size-full wp-image-6547" /></a><strong>Adding New Product to Existing Factory</strong></p>
<p>In creating the Button Factory example (see previous post), I wanted to add more to it for a full UI factory. The problem is that the Creator participant returned a Sprite and I wanted to be able to assign text values to instances the factory created. So I needed to make some changes to the original factory to accommodate these changes—mainly by having the program return a <strong>TextField</strong> object instead of a <strong>Sprite</strong>. In the process I found a distinct advantage of using abstract classes instead of interfaces for a general interface.</p>
<p>The whole point of spending time with a design pattern is so that you can easily make changes and add different components without having to re-write the whole program. The original <a href="http://www.as3dp.com/2012/03/the-button-factory-labels-and-buttons-from-the-same-factory-methods/">Button Factory</a> included a label returned on a Sprite. The text for the label is passed in a parameter by the Client, but the returned button is stuck with the text on the button. You cannot change the text in the button instance. That&#8217;s fine for the button because you can add any label you want on instantiation. However, when your program is passing information displays in text fields, you need access to the text property of the TextField to display updated information.</p>
<p>To get started, you can click the Play button to see an example and the Download button to get all of the files:<br />
<a href="http://www.sandlight.com/DesignPatterns/uifactory/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="play button" title="play" width="136" height="48" class="alignleft size-full wp-image-6372" /></a><a href="http://www.sandlight.com/DesignPatterns/uifactory/uifactory.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="download this sucker" title="download" width="136" height="48" class="alignnone size-full wp-image-6426" /></a></p>
<p>You will find all of the materials from the <a href="http://www.as3dp.com/2012/03/the-button-factory-labels-and-buttons-from-the-same-factory-methods/">Button Factory</a> as well as the new classes. I think you&#8217;ll find this little UI factory to be fairly handy when you need buttons and text fields with some formatting in a hurry. Of course, you can add your own UI components easily without disrupting the program.</p>
<p><strong>Adding a Second Factory Method</strong></p>
<p>In order to return a TextField object, I needed to add a second factory method to the Creator (UICreator) and I had to add a new Product (UITProduct). Then I just added the concrete factory (TextDisplayMaker) and the concrete product (TextDisplay). Because I used an abstract class instead of an interface, I did not have to implement all of the methods, but instead I am able to use one of two different factory methods and can implement one or both, depending what I need for my UI. So first, here&#8217;s the new UICreator with the added factory method:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6526code20'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652620"><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
</pre></td><td class="code" id="p6526code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Creator revised</span>
package uifactory
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UICreator
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> product:UIProduct;
		protected <span style="color: #000000; font-weight: bold;">var</span> tProduct:UITProduct;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Second factory method added</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> textFactoryMethod<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice that the added factory method is identical to the first one except that it returns a TextField instead of a Sprite. Note also that the revised code imports TextField.</p>
<p>Next, I had to create a new Product—one that results in a useable TextField object and not a Sprite:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6526code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652621"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p6526code21"><pre class="actionscript" style="font-family:monospace;">package uifactory
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
	<span style="color: #000000; font-weight: bold;">class</span> UITProduct
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> ui:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUI<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Again, you can see that the new abstract Product (<strong>UITProduct</strong>) is exactly the same as the <strong>UIProduct</strong> except that it imports a TextField and returns one instead of a Sprite.</p>
<p>Next, in looking at the factory for creating a concrete <strong>UITProduct</strong>, the <strong>TextDisplayMaker</strong> implements the same <strong>UICreator</strong> as one of the button products from the previous post, but it must use the new factory method, <strong>textFactoryMethod</strong> to return a usable text field. Otherwise it&#8217;s the same, and the lazy instantiation creates a new <strong>tProduct</strong> instead of a <strong>product</strong>, which is the property of the <strong>UIProduct</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6526code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652622"><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" id="p6526code22"><pre class="actionscript" style="font-family:monospace;">package uifactory
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TextDisplayMaker <span style="color: #0066CC;">extends</span> UICreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> textFactoryMethod<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> tProduct<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				tProduct=<span style="color: #000000; font-weight: bold;">new</span> TextDisplay<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> tProduct.<span style="color: #006600;">getUI</span><span style="color: #66cc66;">&#40;</span>msg,hor,v,col,w,h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The text product itself has some twists that provide a flexible TextField object—one that can be either dynamic or input. It also has variable text formatting, but the message text is implemented by the Client. It implements the UITProduct.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6526code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652623"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code" id="p6526code23"><pre class="actionscript" style="font-family:monospace;">package uifactory
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TextDisplay <span style="color: #0066CC;">extends</span> UITProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getUI<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,<span style="color: #0066CC;">align</span>:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
		<span style="color: #66cc66;">&#123;</span>
			ui=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Verdana&quot;</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0x000000;
			txtFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">11</span>;
			txtFormat.<span style="color: #0066CC;">blockIndent</span>=<span style="color: #cc66cc;">5</span>;
&nbsp;
			ui.<span style="color: #006600;">defaultTextFormat</span> = txtFormat;
&nbsp;
			<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;DYNAMIC&quot;</span>:
				ui.<span style="color: #0066CC;">type</span>=TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;INPUT&quot;</span>:
				ui.<span style="color: #0066CC;">type</span>=TextFieldType.<span style="color: #006600;">INPUT</span>;
				<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">//Type will be INPUT or DYNAMIC</span>
			<span style="color: #808080; font-style: italic;">//ui.type = TextFieldType.msg;</span>
&nbsp;
			ui.<span style="color: #0066CC;">selectable</span> = <span style="color: #000000; font-weight: bold;">false</span>;
			ui.<span style="color: #0066CC;">border</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			ui.<span style="color: #0066CC;">multiline</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			ui.<span style="color: #0066CC;">borderColor</span>=0xbbbbbb;
			ui.<span style="color: #0066CC;">background</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			ui.<span style="color: #0066CC;">wordWrap</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			ui.<span style="color: #0066CC;">backgroundColor</span>=col;
			ui.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">align</span>;
			ui.<span style="color: #006600;">y</span> = v;
			ui.<span style="color: #0066CC;">width</span> = w;
			ui.<span style="color: #0066CC;">height</span> = h;
			<span style="color: #808080; font-style: italic;">//ui.text=msg;</span>
			<span style="color: #b1b100;">return</span> ui;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The concrete text product has a good deal of flexibility, allowing for either INPUT or DYNAMIC text fields. In order to show how it works with the earlier version of objects in the same UIFactory, the following Client uses a button from the original Button Factory (see previous post) and the new text factory and products from this current post.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6526code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652624"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code" id="p6526code24"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> uifactory.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">FocusEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UIClient <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnMk:UICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> display:UICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> inDisplay:UICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> displayNow:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span>  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> inputNow:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span>  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> passOn:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> UIClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnMk=<span style="color: #000000; font-weight: bold;">new</span> SpriteButtonMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//(for label,x,y,color,width,height)</span>
			btn = btnMk.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Click 4 Text&quot;</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">40</span>,0x53933f,<span style="color: #cc66cc;">120</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, passText<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Input text</span>
			inDisplay=<span style="color: #000000; font-weight: bold;">new</span> TextDisplayMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			inputNow = inDisplay.<span style="color: #006600;">textFactoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;INPUT&quot;</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">100</span>,0xFFB03B,<span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">48</span><span style="color: #66cc66;">&#41;</span>;
			inputNow.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FocusEvent.<span style="color: #006600;">FOCUS_IN</span>,eraser<span style="color: #66cc66;">&#41;</span>;
			inputNow.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Enter your text here:&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>inputNow<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> eraser<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:FocusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> passText<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			passOn = inputNow.<span style="color: #0066CC;">text</span>;
			display=<span style="color: #000000; font-weight: bold;">new</span> TextDisplayMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			displayNow = display.<span style="color: #006600;">textFactoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;DYNAMIC&quot;</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">160</span>,0xCEF09D,<span style="color: #cc66cc;">250</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			displayNow.<span style="color: #0066CC;">text</span> = passOn;
			addChild<span style="color: #66cc66;">&#40;</span>displayNow<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The purpose of this post is to illustrate how simple it is to make changes within a design pattern without disrupting previous work you have done. Also, I hope that some of you will extend the UI factory even further and share them with us. Happy Programming!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F03%2Factionscript-3-0-factory-method-expansion-the-ui-factory%2F&amp;title=ActionScript%203.0%20Factory%20Method%20Expansion%3A%20The%20UI%20Factory" id="wpa2a_6"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_sjifRn_AiY:i-OLbGJ8w3k:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_sjifRn_AiY:i-OLbGJ8w3k:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=_sjifRn_AiY:i-OLbGJ8w3k:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_sjifRn_AiY:i-OLbGJ8w3k:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_sjifRn_AiY:i-OLbGJ8w3k:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=_sjifRn_AiY:i-OLbGJ8w3k:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/_sjifRn_AiY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/03/actionscript-3-0-factory-method-expansion-the-ui-factory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/03/actionscript-3-0-factory-method-expansion-the-ui-factory/</feedburner:origLink></item>
		<item>
		<title>The Button Factory: Labels and Buttons from the Same Factory Methods</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/fsuyminEPWo/</link>
		<comments>http://www.as3dp.com/2012/03/the-button-factory-labels-and-buttons-from-the-same-factory-methods/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 20:05:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Lazy Initialization]]></category>
		<category><![CDATA[Lazy instantiation]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6499</guid>
		<description><![CDATA[Beginning of a UI Factory The other day I was looking at an ActionScript 3.0 program, and the developer used a &#8216;UI Factory.&#8217; I looked at it, and while the program did indeed get UIs from the UIFactory class, it wasn&#8217;t a design pattern. Instead the UI Factory was just a single class with a [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6524" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/02/buttonFactory.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/02/buttonFactory.png" alt="" title="buttonFactory" width="250" height="185" class="size-full wp-image-6524" /></a><p class="wp-caption-text">A UI Factory for Buttons and Labels</p></div><strong>Beginning of a UI Factory</strong></p>
<p>The other day I was looking at an ActionScript 3.0 program, and the developer used a &#8216;UI Factory.&#8217; I looked at it, and while the program did indeed get UIs from the <strong>UIFactory</strong> class, it wasn&#8217;t a design pattern. Instead the UI Factory was just a single class with a bunch of methods for building different parts of a UI. Included in the factory, though, was a call to another class that also was part of the overall UI.</p>
<p>Basically, the call from one factory product to another made the requesting product a client.  So, I thought, why not create a factory method design pattern where one part of a product called another to complete the product? The design pattern would be a straightforward Factory Method pattern, and the call from within the pattern would follow the same call through the Creator as a call from a general Client class would be. In this way, I would be able to use the same interfaces (abstract classes) for calls within the pattern as well as from outside the pattern by an external Client class.</p>
<p><strong>A Button and Label Factory</strong></p>
<p>The basic process is the same used in the post describing <a href="http://www.as3dp.com/2010/05/actionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern/" target="_blank">Lazy Initialization</a> on this blog. The difference is that one product uses another product in the factory process. To get started, take a look at Figure 1 that shows how an external client and the UIButton both make requests through the UICreator. The external client can get either a button or just the label, but when a button is requested, it then requests a label to go with it.<br />
<div id="attachment_6532" class="wp-caption alignnone" style="width: 509px"><a href="http://www.as3dp.com/wp-content/uploads/2012/03/UIFact.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/03/UIFact.png" alt="uifact" title="UIFact" width="499" height="329" class="size-full wp-image-6532" /></a><p class="wp-caption-text">Figure 1: Request by button and external client for a label</p></div></p>
<p>The button uses a Sprite object for the base of a graphic gradient shape using a round rectangle. The label is put into a Sprite as well and then added to the button. The Sprite is inherited from the same UIProduct class.</p>
<p><strong>Creators and Products</strong></p>
<p>Just like any other Factory Method, the pattern uses abstract classes for both the UICreator and the UIProduct. The following listing shows that both abstract classes have methods that use the same set of parameters.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6499code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p649929"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p6499code29"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Creator</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UICreator
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> product:UIProduct;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Product</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
	<span style="color: #000000; font-weight: bold;">class</span> UIProduct
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> ui:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUI<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The &#8220;abstracting&#8221; of the classes provides a good deal of flexibility and bindings are loose. The concrete implementations are only two of a larger planned UI set. However, it&#8217;s not difficult to add any concrete UI you want.</p>
<p><strong>The Concrete Label Maker and Product</strong></p>
<p>First, take a look at the concrete label and label creator. The creator uses lazy initialization, and the concrete label product uses <strong>TextField</strong>  and <strong>TextFormat</strong> objects for the label product.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6499code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p649930"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p6499code30"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Concrete Label Creator</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpriteLabelMaker <span style="color: #0066CC;">extends</span> UICreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> SpriteLabel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getUI</span><span style="color: #66cc66;">&#40;</span>msg,hor,v,col,w,h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Label Product</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpriteLabel <span style="color: #0066CC;">extends</span> UIProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLabel:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getUI<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,<span style="color: #0066CC;">align</span>:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			ui=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0xffffff;
			txtLabel = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtLabel.<span style="color: #006600;">defaultTextFormat</span> = txtFormat;
			txtLabel.<span style="color: #0066CC;">text</span> = msg;
			txtLabel.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			txtLabel.<span style="color: #0066CC;">selectable</span> = <span style="color: #000000; font-weight: bold;">false</span>;
			txtLabel.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">align</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
			txtLabel.<span style="color: #006600;">y</span> = h<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">4</span>;
			ui.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtLabel<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> ui;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>By including both TextField and TextFormat objects, the look and feel of the button labels have a good deal of flexibility. Currently set to create only white buttons, the color parameter (<strong>col</strong>) could be used to add more appropriate colors if needed.</p>
<p><strong>The Concrete Button Maker and Product</strong></p>
<p>Like the label, the concrete implementations of the creator and product abstract classes use the same set of method parameters.  However, the button product acts as a client calling for a label product through the UICreator interface (abstract class). Both return a Sprite object, but when the button object appears, it always has a label with it. So a request for a button object is also a request for a label object. Labels, however, may be requested by a client without having to get a button object as well.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6499code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p649931"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p6499code31"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Button concrete Creator</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpriteButtonMaker <span style="color: #0066CC;">extends</span> UICreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> SpriteButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getUI</span><span style="color: #66cc66;">&#40;</span>msg,hor,v,col,w,h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Button concrete Product</span>
package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">GradientType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SpreadMethod</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpriteButton <span style="color: #0066CC;">extends</span> UIProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lbl:UICreator;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getUI<span style="color: #66cc66;">&#40;</span>msg:<span style="color: #0066CC;">String</span>,hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			ui= <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> fill:<span style="color: #0066CC;">String</span> = GradientType.<span style="color: #006600;">LINEAR</span>;
			<span style="color: #000000; font-weight: bold;">var</span> spread:<span style="color: #0066CC;">String</span> = SpreadMethod.<span style="color: #006600;">PAD</span>;
			ui.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginGradientFill</span><span style="color: #66cc66;">&#40;</span>fill, <span style="color: #66cc66;">&#91;</span>col, 0x70c656<span style="color: #66cc66;">&#93;</span>,<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">127</span>,<span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #000000; font-weight: bold;">null</span>,spread,<span style="color: #ff0000;">&quot;rgb&quot;</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Button shape (x,y,width, height, corner)</span>
			ui.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,w, h, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
			ui.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			ui.<span style="color: #006600;">x</span>=hor, ui.<span style="color: #006600;">y</span>=v;
			ui.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			ui.<span style="color: #0066CC;">useHandCursor</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			lbl=<span style="color: #000000; font-weight: bold;">new</span> SpriteLabelMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			ui.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>lbl.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span>msg,hor,v,col,w,h<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> ui;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Both the graphic portion and label portion of the button use the <strong>ui</strong> property inherited and implemented from the <strong>UIProperty</strong> class. In that respect the button product is recursively formed. It is built by embedding the label Sprite into the button Sprite.</p>
<p>Finally, here&#8217;s an example of a Client. It creates two different buttons based on the arguments.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6499code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p649932"><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" id="p6499code32"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UIClient <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnMk:UICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lbl:UICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn2:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> UIClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnMk=<span style="color: #000000; font-weight: bold;">new</span> SpriteButtonMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//(for label,x,y,color,width,height)</span>
			btn=btnMk.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Click Here&quot;</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">40</span>,0x53933f,<span style="color: #cc66cc;">120</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
			btn2=btnMk.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Magic&quot;</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">110</span>,0x0000cc,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><div id="attachment_6540" class="wp-caption alignleft" style="width: 150px"><a href="http://www.as3dp.com/wp-content/uploads/2012/03/BtnExs.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/03/BtnExs.png" alt="" title="BtnExs" width="140" height="114" class="size-full wp-image-6540" /></a><p class="wp-caption-text">Figure 2: Flexibility in Button Factory</p></div> The buttons created have some range, and by adding more parameters, you can extend that range to a wider variety of gradient colors and different fonts and font sizes. However, for now, you can see how the buttons can be created using a simple Factory Method. Here&#8217;s a great opportunity to show off some different examples and tweaks to make the pattern more flexible for your needs.</p>
<p>One important feature I tested with this little design pattern was whether the buttons could indeed be used as buttons. Most importantly, I wanted to make sure that they could handle event listeners that in turn could handle events. Fortunately, they worked like any other button, and all that was necessary was to make sure that the <strong>buttonMode</strong> and <strong>useHandCursor</strong> properties were set to true. I have a feeling that this is a little application that I&#8217;ll be re-using a lot and tweaking a lot more!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F03%2Fthe-button-factory-labels-and-buttons-from-the-same-factory-methods%2F&amp;title=The%20Button%20Factory%3A%20Labels%20and%20Buttons%20from%20the%20Same%20Factory%20Methods" id="wpa2a_8"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=fsuyminEPWo:fXKaXn3tOLo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=fsuyminEPWo:fXKaXn3tOLo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=fsuyminEPWo:fXKaXn3tOLo:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=fsuyminEPWo:fXKaXn3tOLo:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=fsuyminEPWo:fXKaXn3tOLo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=fsuyminEPWo:fXKaXn3tOLo:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/fsuyminEPWo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/03/the-button-factory-labels-and-buttons-from-the-same-factory-methods/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/03/the-button-factory-labels-and-buttons-from-the-same-factory-methods/</feedburner:origLink></item>
		<item>
		<title>Recursion: An ActionScript 3.0 Problem or Problem Solver?</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/_jE77Bry4pA/</link>
		<comments>http://www.as3dp.com/2012/02/recursion-an-actionscript-3-0-problem-or-problem-solver/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 14:23:28 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Recursion]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6496</guid>
		<description><![CDATA[Why Recursion? (Again) In previous posts we&#8217;ve considered recursion, and while not rejecting recursion out of hand, it&#8217;s been roughed up a bit, especially by smart reader comments. (You can see posts on recursion here.) For those of you who may be new to the recursive concept, it refers to a method that calls itself. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/wp-content/uploads/2012/02/recursionside.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/02/recursionside.png" alt="recursion image" title="recursionside" width="250" height="122" class="alignleft size-full wp-image-6497" /></a><strong>Why Recursion? (Again)</strong></p>
<p>In previous posts we&#8217;ve considered recursion, and while not rejecting recursion out of hand, it&#8217;s been roughed up a bit, especially by smart reader comments. (You can see posts on recursion <a href="http://www.as3dp.com/category/recursion/" title="Recursion category" target="_blank"><strong>here</strong></a>.) For those of you who may be new to the recursive concept, it refers to a <strong>method that calls itself</strong>.</p>
<p>In general, ActionScript 3.0 programmers tend to avoid recursion because recursive functions fill up the stack faster than a loop doing essentially the same thing, and loops are generally faster than recursive functions. Now that we really need to pay attention to speed for mobile development, should we waste time thinking about recursion when we need to be thinking about speed and multicore operations? I think we need to understand recursion, and use it when needed. More importantly, it is a useful tool for thinking about programming.</p>
<p><strong>Basic ActionScript 3.0 Recursive Functions: Direct and Indirect</strong></p>
<p>As a simple refresher, the following illustrates two examples of recursion in programming. A direct recursive function is one where the function calls itself and indirect recursion occurs where a function calls another function and then calls the calling function.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6496code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p649634"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code" id="p6496code34"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Recursion <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> cur:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> counter:uint = <span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Recursion<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			direct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			indirect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Direct recursion occurs when a method calls itself</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> direct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>counter<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				cur = <span style="color: #ff0000;">&quot;Direct recursion&quot;</span>;
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>cur+counter<span style="color: #66cc66;">&#41;</span>;
				counter++;
				<span style="color: #808080; font-style: italic;">//Self-calling method</span>
				direct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Indirect recursion occurs when a method calls a method </span>
		<span style="color: #808080; font-style: italic;">//that calls the calling method</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> indirect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>counter <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				cur = <span style="color: #ff0000;">&quot;Indirect recursion&quot;</span>;
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>cur+counter<span style="color: #66cc66;">&#41;</span>;
				counter++;
				<span style="color: #808080; font-style: italic;">//Calls method that calls this method</span>
				indirectly<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> indirectly<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			indirect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The conditional statements inside the recursive functions keep the functions from a stack overflow error. It is identical to avoiding an infinite loop by making sure that a loop termination condition is placed in the loop statement.</p>
<p><strong>Gödel, Escher, Bach: An Eternal Golden Braid (A metaphorical fugue on minds and machines in the spirit of Lewis Carroll)</strong></p>
<p>Wanting to dust the cobwebs out of my head <em>and</em> get away from linear thinking, I inquired for a good starting point for <em>pure mathematics</em>. I was guided to <em>Gödel, Escher, Bach</em> (GEB) by Douglas R. Hofstadter. Published in 1979, it won the nonfiction Pulitzer Prize in 1980 and has been a perennial favorite of programmers, mathematicians, artists, musicians and others who want to give their minds a workout ever since. (See the <a href="http://en.wikipedia.org/wiki/Gödel,_Escher,_Bach" target="_blank"><strong>WikiPedia</strong></a> entry for a quick overview. Also, MIT offers <a href="http://ocw.mit.edu/high-school/courses/godel-escher-bach/" target="_blank">GES: A Mental Space Odyssey</a> as part of their Open Courseware program.) Of particular interest for this post is <em>Chapter V: Recursive Structures and Processes (Canon by Intervallic Augmentation)</em>. For Hofstadter, recursion is a general concept and can include such things as movies within movies, Russian stacking dolls and paintings inside paintings. (The image at the beginning of this post is borrowed from Escher&#8217;s famous illustration of two hands drawing one another—a recursive illustration.)</p>
<p>One type of recursion occurs in graphics where the pixels that define a shape (context) are defined by the context. For example, consider Figure 1. The dots that make up the circle shape are also positioned relative to the circle they define. <div id="attachment_6509" class="wp-caption alignleft" style="width: 210px"><a href="http://www.as3dp.com/wp-content/uploads/2012/02/circle.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/02/circle.png" alt="recursive" title="circle" width="200" height="200" class="size-full wp-image-6509" /></a><p class="wp-caption-text">Figure 1: Dots&#039; positions defined by the circle they define.</p></div> The gray arrow points to a dot in the upper left of the circle. The location &#8220;upper left&#8221; is relative to the circle that the dots define.  The recursive nature of the position is that the defining building block (dot) is relative to the structure it defines (circle).</p>
<p>The dot (or pixel) is part of the images we seek to place on the stage, and their placement makes up the image that defines their position. You may be thinking,</p>
<blockquote><p>So what?</p></blockquote>
<p>How can I use this knowledge to become a better programmer or make my programs better? You can do it in the same way that Gamma and his associates looked a programming in a different way and came up with <strong>Design Patterns</strong>. Instead of asking, </p>
<blockquote><p>What will make my program run faster and how can I optimize its performance?</p></blockquote>
<p>They asked,</p>
<blockquote><p>What kind of program structures do we need when programs change so that the other parts of the program will be unaffected by the change?</p></blockquote>
<p>Perhaps instead of asking <em>&#8220;How do I make a circle by lighting up pixels?&#8221;</em>, we may ask, <em>&#8220;How do circles position pixels?&#8221;</em> They&#8217;re the same question, but they generate different ways of thinking about creating graphics in ActionScript 3.0. Of course, AS3 has built-in Graphics methods for creating circles; so you may not think about creating circles. (See <a href="http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture6/circle.html">Circle-Drawing Algorithms</a> for an interesting discussion of circle creation.) However, in terms of problem-solving, you will find recursion again and again. Recursion is in the world, and linear thinking is not the only way to think about recursion. In some respects linear thinking distorts reality because it distorts recursion by unwinding it to fit into a linear plan.</p>
<p><strong>Design Patterns as Cheat Sheets for Critical Thinking</strong></p>
<p>Like recursion, a lot of the principles in the GoF book are nonlinear. For example the principle, <em>Program to the interface and not the implementation</em>, is a good example of nonlinear thinking. The fact that the Gang of Four took the time to work out the set of patterns they did for the different kinds of programming structures is very valuable because that means <em>I don&#8217;t have to do it for myself</em>.  For example, if I have a bunch of algorithms that I know are going to change and interact with the rest of the program, I don&#8217;t have to figure out a flexible way to incorporate them into my program. I can snatch up a <strong>Strategy</strong> pattern and it has the structure laid out for me. In the same way, the concept of recursion is a structure that can be used to help solve a problem. I may end up using a loop, but I&#8217;m more likely to first think, &#8220;Is this a recursive structure?&#8221; If so, then I can add a recursive method (as I might in a <strong>Chain of Responsibility</strong> structure) and then eventually solve the problem using a switch statement inside a loop. In that respect, recursive methods are cheat sheets as well. We don&#8217;t have to reinvent new structures or rely only on linear ones when we sit down to program.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F02%2Frecursion-an-actionscript-3-0-problem-or-problem-solver%2F&amp;title=Recursion%3A%20An%20ActionScript%203.0%20Problem%20or%20Problem%20Solver%3F" id="wpa2a_10"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_jE77Bry4pA:P4KKywdbp-8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_jE77Bry4pA:P4KKywdbp-8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=_jE77Bry4pA:P4KKywdbp-8:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_jE77Bry4pA:P4KKywdbp-8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=_jE77Bry4pA:P4KKywdbp-8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=_jE77Bry4pA:P4KKywdbp-8:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/_jE77Bry4pA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/02/recursion-an-actionscript-3-0-problem-or-problem-solver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/02/recursion-an-actionscript-3-0-problem-or-problem-solver/</feedburner:origLink></item>
		<item>
		<title>The Grid State: Movement in Mobile</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/4FQG6pOhSWs/</link>
		<comments>http://www.as3dp.com/2012/02/the-grid-state-movement-in-mobile/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 23:05:50 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Getters and Setters]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6333</guid>
		<description><![CDATA[Matrix Work As our desktop screens soar to new heights in size, we find ourselves coping with smaller platforms for mobile viewing. To address this problem, developers simply move from one cell, grid or section to another. Each new cell becomes the full screen. For example, a 3 x 3 matrix has 9 cells (grid [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/12/GridStateRel.png" alt="GridStateRel" title="GridStateRel" width="250" height="250" class="alignleft size-full wp-image-6332" /><strong>Matrix Work</strong></p>
<p>As our desktop screens soar to new heights in size, we find ourselves coping with smaller platforms for mobile viewing.  To address this problem, developers simply move from one cell, grid or section to another. Each new cell becomes the full screen. For example, a 3 x 3 matrix has 9 cells (grid sections or just &#8216;grids&#8217;). The matrix can be seen as a &#8220;game universe&#8221; and each cell is the current &#8220;stage&#8221; for interaction. Some of you old-timers may remember the <em>Star Trek</em> games from the early days of desktop computing. Your starship would be an asterisk and you&#8217;d go from grid cell to grid cell chasing or chased by Romulans or Klingons, and when you came to the edge of a section you&#8217;d just change sections and with it came a new &#8220;stage.&#8221;</p>
<p>I have no idea of the programming behind those games, but the concept is perfectly clear. As you change sections, you get a new &#8216;map&#8217; of that section. To get a sense of what&#8217;s going on, click the <strong>Play</strong> button to view the desktop version of the game, and then the <strong>Download</strong> button to get all of the code and objects in FLA and SWC files:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/sam14/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="play button" title="play" width="136" height="48" class="alignleft size-full wp-image-6372" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/dp/sam14/sam14.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="download this sucker" title="download" width="136" height="48" class="alignnone size-full wp-image-6426" /></a></p>
<p><strong>Movement Rules</strong></p>
<p>To get started building this game, I wanted to begin with simple movement rules. So I decided that movement would only be up, down, left, right. No diagonal movement allowed. For example, if an object were in <strong>Grid 7</strong>, it could go either to <strong>Grid 4</strong> or <strong>Grid 8</strong>. No movement to <strong>Grid 5</strong> or off the grid allowed. The opponent character is randomly placed into one of the grids, and the player moves from grid to grid until he finds the opponent. Figure 1 shows an example:<br />
<div id="attachment_6470" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/02/samFig1.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/02/samFig1.png" alt="two sams" title="samFig1" width="500" height="330" class="size-full wp-image-6470" /></a><p class="wp-caption-text">Figure 1: The player&#039;s samurai and opponent meet in a grid cell</p></div></p>
<p>The four Japanese characters are buttons for movement direction—North (up), South (down), West (left) and East (right). The buttons are simple ones and use a <strong>TouchEvent</strong> to slip in images behind the button set and characters. A single &#8220;Attack&#8221; button does nothing more than animate the characters [for now]. Eventually, I want the movement of the individual samurai objects to move across each grid and then jump to the next grid once he reaches a border. I also want the character to move faster along roads than non-road terrain. For example, in Figure 2, the character would be able to move speedily along roads to the next grid cell moving north, south or east, but moving west (left) movement would be slower because the lack of a road. Also, he would be able to move at road speed across the bridge (bottom right), but he would move across the river with no bridge at a much slower rate if at all.<br />
<div id="attachment_6472" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2012/02/samFig2.png" alt="" title="samFig2" width="500" height="330" class="size-full wp-image-6472" /><p class="wp-caption-text">Figure 2: Movement options (iPhone screen shot)</p></div></p>
<p>At this juncture, all I have is the movement through grids and not within a grid. In order to facilitate movement, I&#8217;ve employed a <strong>State Design Pattern</strong> (See <a href="http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii/" title="State Diagram">Class and File Diagrams</a> in earlier posts). Each state class shows the options. For example, <strong>State 5</strong> is right in the middle of the grid (see grid map at the beginning of this post.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6333code40'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p633340"><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
</pre></td><td class="code" id="p6333code40"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Grid5State <span style="color: #0066CC;">implements</span> IGrid
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Grid5State<span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goWest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getGrid4State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">4</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getGrid6State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">6</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goNorth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getGrid2State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">2</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goSouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getGrid8State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">8</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, all four directions are available to a player in <strong>Grid 5</strong>. However, if we look at <strong>Grid 9</strong>, the player can only go north or west as shown in the following class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6333code41'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p633341"><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
</pre></td><td class="code" id="p6333code41"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Grid9State <span style="color: #0066CC;">implements</span> IGrid
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Grid9State<span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goWest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getGrid8State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">8</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">9</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goNorth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getGrid6State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">6</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goSouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">9</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>If the player tries to go south or east, the class simply returns to value of the current grid and the player goes nowhere. Likewise, no context is changed. A choice of north or west results in the appropriate change in context and the next grid cell is slipped in.</p>
<p><strong>The Client, Context and State Interface</strong></p>
<p>Like all State Design Patterns, the Client makes its calls through the context. In this case the Client is pretty big because it is also setting the state with buttons and whatnot. However, once in place, the navigation and attack buttons no longer need to be called because the backgrounds are slipped beneath them. You can see how this works in Client code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6333code42'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p633342"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
</pre></td><td class="code" id="p6333code42"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TouchEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> sam1:<span style="color: #0066CC;">MovieClip</span>=<span style="color: #000000; font-weight: bold;">new</span> Sam1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> sam2:<span style="color: #0066CC;">MovieClip</span>=<span style="color: #000000; font-weight: bold;">new</span> Sam2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> foeGrid:uint = uint<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: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gridNow:uint;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> attackBtn:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> westBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> West<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> eastBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> East<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> northBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> North<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> southBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> South<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid2:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid3:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid4:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid4<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid5:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid5<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid6:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid6<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid7:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid7<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid8:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid8<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid9:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Grid9<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			grid = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;grid&quot;</span> + context.<span style="color: #006600;">randomStart</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>grid,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			sam1.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">350</span>,sam1.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">220</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sam1<span style="color: #66cc66;">&#41;</span>;
			sam1.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			sam2.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">400</span>,sam2.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			sam2.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			setControls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> setControls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">TOUCH_POINT</span>;
			northBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">67</span>,northBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">5</span>;
			northBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,northNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>northBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			westBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,westBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">80</span>;
			westBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,westNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>westBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			eastBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>,eastBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">80</span>;
			eastBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,eastNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>eastBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			southBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">67</span>,southBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">160</span>;
			southBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,southNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>southBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Animate</span>
			attackBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>,attackBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Attack!&quot;</span>;
			attackBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">15</span>,attackBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">360</span>;
			attackBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,attackNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>attackBtn<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> northNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#41;</span>;
			gridNow = context.<span style="color: #006600;">doNorth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				addChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			grid = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;grid&quot;</span> + gridNow<span style="color: #66cc66;">&#93;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>grid,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> westNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#41;</span>;
			gridNow = context.<span style="color: #006600;">doWest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				addChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			grid = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;grid&quot;</span> + gridNow<span style="color: #66cc66;">&#93;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>grid,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> eastNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#41;</span>;
			gridNow = context.<span style="color: #006600;">doEast</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				addChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			grid = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;grid&quot;</span> + gridNow<span style="color: #66cc66;">&#93;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>grid,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> southNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>grid<span style="color: #66cc66;">&#41;</span>;
			gridNow = context.<span style="color: #006600;">doSouth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>gridNow==foeGrid<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				addChild<span style="color: #66cc66;">&#40;</span>sam2<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			grid = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;grid&quot;</span> + gridNow<span style="color: #66cc66;">&#93;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>grid,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Toggle button</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> attackNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span> == <span style="color: #ff0000;">&quot;Attack!&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				sam1.<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
				sam2.<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
				attackBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Cease!&quot;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				sam1.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
				sam2.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
				attackBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Attack!&quot;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>One of the features in the <strong>Client</strong> class that I don&#8217;t like is the use of <em>conditional statements</em> in the calls to the different directions. I found that the &#8220;Foe Samurai&#8221; was a bit of a problem. He&#8217;s only allowed in the random cell that has been selected, and if I didn&#8217;t remove the <strong>sam2</strong> object, he&#8217;d get over stacked whenever the player object (<strong>sam1</strong>) entered the cell. There&#8217;s probably some more elegant way to deal with that issue, but I didn&#8217;t see it.</p>
<p>I decided to use grid numbers—unsigned integers—in the hope of making the code more efficient. The grid number is knitted to a &#8220;grid&#8221; from a <strong>Sprite</strong> class stored in the Library. All grids are instantiated and assigned values at the outset and so they&#8217;re not re-instantiated; just added and removed from the stage&#8211;sort of like the magic trick where the table cloth is pulled quickly from a table and all the dishes and silverware remain on top.</p>
<p>The <strong>IGrid</strong> interface and <strong>Contex</strong>t class are pretty simple. The interface has methods for the four directions:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6333code43'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p633343"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p6333code43"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Interface</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IGrid
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> goWest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint;
		<span style="color: #000000; font-weight: bold;">function</span> goEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint;
		<span style="color: #000000; font-weight: bold;">function</span> goNorth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint;
		<span style="color: #000000; font-weight: bold;">function</span> goSouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Given the polymorphism that goes into each of these methods with the nine concrete states, it&#8217;s really nice to have this kind of interface. It&#8217;s a mix of flexibility, simplicity and possibilities.</p>
<p>The <strong>Context</strong> is also fairly simple, but it is at the heart of the State pattern because it keeps track of the current state. By doing so, no matter what state (grid cell) the samurai is in, not only does it know what cell it in, the connected concrete <strong>State</strong> class provides all the options available—<em>without a single conditional statement</em>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6333code44'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p633344"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code" id="p6333code44"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Context class</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid1State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid2State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid3State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid4State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid5State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid6State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid7State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid8State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grid9State:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateNow:IGrid;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rndNow:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;States Set&quot;</span><span style="color: #66cc66;">&#41;</span>;
			grid1State = <span style="color: #000000; font-weight: bold;">new</span> Grid1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid2State = <span style="color: #000000; font-weight: bold;">new</span> Grid2State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid3State = <span style="color: #000000; font-weight: bold;">new</span> Grid3State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid4State = <span style="color: #000000; font-weight: bold;">new</span> Grid4State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid5State = <span style="color: #000000; font-weight: bold;">new</span> Grid5State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid6State = <span style="color: #000000; font-weight: bold;">new</span> Grid6State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid7State = <span style="color: #000000; font-weight: bold;">new</span> Grid7State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid8State = <span style="color: #000000; font-weight: bold;">new</span> Grid8State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			grid9State = <span style="color: #000000; font-weight: bold;">new</span> Grid9State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rndNow=uint<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: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			stateNow = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;grid&quot;</span>+rndNow+<span style="color: #ff0000;">&quot;State&quot;</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> randomStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> rndNow;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> doWest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow.<span style="color: #006600;">goWest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> doEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow.<span style="color: #006600;">goEast</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> doNorth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow.<span style="color: #006600;">goNorth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> doSouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow.<span style="color: #006600;">goSouth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>stateNow:IGrid<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateNow</span> = stateNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid1State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid2State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid2State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid3State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid3State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid4State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid4State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid5State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid5State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid6State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid6State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid7State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid7State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid8State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid8State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> getGrid9State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGrid
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">grid9State</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Note that all of the grid state properties are typed as <strong>IGrid</strong> types—that&#8217;s <em>Programming to the interface instead of the implementation</em>. This provides the properties with far more flexibility and reduces binding. Likewise, the return types on the getter functions were all typed to the <strong>IGrid</strong> interface, and so changes are easy because objects are not bound to the implementation.</p>
<p><strong>Some (More) Mobile Tweaks</strong></p>
<p>In the original version of this app, I used the <strong>SimpleButton</strong> timeline with different states for Up/Over/Down/Hit. I noticed that the <strong>Over</strong> state did not work but the buttons did—sort of. However, the movement was gummy and I often had to tap the button several times. I removed all of the states except for <strong>Up</strong> state and the movement event handling went much smoother. I could probably just as well use Sprite objects for buttons and I may change that.</p>
<p>Another feature that I want to work on is a <strong>New Game</strong> button of some sort. Given the sparse real estate on a mobile device I may have to have some kind of popup menu with different options for a new game, attacking and defending. I&#8217;d like to clear out memory and re-set where the &#8220;opponent&#8221; samurai goes—fresh start the whole thing.</p>
<p>Finally, I&#8217;m going to have to integrate intra-cell and inter-cell movement. All movement within a cell will uses the current State pattern so that when the samurai reaches the border it will jump to the next cell.  This kind of movement may require an additional set of state classes, but right now I&#8217;m not sure. Likewise, I want to set it up so that simultaneous movement of the player samurai and the robot samurai can attack/defend. The <strong>Symmetric Proxy Pattern</strong> (Chapter 13) can possibly be employed so that each combat movement is judged simultaneously.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F02%2Fthe-grid-state-movement-in-mobile%2F&amp;title=The%20Grid%20State%3A%20Movement%20in%20Mobile" id="wpa2a_12"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=4FQG6pOhSWs:N-d5HzWjwTE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=4FQG6pOhSWs:N-d5HzWjwTE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=4FQG6pOhSWs:N-d5HzWjwTE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=4FQG6pOhSWs:N-d5HzWjwTE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=4FQG6pOhSWs:N-d5HzWjwTE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=4FQG6pOhSWs:N-d5HzWjwTE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/4FQG6pOhSWs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/02/the-grid-state-movement-in-mobile/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/02/the-grid-state-movement-in-mobile/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 GestureEvent: Working with Gestures on Mobile</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/GtGVUYi6Qwc/</link>
		<comments>http://www.as3dp.com/2012/01/actionscript-3-0-gestureevent-working-with-gestures-on-mobile/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 17:00:46 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6415</guid>
		<description><![CDATA[Using Gestures When working with high level and complex structures like Design Patterns, the focus is squarely on the relationship between objects—classes, inheritance, composition, implementation, interfaces and related concepts and structures. The &#8220;details&#8221; are the details of these various relationships and how they work in concert. Using Algorithm 1 or Algorithm 2 is only pertinent [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/wp-content/uploads/2012/01/billz.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/billz.png" alt="Developing Gesture Apps Requires Actions with No Equivalent in MouseEvents" title="billz" width="250" height="188" class="alignleft size-full wp-image-6445" /></a><strong>Using Gestures</strong></p>
<p>When working with high level and complex structures like <strong>Design Patterns</strong>, the focus is squarely on the relationship between objects—classes, inheritance, composition, implementation, interfaces and related concepts and structures. The &#8220;details&#8221; are the details of these various relationships and how they work in concert. Using <strong>Algorithm 1</strong> or <strong>Algorithm 2</strong> is only pertinent insofar as it relates to a method or property that needs to be loosely bound to some other object. If you start thinking about algorithm details as a primary concern and fretting over internals, you can easily lose sight of the larger structures that are under development and never really understand <strong>Design Patterns</strong>.</p>
<p><strong>Seismic Shift</strong></p>
<p>Every now and again we encounter a <em>seismic shift</em> in the way things are done, and you have to make adjustments.(Some of you may remember the <strong>Commodore 64</strong> that went the way of the Dodo Bird. At one time it was the most popular personal computer on earth, but if you insisted on sticking with it, you probably don&#8217;t have any clients for your services.) In my own case, shift to mobile devices forced me to spend some quality time with the event processes in ActionScript 3.0. Going to the base, I reviewed how the Display List is handled in Flash and by AS3. Then I went to look at the AS3 Event Flow and among the several articles I read, one of the best is <a href="http://www.actionscript.org/resources/articles/860/1/Understanding-the-AS3-Event-Flow/Page1.html">Jody Hall&#8217;s</a>. Adobe has several good ones as well, and I&#8217;m sure many more are available that some of you may want to share.</p>
<p><strong>On to Gestures</strong></p>
<p>I wrote some gesture events and handlers. The results were unsatisfactory. It wasn&#8217;t that they didn&#8217;t work, but after one would work, another would not. Very baffling and frustrating. My goal was to create a &#8216;workbench&#8217; where I could try all kinds of gesture events, but I had very poor luck with just about everything at some point. So I backed away and tried an application with only two gestures; neither available with MouseEvent. One was <strong>Rotate</strong> and the other <strong>Zoom</strong>. Christian Cantrell has a <a href="http://www.adobe.com/devnet/flash/articles/multitouch_gestures.html">good article</a> on using gestures, and in addition to getting some good information on using gesture events, I also got a nice little piece on using gestures with bitmaps. (I had been having a devil of a time with Sprite objects in the Library with gestures.) Click the download button below to get the source code and FLA files for Flash Pro 5.5, 5 and 4 along with the image files and ActionScript 3.0 code:</p>
<p><a href="http://www.sandlight.com/DesignPatterns/PicFlip.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="download this sucker" title="download" width="136" height="48" class="alignnone size-full wp-image-6426" /></a></p>
<p>In my last post, I was convinced that <strong>TOUCH_TAP</strong> from <strong>TouchEvent</strong> works better than <strong>CLICK</strong> from <strong>MouseEvent</strong> with a mobile device. That conclusion was not based on the inner-workings of events and event handling, but rather from testing the different event handlers using my iOS device, a 4S iPhone. In the Adobe documentation, they point out that it <em>might be better</em> to use a <strong>MouseEvent</strong> rather than a <strong>TouchEvent</strong> or <strong>GestureEvent</strong>. It depends on the app, device and device OS. That pretty much makes it an empirical question.</p>
<p>In this application, there was no choice to use <strong>MouseEvent</strong> because it does not contain the required finger-flipping gestures I can make on my iPhone—<strong>GESTURE_ZOOM</strong> and <strong>GESTURE_ROTATE</strong>. Likewise, <strong>TouchEvent</strong> did not have the kinds of gestures; so I had to use <strong>TransformGestureEvent</strong>. The following code shows how it is employed in a simple picture flipping/zooming app:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6415code47'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p641547"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p6415code47"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TransformGestureEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>SWF<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">640</span>, <span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">960</span>, frameRate=<span style="color: #cc66cc;">24</span>, <span style="color: #0066CC;">backgroundColor</span>=0xcc0000<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PicFlip <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source = <span style="color: #ff0000;">&quot;vanBillz.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> BillzImage:<span style="color: #000000; font-weight: bold;">Class</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> billzBitmap:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> BillzImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> carrier:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PicFlip<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">320</span>, carrier.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">550</span>;
			carrier.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>billzBitmap<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			billzBitmap.<span style="color: #006600;">x</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">320</span> - <span style="color: #66cc66;">&#40;</span>billzBitmap.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</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;">*</span> -<span style="color: #cc66cc;">1</span>;
			billzBitmap.<span style="color: #006600;">y</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">480</span> - <span style="color: #66cc66;">&#40;</span>billzBitmap.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">height</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;">*</span>-<span style="color: #cc66cc;">1</span>;
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>carrier<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">GESTURE</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ZOOM</span>, doZoom<span style="color: #66cc66;">&#41;</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ROTATE</span>, doRotate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doZoom<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">scaleX</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleX</span>;
			carrier.<span style="color: #006600;">scaleY</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleY</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doRotate<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">rotation</span> +=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">rotation</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I tested it on an iPhone, but there&#8217;s no reason it won&#8217;t work perfectly well on an Android or some other mobile device. Further, I found it to be very responsive. The zooming seemed to work quite well with bitmapped graphics, and so I assumed that it would work just as well, if not better with a vector object in the Library. So I put together a little test symbol using the drawing tools in Flash and stored it as  Sprite class in the Library. (By the way, in case you were not aware, to make a Sprite class in the Library, when you create a New Symbol, just classify it as a MovieClip and in the Base Class window change the type from <strong>flash.display.MovieClip</strong> -> <strong>flash.display.Sprite</strong>. The Library icon turns from Blue to Green to indicate it is now a Sprite.)</p>
<p>I used the following code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6415code48'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p641548"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p6415code48"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TransformGestureEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PicFlipLib <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> carrier:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> billzVector:Sprite=<span style="color: #000000; font-weight: bold;">new</span> BillzVector<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PicFlipLib<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">220</span>, carrier.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">350</span>;
			carrier.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>billzVector<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>carrier<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">GESTURE</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ZOOM</span>, doZoom<span style="color: #66cc66;">&#41;</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ROTATE</span>, doRotate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doZoom<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">scaleX</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleX</span>;
			carrier.<span style="color: #006600;">scaleY</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleY</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doRotate<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">rotation</span> +=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">rotation</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>One of the features that seemed to make a difference was that instead of having the event listener connected directly to the Library Sprite (<strong>BillzVector</strong>), I used an object container Sprite, <strong>carrier</strong>. Figure 1 shows the zoom and rotation on an iPhone 4S. This was used with both the bitmap and the vector Sprite objects, and they both worked quite well.<br />
<div id="attachment_6460" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/rotateZoom.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/rotateZoom.png" alt="rotate" title="rotateZoom" width="500" height="372" class="size-full wp-image-6460" /></a><p class="wp-caption-text">Figure 1: Both rotation and zoom worked fined with the vector-based Sprite</p></div></p>
<p><strong>Back to Design Patterns</strong></p>
<p>I was going to post my final matrix motion solution, but I got jammed up with event handling. Next time, I should be able to post the next stage in my Samurai game. It uses a State design pattern to move through nine cells in a 3 X 3 matrix. Everything was working well except that my event handlers were not as crisp as I wanted. I&#8217;ll have a chance to see now whether the time-out with AS3 event handlers was enough. The Design Pattern does its job; I just was not getting the event handlers to do theirs. In the next post you&#8217;ll be able to see whether they are all working well together in a mobile environment.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F01%2Factionscript-3-0-gestureevent-working-with-gestures-on-mobile%2F&amp;title=ActionScript%203.0%20GestureEvent%3A%20Working%20with%20Gestures%20on%20Mobile" id="wpa2a_14"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=GtGVUYi6Qwc:enhoFoyNZIM:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=GtGVUYi6Qwc:enhoFoyNZIM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/GtGVUYi6Qwc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/actionscript-3-0-gestureevent-working-with-gestures-on-mobile/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/actionscript-3-0-gestureevent-working-with-gestures-on-mobile/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 TouchEvent: Guaranteed Mobile Speedup</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/J05OqC36Tzc/</link>
		<comments>http://www.as3dp.com/2012/01/actionscript-3-0-touchevent-guaranteed-mobile-speedup/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 16:44:06 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Speed Tweaks]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6400</guid>
		<description><![CDATA[The TouchEvent Object I suppose old habits die hard, especially when those habits still work. (Some programmers are fond of saying, If it ain&#8217;t broke, don&#8217;t fix it! and the similarly dull-witted proverb, Don&#8217;t re-invent the wheel!) Well, with mobile devices, you&#8217;d better think about breaking old habits, and I put myself at the top [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6413" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/touch.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/touch.png" alt="Tap screen" title="touch" width="250" height="333" class="size-full wp-image-6413" /></a><p class="wp-caption-text">MouseEvent Works but TouchEvent Works better</p></div><strong>The TouchEvent Object</strong></p>
<p>I suppose old habits die hard, especially when those habits still work. (Some programmers are fond of saying, <em>If it ain&#8217;t broke, don&#8217;t fix it!</em> and the similarly dull-witted proverb, <em>Don&#8217;t re-invent the wheel!</em>) Well, with mobile devices, you&#8217;d better think about breaking old habits, and I put myself at the top of the dullard list for taking so long to make some adoptions. One of the first changes to make when creating mobile apps is to use the <strong>TouchEvent</strong> instead of the <strong>MouseEvent</strong>. In working on a game, I merrily kept using <strong>MouseEvent</strong> until I noticed that my apps kept getting stuck in the mud when I put them on a mobile device. So, I decided to give TouchEvent a try. Naturally, I couldn&#8217;t test in Flash Pro CS5.5; so I had to wait until I launched it in my iPhone. When I did, the difference was night and day. The sluggishness disappeared.</p>
<p>The good news is that <strong>MouseEvent</strong> and <strong>TouchEvent</strong> classes work pretty much the same as far as programming them into an app. I have no idea what&#8217;s going on with either under the hood, and I shouldn&#8217;t have to concern myself with those issues other than to know that one works better with mobile devices than the other. To get started, we&#8217;ll take a look at the sequence for setting up for TouchEvents, and while almost identical to MouseEvent, there are programming differences. First, though, click the Play button to see the little testing app on your computer. You can also download the Flash 5.5 Fla file and Client class by clicking on the Download button.<br />
<a href="http://www.sandlight.com/Touch/touchdown.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="" title="download" width="136" height="48" class="alignleft size-full wp-image-6426" /></a><a href="http://www.sandlight.com/Touch/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="play button" title="play" width="136" height="48" class="alignnone size-full wp-image-6372" /></a></p>
<p>As you can see it&#8217;s a simple little app to move a &#8220;Samurai&#8221; character left and right; up and down. The buttons are in Japanese, and if you read Japanese, you&#8217;ll realize that the characters look like a first-grader&#8217;s attempts to learn how to write in Japanese. (Feel free to make some better simple buttons using Japanese characters and send them along with any comments you may have.)</p>
<blockquote><p><strong>Possible Controversy</strong>: After I had finished this post, I ran across the following:</p>
<p>http://help.adobe.com/en_US/as3/dev/WSb2ba3b1aad8a27b0-6ffb37601221e58cc29-8000.html</p>
<p>In it is a note of caution:</p>
<p><em><strong>Note</strong>: Listening for touch and gesture events can consume a significant amount of processing resources (equivalent to rendering several frames per second), depending on the computing device and operating system. It is often better to use mouse events when you do not actually need the extra functionality provided by touch or gestures. When you do use touch or gesture events, consider reducing the amount of graphical changes that can occur, especially when such events can be dispatched rapidly, as during a pan, rotate, or zoom operation. For example, you could stop animation within a component while the user resizes it using a zoom gesture.</em></p>
<p>That certainly makes sense, but I found that even using TOUCH_TAP, it was a quicker and far more responsive than listening for a CLICK event. There&#8217;s only one way to resolve this issue; try it yourself with a mobile app on a mobile device&#8211;any one you want. Use the Comment section to report what you&#8217;ve found. The sample app is as good as any for a trial, but feel free to create one of your own choosing.</p>
<p>As Chico Marx remarked in <strong>Duck Soup</strong>, <em>Well, who you gonna believe, me or your own eyes? </em> Believe your eyes and give it a whirl!
</p></blockquote>
<p><strong>Just the Right Touch</strong></p>
<p>Setting up the TouchEvent includes three imports:</p>
<ul>
<li><strong>import flash.events.TouchEvent;</strong></li>
<li> <strong>import flash.ui.Multitouch;</strong></li>
<li><strong>import flash.ui.MultitouchInputMode;</strong></li>
</ul>
<p>That contrasts with the single <strong>MouseEvent</strong> import required when using the mouse event. However, prior to using the TouchEvent class you have to set up your mobile context with the following line:</p>
<p>     <strong>Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; </strong></p>
<p>That statement focuses the input mode to touch instead of a mouse event. I was unable to get the <strong>TouchEvent</strong> to work without the statement; so, don&#8217;t forget to include it in your program.</p>
<p>Finally, you need an event listener, and it has the same format as a mouse event except you use <strong>TouchEvent</strong>. Also, instead of using CLICK, use TOUCH_TAP, as the following illustrates:</p>
<p>     <strong>upBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow);</strong></p>
<p>With that knowledge, you&#8217;re all set to set up an app using TouchEvent.</p>
<p><strong>Many Ears; One Handler</strong></p>
<p>One of the big memory hogs in programming, especially noticeable in mobile devices, is event listening. Each event listener sets up a series of operations that have to keep listening for an event. Having a single event won&#8217;t help, but it&#8217;s a step in the direction of having a single listener (which will help). The following <strong>Client</strong> sets up an example of using the <strong>TouchEvent</strong>. All of the buttons are <strong>SimpleButton</strong> objects and the background is a big <strong>Sprite</strong>. Likewise, <strong>Sam3</strong> is a Sprite that you can move around using tap on your mobile device.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6400code50'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p640050"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code" id="p6400code50"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TouchEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> leftBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Left</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> rightBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Right</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> upBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Up</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> downBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Down</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> bkground:Sprite=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Background</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sam3:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sam3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChildAt<span style="color: #66cc66;">&#40;</span>bkground,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			setControls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setSamurai<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> setControls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">TOUCH_POINT</span>;
			upBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">825</span>,upBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">19</span>;
			upBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>upBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			leftBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">740</span>,leftBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">106</span>;
			leftBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>leftBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rightBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">873</span>,rightBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">105</span>;
			rightBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rightBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			downBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">825</span>,downBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">202</span>;
			downBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>downBtn<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> setSamurai<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sam3.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">960</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>; sam3.<span style="color: #006600;">y</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">640</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sam3<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> moveNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance11&quot;</span>:
				sam3.<span style="color: #006600;">y</span> -=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance1&quot;</span>:
				sam3.<span style="color: #006600;">x</span> -=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance6&quot;</span>:
				sam3.<span style="color: #006600;">x</span> +=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance16&quot;</span>:
				sam3.<span style="color: #006600;">y</span> +=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I set it up to be viewed from a landscape perspective because of a larger project I&#8217;m developing. Also, as you can see in the <strong>moveNow</strong> method, there are no &#8220;brakes&#8221; on the movement, and so the character can move anywhere, including right off the stage. Currently, I have a nicely working global movement system set up using a State Pattern, and I&#8217;d like to integrate both local and global movement into a single movement. Not only would this cut down on the number of listeners, but it would also provide a more interesting game. Figure 1 shows how it looks on an iPhone, and I&#8217;d like any feedback if tried on Android or other non-iOS devices.<br />
<div id="attachment_6422" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/touchMob.png" alt="" title="touchMob" width="500" height="333" class="size-full wp-image-6422" /><p class="wp-caption-text">The 960 x 640 size is compressed to fit in an iPhone environment</p></div></p>
<p>You also might want to swap out the <strong>TouchEvent</strong> for a <strong>MouseEvent</strong> and try it again in a mobile device. You&#8217;ll find that it gets &#8220;stuck&#8221; after a few clicks. As always, we&#8217;re interested in your comments and feedback.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F01%2Factionscript-3-0-touchevent-guaranteed-mobile-speedup%2F&amp;title=ActionScript%203.0%20TouchEvent%3A%20Guaranteed%20Mobile%20Speedup" id="wpa2a_16"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=J05OqC36Tzc:dmHVwkfvzNQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=J05OqC36Tzc:dmHVwkfvzNQ:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/J05OqC36Tzc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/actionscript-3-0-touchevent-guaranteed-mobile-speedup/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/actionscript-3-0-touchevent-guaranteed-mobile-speedup/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 Single Class Event Handler</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/725Jqmf7Z-8/</link>
		<comments>http://www.as3dp.com/2012/01/actionscript-3-0-single-class-event-handler/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 16:08:28 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Speed Tweaks]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6359</guid>
		<description><![CDATA[One Object:Several Events The first time I read Scott Petersen&#8217;s article on optimizing code for iOS devices, I got it wrong in terms of what he was trying to do in one of the optimizations. In re-reading it, I discovered an interesting structure Scott included, and while not a speed tweak in and of itself, [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6398" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/reachout.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/reachout.png" alt="reachout" title="reachout" width="250" height="165" class="size-full wp-image-6398" /></a><p class="wp-caption-text">Reaching out to Single Class for Event Handling</p></div><strong>One Object:Several Events</strong></p>
<p>The first time I read <a href="http://www.adobe.com/devnet/flash/articles/optimize_content_ios.html">Scott Petersen&#8217;s</a> article on optimizing code for iOS devices, I got it wrong in terms of what he was trying to do in one of the optimizations. In re-reading it, I discovered an interesting structure Scott included, and while not a speed tweak in and of itself, I thought it deserved a short discussion and example.</p>
<p>The idea is to have one object with several methods handle all event requests. A single method handles a single request. As you know, event-requests expect one parameter&#8211;the event-object listening for the event. Once initiated, the <strong>event.target</strong> object can be used to determine an appropriate course of action. To see how this works, I put together a simple class for handling requests. It does nothing more than trace back the label of the UI that makes the initial request.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6359code53'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635953"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p6359code53"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> OneNav
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navNorth<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navSouth<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navEast<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navWest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With the <strong>OneNav</strong> class, the Client can then address all event handlers to the same class (object) and just append the specific method to the object. The general format is:</p>
<blockquote><p>listeningObj.addEventListener(MouseEvent.CLICK,actionObj.actionMethod);</p></blockquote>
<p>So now the developer can have everything to be handled in a single class with methods taking care of the details. If the details change, the developer can just go change the methods to reflect the changed details. To test it out, I&#8217;ve included the following <strong>Client</strong> class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6359code54'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635954"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p6359code54"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnNorth:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnSouth:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnEast:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnWest:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> navNow:OneNav=<span style="color: #000000; font-weight: bold;">new</span> OneNav<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnNorth.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navNorth</span><span style="color: #66cc66;">&#41;</span>;
			btnNorth.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnNorth.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			btnNorth.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;North&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnNorth<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnSouth.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navSouth</span><span style="color: #66cc66;">&#41;</span>;
			btnSouth.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnSouth.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">80</span>;
			btnSouth.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;South&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnSouth<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnEast.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navEast</span><span style="color: #66cc66;">&#41;</span>;
			btnEast.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnEast.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">110</span>;
			btnEast.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;East&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnEast<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnWest.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navWest</span><span style="color: #66cc66;">&#41;</span>;
			btnWest.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnWest.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">140</span>;
			btnWest.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;West&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnWest<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In some respects, this technique can add more work for the developer as he tries to figure out how to get the events to spark something on the viewing area even if he&#8217;s using a design pattern. This particular implementation reflects work I&#8217;ve been doing on the navigation portion of a game I&#8217;m developing, but the technique could apply to virtually any project. I would have to have the <strong>OneNav</strong> method call a State Pattern to get the appropriate image to show up on the stage. The would take a bit of doing. However, for some other kind of application, it might be just the thing you need.</p>
<p><strong>A <em>Final</em> Word</strong></p>
<p>One of the speed tweaks that Scott points out in this same article is using <strong>final</strong> in marking classes and methods. If you&#8217;re not subclassing a given class, you can mark it <strong>final</strong> as done in the <strong>Client</strong> class. Marking a class <strong>final</strong> allows compile-time determination of the exact function being called. When some methods of a class can be overridden, you cannot mark the class <strong>final</strong> but you can mark the individual method as <strong>final</strong>, as was done in the <strong>OneNav</strong> class.</p>
<p>As a matter of fact, I cannot think of a single use case where I would subclass a Client class, and so, it&#8217;s probably a safe bet to mark all Client classes as final. When developing with ActionScript 3.0 for mobile devices, every single speed tweak matters.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F01%2Factionscript-3-0-single-class-event-handler%2F&amp;title=ActionScript%203.0%20Single%20Class%20Event%20Handler" id="wpa2a_18"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=725Jqmf7Z-8:cUwpu0iShTk:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=725Jqmf7Z-8:cUwpu0iShTk:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/725Jqmf7Z-8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/actionscript-3-0-single-class-event-handler/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/actionscript-3-0-single-class-event-handler/</feedburner:origLink></item>
		<item>
		<title>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part II The Help Desk</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/zQJp_uYUrfM/</link>
		<comments>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 22:29:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://new.as3dp.com/?p=6352</guid>
		<description><![CDATA[A JavaScript Chain of Responsibility I&#8217;ve always liked JavaScript, but after going through this last project of creating a design pattern with JS, I find that I like ActionScript 3.0 even more than I did before. The biggest problems that I encountered with JavaScript is that different users slipped in different frameworks (e.g., Prototype.js) and [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6360" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/jscor250.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/jscor250.png" alt="corjs" title="jscor250" width="250" height="333" class="size-full wp-image-6360" /></a><p class="wp-caption-text">JavaScript can do Design Patterns</p></div><strong>A JavaScript Chain of Responsibility</strong></p>
<p>I&#8217;ve always liked JavaScript, but after going through this last project of creating a design pattern with JS, I find that I like ActionScript 3.0 even more than I did before. The biggest problems that I encountered with JavaScript is that different users slipped in different frameworks (e.g., Prototype.js) and didn&#8217;t seem to mention it. My goal was to create a JS design pattern with pure unadulterated JavaScript—not using JQuery, JSon or some other helpful framework. Now, don&#8217;t get me wrong—I appreciate a good framework as much as the next guy. However, when I&#8217;m trying to create a design pattern for a general language like JavaScript, I&#8217;d rather not have to have readers run out and get one framework or another. I wanted this to work with Plain Vanilla JavaScript. So that&#8217;s what you&#8217;re going to get.</p>
<p>To get started, I took a look at some articles on JavaScript and classes, inheritance and OOP in general. Then I looked at some implementations of JavaScript with a Chain of Responsibility pattern. A lot of them didn&#8217;t make a bit of sense, and that&#8217;s when I realized that they were slipping in different &#8220;helpers,&#8221; and so I went back to the most basic JavaScript OOP I knew of&#8211;the 1999 book, <em>JavaScript Objects</em> by Nakhimovsky and Myers.  Starting there, I pretty much followed that Chain of Responsibility pattern we had in <a href="http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/">Part I</a> of this two-part series. However, instead of a mobile browser detector, I created a &#8220;Help Desk.&#8221; </p>
<p>Before continuing, take a look at the &#8220;Help Desk&#8221; app made with the JS CoR:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/jscor/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="" title="play" width="136" height="48" class="alignnone size-full wp-image-6372" /></a><br />
<div id="attachment_6385" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/helpdesk.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/helpdesk.png" alt="helpdesk" title="helpdesk" width="500" height="500" class="size-full wp-image-6385" /></a><p class="wp-caption-text">Figure 1: JavaScript Chain of Responsibility Help Desk</p></div></p>
<p><strong>Inheritance is a &#8216;IS-A&#8217;</strong></p>
<p>First I made an abstract Handler class using a single concrete and single abstract method. It seemed to me that classes in JavaScript could be abstract simply by adding a method with no content. I came up with the following class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code60'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635260"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p6352code60"><pre class="javascript" style="font-family:monospace;">function Handler()
{ 
	//Handler 'abstract class'
} 
&nbsp;
Handler.prototype.setSuccessor=function(successor)
{ 
	this.successor=successor;
} 
&nbsp;
Handler.prototype.handleRequest=function(req)
{ 
	//Abstract method overridden in concrete implementation
}</pre></td></tr></table></div>

<p>I realize that it looks strange compared to more mature OOP languages like ActionScript 3.0 (not to mention Java and C++), but remember, I&#8217;m using pure native JavaScript. You create the class by the simple expedient of a function. Then, I create the methods for the class by using a <strong>Class.prototype</strong>. The class Handler now has the <strong>setSuccessor</strong> and <strong>handleRequest</strong> methods, just as we had in the ActionScript 3.0 version. So, while it looks a little goofy for creating classes and their methods and properties, that&#8217;s how it&#8217;s done.</p>
<p>So far so good, and it&#8217;s not rocket science. However, the next part gets a little dicey and may pop a few brain nodes. When you extend a class in JavaScript, you first instantiate the parent class. As we all know, you do not instantiate abstract classes or any other interface. They&#8217;re extended or implemented. However, that&#8217;s not the case with JavaScript. Take a look at this first child class of Handler:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code61'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635261"><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
</pre></td><td class="code" id="p6352code61"><pre class="javascript" style="font-family:monospace;">//AS3DP inherits handler
AS3DP.prototype= new Handler();
&nbsp;
function AS3DP()
{
	//AS3DP 'class'
}
&nbsp;
AS3DP.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;as3dp&quot;)
	{
		document.write(&quot;Naturally you want to read &lt;em&gt;ActionScript 3.0 Design Patterns&lt;/em&gt; and visit our blog at as3dp.com.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not AS3DP&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}</pre></td></tr></table></div>

<p>If you recall, basic design pattern principles, a child class &#8220;Is-A&#8221; parent class. When a JavaScript object (like AS3DP) is instantiated, it is done by declaring itself as a <strong>new Handler()</strong>. So rather than becoming a child of the parent through extension, JavaScript uses the object prototype to declare itself as an instance of the parent. It&#8217;s actually easier to see that a child class indeed &#8220;Is-A&#8221; parent because the prototype declares itself as such.</p>
<p>As we have seen, the methods and properties have been created <em>outside of the class constructors</em>. Instead of being <em>in the class</em>, they are <em>in the prototype</em>. For now, you can think of the class as the &#8220;concept of an object&#8221; and it creates and stores the properties for that concept in prototypes. So, knowing that, we can create the rest of the concrete handlers:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code62'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635262"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code" id="p6352code62"><pre class="javascript" style="font-family:monospace;">//Game inherits Handler
Game.prototype= new Handler();
&nbsp;
function Game()
{
	//Game 'class'
}
&nbsp;
Game.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;game&quot;)
	{
		document.write(&quot;As the states change, you want a good state machine like the State Design Pattern.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not State pattern&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Algorithm inherits Handler
Algorithm.prototype= new Handler();
&nbsp;
function Algorithm()
{
	//Algorithm 'class'
}
&nbsp;
Algorithm.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;algorithm&quot;)
	{
		document.write(&quot;The Strategy Design Pattern lets you access different algorithms directly without having to use conditional statements!&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Strategy pattern&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Factory inherits Handler
Factory.prototype= new Handler();
&nbsp;
function Factory()
{
	//Factory 'class'
}
&nbsp;
Factory.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;factory&quot;)
	{
		document.write(&quot;The Factory Method design pattern will unlink the request to build an object from the actual object creation.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Factory&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//ToInterface inherits Handler
ToInterface.prototype= new Handler();
&nbsp;
function ToInterface()
{
	//ToInterface 'class'
}
&nbsp;
ToInterface.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;tointerface&quot;)
	{
		document.write(&quot;The first principle is to &lt;em&gt;Program to the interface and not the implementation.&lt;/em&gt; When you declare a new object, type it to the object's parent class; not the object itself.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Program to Interface&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Truth inherits Handler
Truth.prototype= new Handler();
&nbsp;
function Truth()
{
	//Truth 'class'
}
&nbsp;
Truth.prototype.handleRequest=function(req)
{
&nbsp;
	document.write(&quot;You know I can't handle the truth! Go visit a muse, a swami, or 3-year old.&lt;br/&gt;&quot;);
&nbsp;
}</pre></td></tr></table></div>

<p>As you no doubt have noticed, this Chain of Responsibility implementation is not about mobile devices and their languages. Instead, it&#8217;s a &#8220;Help Desk&#8221; that I think is more typical of what you may actually use a CoR pattern for—either in JavaScript or ActionScript 3.0.</p>
<p>All that&#8217;s left is to do with the JavaScript is to create instances of the different handlers and set the chain of successors. The &#8220;client&#8221; that makes the requests is the HTML5 UI.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code63'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635263"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p6352code63"><pre class="javascript" style="font-family:monospace;">//'Instantiate Concrete Handlers'
var as3dp = new AS3DP();
var game=new Game();
var algorithm=new Algorithm();
var factory=new Factory();
var tointerface=new ToInterface();
var truth=new Truth();
//Set successors
as3dp.setSuccessor(game); 
game.setSuccessor(algorithm); 
algorithm.setSuccessor(factory);
factory.setSuccessor(tointerface);
tointerface.setSuccessor(truth);</pre></td></tr></table></div>

<p>You can set up the successors in any way you want except the last one has to be to the <strong>Truth</strong> class. It has no successor and is meant to be the caboose of the chain. Before going on, place all of the JavaScript into a single file and save it as &#8220;HelpDesk.js&#8221; and put it in the same folder (directory) as the HTML5 program that follows.</p>
<p><strong>The HTML5 Client</strong></p>
<p>Like most work with JavScript, the UI is in HTML. This is no different. All it does is to call the top of the chain (<strong>AS3DP</strong> instance) and make a request. All of the requests look like the following:</p>
<blockquote><p>onclick=&#8221;as3dp.handleRequest(&#8216;algorithm&#8217;);</p></blockquote>
<p>The click handler calls the top of the chain, and the Chain of Responsibility just chugs on through until it finds what you want. Here&#8217;s the whole HTML5 code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code64'); return false;">View Code</a> HTML5</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635264"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p6352code64"><pre class="html5" style="font-family:monospace;">&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
body {
	font-family:Verdana, Geneva, sans-serif;
	color:#1D232;
	background-color:#dddcc5;
	font-size:12px;
}
h2 {
	font-family:&quot;Arial Black&quot;, Gadget, sans-serif;
	font-size:24px;
	text-align:center;
	color:#611427;
}
form {
	color:#6A6A61;
}
h3 {
	background-color:#611427;
	color:#958976;
	font-size:18px;
}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;HelpDesk.js&quot;&gt;
&lt;/script&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Billz Help Desk&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;header&gt;
  &lt;h2&gt;Billz Help Desk&lt;/h2&gt;
&lt;/header&gt;
&lt;article&gt;
&lt;header&gt;
  &lt;h3&gt;&amp;nbsp;Chain of Responsibility: JavaScript&lt;/h3&gt;
&lt;/header&gt;
&lt;section&gt; This is a simple Chain of Responsibility (CoR) application. Each button represents a different request that the CoR will handle. In this case, the help is in the form of a 'Help Desk' and the CoR finds the appropriate response to the query.&lt;br/&gt;
  &lt;br/&gt;
  &lt;form&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('as3dp');&quot; /&gt;
    How do I learn about design patterns for ActionScript 3.0 &lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('game');&quot; /&gt;
    I'm making a game where states keep changing. What would be a good design pattern for dealing with changing states?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('algorithm');&quot; /&gt;
    My app involves a lot of problem solving with different algorithms? What's a good pattern for dealing with multiple algorithms?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('factory');&quot; /&gt;
    Whenever I create a new object with my Client object, I build up dependencies. Is there a pattern to avoid such dependencies?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('tointerface');&quot; /&gt;
    What is a primary guiding design pattern principle?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('truth');&quot; /&gt;
    I want to know the truth!&lt;br/&gt;
  &lt;/form&gt;
&lt;/section&gt;
&lt;br/&gt;
&lt;section&gt;The CoR pattern works sequentially so that as it moves through the chain, you can see each rejected attempt to find the correct handler. (Normally, you wouldn't see those rejected handlers.) It is a very easy pattern to update and add additional handlers.&lt;/section&gt;
&lt;/article&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>So, now you&#8217;re all set to use JavaScript for design patterns. The plain vanilla JS has limitations doing OOP, but you can harness it and bend it to your OOP will. The many different frameworks that work in conjunction with basic JavaScript will make it look more like an OOP language, but you can get by without. There&#8217;s a lot to be done with even the most unadorned JavaScript, and I tested the app on an iPhone, and it works fine. However, I&#8217;ll bet that several of you can find better ways to handle either the JavaScript or HTML5 (or both) with design patterns, and we&#8217;d like to hear from you. Naturally, if you have a favored framework that makes OOP work more efficient, let us know—even better send a design pattern example.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2012%2F01%2Ffrom-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk%2F&amp;title=From%20ActionScript%203.0%20to%20JavaScript%20Chain%20of%20Responsibility%3A%20Part%20II%20The%20Help%20Desk" id="wpa2a_20"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=zQJp_uYUrfM:SRDNlD4MkSI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=zQJp_uYUrfM:SRDNlD4MkSI:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/zQJp_uYUrfM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/</feedburner:origLink></item>
	</channel>
</rss>

