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

<channel>
	<title>ActionScript 3.0 Design Patterns &#187; State</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/state/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript 3.0 Flash Builder App in Apple iOS: Decorator and State</title>
		<link>http://www.as3dp.com/2011/09/actionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state/</link>
		<comments>http://www.as3dp.com/2011/09/actionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 01:31:41 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6182</guid>
		<description><![CDATA[Decorator and State Patterns in Mobile Game Development While I&#8217;m trying to find the best graphic tool to use in conjunction with mobile devices and ActionScript 3.0, I&#8217;ve decided to stick with wholly programmed images. (I&#8217;m a better programmer than artist, but my images do not improve because they&#8217;re programmed.) For this example, I used [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6181" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/flip.png" alt="Change orientation with device position" title="flip" width="250" height="251" class="size-full wp-image-6181" /><p class="wp-caption-text">Change orientation with device position</p></div><strong>Decorator and State Patterns in Mobile Game Development</strong><br />
 While I&#8217;m trying to find the best graphic tool to use in conjunction with mobile devices and ActionScript 3.0, I&#8217;ve decided to stick with wholly programmed images. (I&#8217;m a better programmer than artist, but my images do not improve because they&#8217;re programmed.) For this example, I used the <strong>ActionScript Mobile Project</strong> option in <em><strong>Flash Builder 4.5.1</strong></em>.  If you&#8217;re using <em><strong>Flash Pro 5.5</strong></em>, you can use the same code by selecting <strong>AIR for iOS</strong> from the Flash main start-up menu. With both, everything is pure ActionScript 3.0—none of the drawing tools, components or other gizmos available in either FB or FP are employed—<em>código puro</em> in both. Click the button below to download the files for Flash Builder. (I also included the files for Flash Pro; so no one is left out.)<br />
<a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><strong>Reminder:</strong> We&#8217;ve placed all of the downloads on the University of Hartford&#8217;s <strong>Multimedia Web Design and Development download page</strong>. Just select the bottom on the ActionScript 3.0 files and click the download button.</p>
<blockquote><p><strong>Flash Builder Tip</strong>: First create an <em>ActionScript Project</em> so that you can test it as you develop. Once you have it all developed, create an  <em>ActionScript <strong>Mobile</strong> Project</em> and just copy and paste the files from the AS Project.</p></blockquote>
<p><strong>Re-Decorating</strong></p>
<p>In this first part all I want to do with this mobile game is to build and decorate the warrior and put it on an iOS mobile device using Flash Builder. The mechanics are very similar to previous posts on using the Decorator pattern to arm a character (See <a href="http://www.as3dp.com/2011/06/14/well-armored-warrior-decorator-for-beginners/">here</a> and <a href="http://www.as3dp.com/2011/06/15/the-warriors-new-clothes-using-as3-in-flash-builder-to-create-sprite-graphic-classes/">here</a>.) Figure 1 shows a file diagram of the Decorator implementation:<br />
<div id="attachment_6195" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobileDec.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Decorator File Diagram&lt;/em&gt;" title="mobileDec" width="500" height="381" class="size-full wp-image-6195" /><p class="wp-caption-text"><em><strong>Figure 1:</strong>Decorator File Diagram</em></p></div></p>
<p>Since this thing is going to be in pure code, we need to create a &#8220;warrior&#8221; in code, which was not done in the previous posts linked above. So outside of the pattern, we need a graphic image, as provided in 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('p6182code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61824"><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
</pre></td><td class="code" id="p6182code4"><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;">Shape</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> BuildWarrior <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> head:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> torso:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> lftArm:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> rtArm:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> lftLeg:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> rtLeg:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> assemble<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeHead<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeTorso<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeLftArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeRtArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeLftLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeRtLeg<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: #0066CC;">this</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeHead<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6F91A6,.50<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">17</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.25</span>,0x0066ff<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x035AA6,.40<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">35</span>, <span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">34</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
			graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> head;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeTorso<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			torso.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			torso.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6F91A6,.50<span style="color: #66cc66;">&#41;</span>;
			torso.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawTriangles</span><span style="color: #66cc66;">&#40;</span>Vector.<span style="color: #66cc66;">&lt;</span>number<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">35</span>,<span style="color: #cc66cc;">90</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			torso.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> torso;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeLftArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7,.75<span style="color: #66cc66;">&#41;</span>;
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> lftArm;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeRtArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7,.75<span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">51</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> rtArm;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeLftLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7<span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>,<span style="color: #cc66cc;">70</span>,<span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span>,<span style="color: #cc66cc;">115</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> lftLeg;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeRtLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7<span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span>,<span style="color: #cc66cc;">70</span>,<span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">37</span>,<span style="color: #cc66cc;">115</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> rtLeg;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All of the concrete decorators similarly implement coded graphic representations. Of course, all of these are &#8220;outside&#8221; of the design pattern but are useful to see in the contact of the pattern.</p>
<p><strong>The Client</strong></p>
<p>In the actual mobile client, you begin with a <strong>super();</strong> call and support for auto orientation in the iOS device. The code is automatically generated:</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('p6182code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61825"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p6182code5"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// support autoOrients</span>
<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;</pre></td></tr></table></div>

<p>I tossed out the <strong>super();</strong> call, and generated the following client:</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('p6182code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61826"><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="p6182code6"><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;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MobileDecState <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> warrior:Component=<span style="color: #000000; font-weight: bold;">new</span> Warrior<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> warriorSpirit: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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MobileDecState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>,warriorSpirit.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>warriorSpirit<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecBodyArmor<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecShield<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecHelmet<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecParticleCompressor<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// support autoOrients</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</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><strong>Publishing for Mobile</strong></p>
<p>For some reason, more steps are involved in publishing for Flash Builder than Flash Pro. However, once you have your certificate (it has a .p12 extension) and your provisioning file created using the tools available to Apple developers; it&#8217;s pretty simple. If you don&#8217;t know about the <a href="http://developer.apple.com/programs/ios/">Apple iOS developer program</a> for both Windows and Mac users, you need to go there first.</p>
<p>Here&#8217;s how to publish using Flash Builder: (<a href="http://www.adobe.com/support/flex/downloads_updaters.html">Download</a> the Beta version/update of Flash Builder 4.5.1. While you&#8217;re at it, download the beta version of <a href="http://labs.adobe.com/technologies/flashplatformruntimes/air3/">Adobe AIR 3.</a>)</p>
<p>1. Open a New > New ActionScript Mobile Project (Figure 2)<br />
<div id="attachment_6207" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; Open a New ActionScript Mobile Project &lt;/em&gt;" title="mobile1" width="500" height="417" class="size-full wp-image-6207" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> Open a New ActionScript Mobile Project </em></p></div></p>
<p>2.  Specify your target platform as Apple iOS with Automatically reorient selected and click Next. (Figure 3)<br />
<div id="attachment_6208" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile2.png" alt="&lt;em&gt;&lt;strong&gt;Figure3: &lt;/strong&gt; Mobile Settings &lt;/em&gt;" title="mobile2" width="500" height="463" class="size-full wp-image-6208" /><p class="wp-caption-text"><em><strong>Figure3: </strong> Mobile Settings </em></p></div></p>
<p>3. Click the Platform Setting and select Platform and Target devices (Figure 4)<br />
<div id="attachment_6210" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile3.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: &lt;/strong&gt; Specify Platformsettings &lt;/em&gt;" title="mobile3" width="500" height="463" class="size-full wp-image-6210" /><p class="wp-caption-text"><em><strong>Figure 4: </strong> Specify Platformsettings </em></p></div></p>
<p>4. Build Paths provides the opportunity to add SWC folders or a project. Figure 5 shows using the default settings.<br />
<div id="attachment_6212" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile4.png" alt="&lt;em&gt;&lt;strong&gt;Figure 5: &lt;/strong&gt; Include additional or different paths if requires &lt;/em&gt;" title="mobile4" width="500" height="616" class="size-full wp-image-6212" /><p class="wp-caption-text"><em><strong>Figure 5: </strong> Include additional or different paths if requires </em></p></div></p>
<p>5. The next step in creating your app will not come until you are finished coding it all. Then when you press the green arrow (Run) button at the top, you will begin the Run Configurations sequence. Figure 6 shows the On device radio button selected. You are likely to see some red circles with white &#8216;X&#8217;s in them, but don&#8217;t worry.  If there&#8217;s one next to the Packaging setting click the [Configure] link.<br />
<div id="attachment_6213" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile5.png" alt="&lt;em&gt;&lt;strong&gt;Figure 6: &lt;/strong&gt; Run Configurations &lt;/em&gt;" title="mobile5" width="500" height="354" class="size-full wp-image-6213" /><p class="wp-caption-text"><em><strong>Figure 6: </strong> Run Configurations </em></p></div></p>
<p>6. Set the Certificate and Provisioning file. Both of these should be ready using the software that the Apple iOS Developer program provides. Use the Browse buttons to locate yours. (These cannot be generated using Adobe software.)<br />
<div id="attachment_6216" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile6.png" alt="&lt;em&gt;&lt;strong&gt;Figure 7: &lt;/strong&gt; Setting Certificate and Provisioning files &lt;/em&gt;" title="mobile6" width="500" height="301" class="size-full wp-image-6216" /><p class="wp-caption-text"><em><strong>Figure 7: </strong> Setting Certificate and Provisioning files </em></p></div></p>
<p>Once the certificate and provisioning file are set, click the Apply button and then click OK.</p>
<p>7. If all has gone right you will see the Packaging Completed window shown in Figure 8. At this point you will have to open iTunes on your computer, and make sure that your mobile device is connected to your computer.  You will need to locate the .ipa package and drop it into the iTunes library. It&#8217;s easiest to just click the (Reveal package in Finder) to get the .ipa package.<br />
<div id="attachment_6218" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile7.png" alt="&lt;em&gt;&lt;strong&gt;Figure 8: &lt;/strong&gt; Completed Window &lt;/em&gt;" title="mobile7" width="500" height="177" class="size-full wp-image-6218" /><p class="wp-caption-text"><em><strong>Figure 8: </strong> Completed Window </em></p></div></p>
<p>8. Figure 9 shows the bin-debug folder opened with the .ipa package. Be sure that the Apps window on your iTunes library is selected and <strong>drag the .ipa package into the iTunes library</strong>.<br />
<div id="attachment_6219" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile8.png" alt="&lt;em&gt;&lt;strong&gt;Figure 9 &lt;/strong&gt; Drag .ipa package into iTunes Apps Library &lt;/em&gt;" title="mobile8" width="500" height="463" class="size-full wp-image-6219" /><p class="wp-caption-text"><em><strong>Figure 9 </strong> Drag .ipa package into iTunes Apps Library </em></p></div></p>
<p>9. The final step requires that you select the connected device (shown right above Little Richard Greatest Songs in Figure 10 ) and then find your new app in the Sync Apps window. Be sure to check the box next to the app. Then click the Apply button. Clicking the Apply button will then begin the Sync process. Once it is finished you should see the icon.<br />
<div id="attachment_6221" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile9.png" alt="&lt;em&gt;&lt;strong&gt;Figure 10: &lt;/strong&gt;Adding the app to an iOS device&lt;/em&gt;" title="mobile9" width="500" height="452" class="size-full wp-image-6221" /><p class="wp-caption-text"><em><strong>Figure 10: </strong>Adding the app to an iOS device</em></p></div><br />
When you click the icon on your iOS mobile device, you will see the image shown at the top of this post. If you turn your device, the object will re-orient itself to the object&#8217;s orientation.</p>
<p><strong>Adding the State Design</strong></p>
<p>With this and the previous posts for mobile devices, you&#8217;ve seen that design patterns work well with mobile app development. All of the advantages found in design pattern development for computers can be found in creating applications for mobile devices.</p>
<p>In the next post, this process will be continued by adding a State pattern to the app to create a game. If you don&#8217;t have a mobile device, you can use the same coding for making the app for your computer.</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%2F2011%2F09%2Factionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state%2F&amp;title=ActionScript%203.0%20Flash%20Builder%20App%20in%20Apple%20iOS%3A%20Decorator%20and%20State" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/09/actionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Video Game State Machine:No Conditionals Part III</title>
		<link>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-iii/</link>
		<comments>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-iii/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 14:07:41 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5792</guid>
		<description><![CDATA[State Design Pattern Game Is the State Pattern the way to go with a game based on videos? First, we have to ask, what varies? In this case, different places on the grid represent different situations in a game environment and depicted in a video. (Actually, any environment that requires unique states presented in videos&#8211;such [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5795" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/stateVideoGame.png" alt="State Video Game!" title="stateVideoGame" width="250" height="375" class="size-full wp-image-5795" /><p class="wp-caption-text">State Video Game!</p></div><strong>State Design Pattern Game</strong></p>
<p>Is the State Pattern the way to go with a game based on videos? First, we have to ask, <em>what varies</em>? In this case, different places on the grid represent different situations in a game environment and depicted in a video. (Actually, any environment that requires unique states presented in videos&#8211;such as a guided tour of a museum&#8211;would be ). Simply put, the videos vary and each represents a different state. So the answer to the first question is, <em>yes</em>, the State pattern is appropriate.</p>
<p>Second, we must ask, a<em>re updates and changes easy with this application of the pattern</em>? In this particular case, I tested several different videos with this applications, and it&#8217;s very easy to swap and change videos. In fact, if I want to use .SWF files for animation instead of videos, that too would be a cinch.</p>
<p>Third, we need to consider the code generated. <em>Will using the State pattern generate more code and more work than other patterns or coding strategies</em>? This may be the Achilles&#8217; heel of the pattern used as a game generation design. Consider the following:</p>
<ol>
<li>Each grid cell is a state</li>
<li>A 19 x 19 grid generates 361 cells</li>
<li>Each cell needs a State class</li>
<li>The grid requires 361 State classes</li>
<li>The Context class requires getter methods for each class</li>
<li>The Context class generates at least 1,444 lines of code (assuming each method takes up 4 lines)</li>
<li>The Context class constructor function requires an additional  361 statements to instantiate instances of each  state</li>
</ol>
<p>If that looks like a lot of code and classes, it is. However, design patterns were originally created to ease the task of <em>very large programs</em>, and just about any way you work it out, 361 different states (video shots) is going to be difficult to keep track of. The biggest problem I can see in this design is in the Context class. The Context keeps track of what state the user is currently in. When states change (and you&#8217;ve got 361 of them), you have to include a method in the Context for changing states. There&#8217;s where you end up with a very big class. Before going on, though try playing the game and download the files using the buttons below: (By the way, this time there&#8217;s a surprise&#8230;see if you can find it in the play..be careful, it&#8217;ll eat you.)<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/stategame/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<blockquote><p>*<strong>Reminder</strong>: I&#8217;m putting all of the downloads into a general download site stored under &#8220;ActionScript 3.0&#8243;; so, just click on the latest download and you&#8217;ll get all of the files, including the videos and even the Excel spreadsheet with the direction grid.</p></blockquote>
<p><span id="more-5792"></span></p>
<p><strong>The Game Map So Far</strong></p>
<p>To see what&#8217;s going on, I&#8217;ve created a &#8220;game map&#8221; of the files in this State and where you can navigate to. All of the videos were made with a Flip Mino HD camera and then &#8220;cartoonized&#8221; in Adobe After Effects and rendered in Adobe Premier. Keep in mind that this is a prototype work-up for a full-fledged game. Figure 1 shows how far the game is at this point. The spiral icon between <strong>East4</strong> and <strong>East5</strong> indicates a &#8220;turnaround.&#8221; That is, when in the <strong>East5</strong> state, if the West button is pressed, the video does a turnaround and heads West to the state <strong>East4</strong>. (Give it a try.) Figure 1 shows the map:<br />
<div id="attachment_6011" class="wp-caption alignnone" style="width: 508px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/map1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Map of the States&lt;/em&gt;" title="map1" width="498" height="650" class="size-full wp-image-6011" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Map of the States</em></p></div></p>
<p>For those of you who work with games and user-guided realities with video, I&#8217;d like to get any feedback you may have. Other than one little conditional statement in the Distributor class, this application is wholly free of conditional statements. The big deal about the lack of conditional statements is that nothing is bound by or entangled in a conditional. That means if you want to change the states or the context, you can do so without having to keep track of any conditional statement.</p>
<p>The drawback of using a State design pattern like this is that it generates a lot of classes. As you can see in the Context class, each state is instantiated in an object, and so a lot of objects are floating around. However, with some tweeks here and there, I believe you&#8217;d be able to create and remove state objects dynamically, and like the video, you&#8217;d just be using one instance at a time.</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%2F2011%2F07%2Fvideo-game-state-machineno-conditionals-part-iii%2F&amp;title=Video%20Game%20State%20Machine%3ANo%20Conditionals%20Part%20III" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-iii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Video Game State Machine:No Conditionals Part II.5</title>
		<link>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii-5/</link>
		<comments>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii-5/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 14:27:04 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5952</guid>
		<description><![CDATA[A Dot Release In our previous post on the State Design Pattern, I wanted to have a nice clear example, but in the process, too many parts were left out, and in some respects it was more complicated with the use of the helper class (Distributor) than it had to be. As a result, the [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5951" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/sucked.png" alt="A Better State is at Hand" title="sucked" width="250" height="352" class="size-full wp-image-5951" /><p class="wp-caption-text">A Better State is at Hand</p></div><strong>A Dot Release</strong></p>
<p>In our previous post on the State Design Pattern, I wanted to have a nice clear example, but in the process, too many parts were left out, and in some respects it was more complicated with the use of the helper class (<strong>Distributor</strong>) than it had to be. As a result, the design generated multiple instances of the Distributor class and each instance was difficult to control—not to mention the fact that each new instance overloaded the program with unneeded instances. As a result, I decided to simplify the video process by creating a single instance of the Distributor in the Client and just pass the name of the requested video to be played through the same instance using the Distributor&#8217;s methods. So instead of having each state return a Sprite instance, they now return a String instance used by the single Distributor instantiated in the Client. The following buttons provide the new files to download and running example:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/statevid2/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<blockquote><p><strong>*Note about Downloads</strong>: I&#8217;m in the process of moving all of the downloads to the MWDD site at the University of Hartford. It&#8217;s still being set up, and I&#8217;m working on a way to provide more information about each of the downloads. All of the downloads for this blog will be under ActionScript 3.0/Flash (which includes Flash Builder/Flex).</p></blockquote>
<p>Secondly, the first example just included two states—one for starting and one for going one step to the east. That helped keep things simple, but it didn&#8217;t do much for seeing how the State design pattern can handle the North-East-West-South (NEWS) directions. Figure 1 provides a statechart that shows an NEWS state machine with an additional state to the east. It provides a better idea of where the State pattern can take us.<br />
<div id="attachment_5955" class="wp-caption alignnone" style="width: 504px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/statechart.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Statechart of Direction Relative States&lt;/em&gt;" title="statechart" width="494" height="285" class="size-full wp-image-5955" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Statechart of Direction Relative States</em></p></div></p>
<p>The statechart shows movement between states and the kinds of transitions relative to a starting position.<br />
<span id="more-5952"></span><br />
<strong>A State Map</strong></p>
<p>Basically, that State pattern is used to map different directional states. Each state has its own class, and because each state is &#8220;self-aware&#8221; in that it knows that it can only go to certain other states, no matter where you are in a map matrix, all of the possible choices of movement are available. In order to create a map, I had to set up a plan for naming each state relative to a starting position. I began with a 19 x 19 spreadsheet. This generated 361 cells (or states) which would mean a total of 361 classes.  Figure 2 shows a portion of the spreadsheet. The yellow cells represent the zero axis of the NEWS positions relative to the starting point. Each cell off of the central axis is named as an intersection between North and South and the East or West state relative to the Start start state:<br />
<div id="attachment_5959" class="wp-caption alignright" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/StateMatrix.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt;State matrix naming map" title="StateMatrix" width="250" height="105" class="size-full wp-image-5959" /><p class="wp-caption-text"><em><strong>Figure 2: </strong>State matrix naming map</em></p></div> The first two characters in each cell represent the north or south values in terms of how far the cell is north or south of the Start cell, and the second two characters represent the number of cells (states) the cell is east or west of the Start cell. So N2E1 is the state that is two north and one east of the Start cell.</p>
<p><strong>New State Interface and State Classes</strong></p>
<p>This new state pattern is still incomplete because it doesn&#8217;t go beyond the initial first steps along the zero axis of north/south and east/west. However, the State pattern will need new interface methods to move in the NEWS directions and move to the Start state. We employ the following interface:</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('p5952code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p595212"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p5952code12"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> goEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #000000; font-weight: bold;">function</span> goWest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #000000; font-weight: bold;">function</span> goNorth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;
		<span style="color: #000000; font-weight: bold;">function</span> goSouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All of the return types are now <strong>String</strong> instead of <strong>void</strong>. So now, all of the concrete states will return a string. This string will call up the video associated with movement  to the appropriate requested cell (state).</p>
<p>I&#8217;ve been giving second thoughts to including a <strong>goStart()</strong> method. Having a start state is important, but it should be just like the other states—the player arrives there by navigating in the NEWS directions. However, I&#8217;m still trying to figure out a way to make it the default starting point without using the <strong>goStart()</strong> method; so for now it stays.</p>
<p>Since the <strong>IState</strong> interface has changed; so too do the classes that implement it. The East1 state now has all five methods. However, a lot of the material is commented out because only a limited set of directional states and videos have been created at this time. So the commented out <strong>dir</strong> values are the actual values that will be used once the videos and the states are completed.</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('p5952code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p595213"><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
</pre></td><td class="code" id="p5952code13"><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: #808080; font-style: italic;">//East1</span>
	<span style="color: #000000; font-weight: bold;">class</span> East1State <span style="color: #0066CC;">implements</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> East1State<span style="color: #66cc66;">&#40;</span>context:Context<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;--East1--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<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>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Going east would require entering into a new state</span>
			<span style="color: #808080; font-style: italic;">//Relative to the starting state, the second eastward</span>
			<span style="color: #808080; font-style: italic;">//move is &quot;east2&quot;;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;heading east&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//dir=&quot;east2&quot;;</span>
			dir = <span style="color: #ff0000;">&quot;east1&quot;</span>;
			<span style="color: #b1b100;">return</span> dir;
			<span style="color: #808080; font-style: italic;">//The following would have to be first built</span>
			<span style="color: #808080; font-style: italic;">//context.setState(context.getEast2State());</span>
		<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>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			dir = <span style="color: #ff0000;">&quot;intro&quot;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Go West&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStartState</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> dir;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<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>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//dir = &quot;N1E1&quot;;</span>
			dir = <span style="color: #ff0000;">&quot;north1&quot;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Going north&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//context.setState(context.getN1E1State());</span>
			<span style="color: #b1b100;">return</span> dir;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<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>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//dir = &quot;S1E1&quot;;</span>
			dir = <span style="color: #ff0000;">&quot;south1&quot;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Going south&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//context.setState(context.getS1E1State());</span>
			<span style="color: #b1b100;">return</span> dir;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			dir = <span style="color: #ff0000;">&quot;intro&quot;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Go to starting point&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStartState</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> dir;
		<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 the next (and last) installment in this series next week, I hope to have more done to create a little D&#038;D type game with videos.  This intermediate post will save time and space for the final installment as far as explaining the matrix.</p>
<p><strong>The Video Playing Class: Distributor</strong></p>
<p>The <strong>Distributor</strong> helper class provides the structure for playing progressive downloads and is now instantiated directly in the  Client and has methods available for a cleaner video playback. Also, in the metadata handler, I added a code for sizing the video player to the size of the current video. Since I&#8217;ve stated to use <em>Adobe After Effects</em> with <em>Adobe Premier</em>, to add cartoon effects to the video, I&#8217;ve been having sizing problems. (Suffice it to say, there&#8217;s a learning curve going on.)  The following shows the new <strong>Distributor</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('p5952code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p595214"><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
</pre></td><td class="code" id="p5952code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//This is a 'helper class'. A helper class is one that is not a 'participant' of the</span>
	<span style="color: #808080; font-style: italic;">//Design Pattern, but it performs a task for one or more of the participants.</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</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;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Distributor <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> nc:<span style="color: #0066CC;">NetConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</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> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</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> hookup:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sniffer:<span style="color: #0066CC;">Object</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><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> Distributor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			nc.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,doConnect<span style="color: #66cc66;">&#41;</span>;
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">137.5</span>,vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">80</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doConnect<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			hookup=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>hookup<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #006600;">client</span> = sniffer;
				sniffer.<span style="color: #006600;">onMetaData</span> = getMeta;
				vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> playVid<span style="color: #66cc66;">&#40;</span>vidName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			dir = <span style="color: #ff0000;">&quot;vids/&quot;</span> + vidName + <span style="color: #ff0000;">&quot;.f4v&quot;</span>;
			<span style="color: #808080; font-style: italic;">//dir=&quot;vids/&quot;+vidName+&quot;.flv&quot;;</span>
			ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>dir<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopHere<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>vid<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> getMeta<span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			vid.<span style="color: #0066CC;">height</span> = mdata.<span style="color: #0066CC;">height</span>;
			vid.<span style="color: #0066CC;">width</span> = mdata.<span style="color: #0066CC;">width</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 <strong>playVid(vidName:String)</strong> method expects a string naming the video to be played (minus the extension). The state methods provide that string to be used in the Client&#8217;s instance of the Distributor class.</p>
<p><strong>The Context and Client</strong></p>
<p>All that&#8217;s left are the Context and Client classes. While the State classes stay the same as far as size is concerned, the Context class grows with each new state to handle.  The reason that the Context class keeps growing with each new state is that it must keep track of the current state. In the State design pattern, the Context is where you can generate a lot of code.  The full 19 x 19 matrix would generate over 1000 lines of code.  There may be a better way of keeping track of the states, but for now, the following Context class reflects the current condition of the 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('p5952code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p595215"><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
</pre></td><td class="code" id="p5952code15"><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> east1State:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> west1State:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> north1State:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> south1State:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> startState:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateNow:IState;
&nbsp;
		<span style="color: #0066CC;">public</span> <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>;
			startState = <span style="color: #000000; font-weight: bold;">new</span> StartState<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			east1State = <span style="color: #000000; font-weight: bold;">new</span> East1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			west1State = <span style="color: #000000; font-weight: bold;">new</span> West1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			north1State = <span style="color: #000000; font-weight: bold;">new</span> North1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			south1State = <span style="color: #000000; font-weight: bold;">new</span> South1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			stateNow = east1State;
		<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//Directional methods</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow.<span style="color: #006600;">goStart</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> <span style="color: #000000; font-weight: bold;">function</span> doEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> 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> <span style="color: #000000; font-weight: bold;">function</span> doWest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> 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> <span style="color: #000000; font-weight: bold;">function</span> doNorth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> 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> <span style="color: #000000; font-weight: bold;">function</span> doSouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> 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>
<span style="color: #808080; font-style: italic;">//Setter for current state</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>stateNow:IState<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A new state is set&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateNow</span> = stateNow;
		<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//Getters for each concrete state</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStartState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">startState</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEast1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">east1State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getWest1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">west1State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getNorth1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">north1State</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getSouth1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">south1State</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>At this stage of development, the Context only allows movement  to the first step away from the starting point. Each concrete state class requires a method to specify each of the interface directions (e.g., <strong>doEast()</strong>) and a getter method for a specific state (e.g., <strong>getEast1State()</strong>). While the directional methods will not change, each concrete state class requires a new getter method.</p>
<p>Finally, the Client sets up the UI and uses information from the concrete states to play different videos. It follows the same format as the original, but it now instantiates an instance of the Distributor 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('p5952code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p595216"><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
</pre></td><td class="code" id="p5952code16"><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> <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> newsState: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> eastBtn:<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> westBtn:<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> northBtn:<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> southBtn:<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: #000000; font-weight: bold;">var</span> startBtn:<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> dis:Distributor=<span style="color: #000000; font-weight: bold;">new</span> Distributor<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> vidNow:<span style="color: #0066CC;">String</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: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>dis<span style="color: #66cc66;">&#41;</span>;
			vidNow=newsState.<span style="color: #006600;">doStart</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dis.<span style="color: #006600;">playVid</span><span style="color: #66cc66;">&#40;</span>vidNow<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			eastBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveEast<span style="color: #66cc66;">&#41;</span>;
			eastBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">500</span>,eastBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">200</span>;
			eastBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;East&quot;</span>,eastBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>eastBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			westBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveWest<span style="color: #66cc66;">&#41;</span>;
			westBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,westBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">200</span>;
			westBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;West&quot;</span>,westBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>westBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			northBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveNorth<span style="color: #66cc66;">&#41;</span>;
			northBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">260</span>,northBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">5</span>;
			northBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;North&quot;</span>,northBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>northBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			southBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveSouth<span style="color: #66cc66;">&#41;</span>;
			southBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">260</span>,southBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">370</span>;
			southBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;South&quot;</span>,southBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>southBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			startBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveStart<span style="color: #66cc66;">&#41;</span>;
			startBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,startBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">20</span>;
			startBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Start&quot;</span>,startBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>startBtn<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> moveEast<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>
			dis.<span style="color: #006600;">stopHere</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dis.<span style="color: #006600;">playVid</span><span style="color: #66cc66;">&#40;</span>newsState.<span style="color: #006600;">doEast</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><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> moveNorth<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>
			dis.<span style="color: #006600;">stopHere</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dis.<span style="color: #006600;">playVid</span><span style="color: #66cc66;">&#40;</span>newsState.<span style="color: #006600;">doNorth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> moveWest<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>
			dis.<span style="color: #006600;">stopHere</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dis.<span style="color: #006600;">playVid</span><span style="color: #66cc66;">&#40;</span>newsState.<span style="color: #006600;">doWest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><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> moveSouth<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>
			dis.<span style="color: #006600;">stopHere</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dis.<span style="color: #006600;">playVid</span><span style="color: #66cc66;">&#40;</span>newsState.<span style="color: #006600;">doSouth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><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> moveStart<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>
			dis.<span style="color: #006600;">stopHere</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			dis.<span style="color: #006600;">playVid</span><span style="color: #66cc66;">&#40;</span>newsState.<span style="color: #006600;">doStart</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the next post I hope to have the beginnings of a little navigation through a video maze and a surprise to gobble up a player.</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%2F2011%2F07%2Fvideo-game-state-machineno-conditionals-part-ii-5%2F&amp;title=Video%20Game%20State%20Machine%3ANo%20Conditionals%20Part%20II.5" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Video Game State Machine:No Conditionals Part II</title>
		<link>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii/</link>
		<comments>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 19:56:04 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5870</guid>
		<description><![CDATA[Help for OOP/DP Beginners: Rewind the State Pattern I started working on the State design pattern, and I realized how easy it is to quickly get lost. The idea of a state machine is pretty clear and simple, and so is the State Design Pattern. However, I find that if I&#8217;m away from it for [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5869" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/stateStrong1.png" alt="Starting State Design Patterns" title="stateStrong" width="250" height="352" class="size-full wp-image-5869" /><p class="wp-caption-text">Starting State Design Patterns</p></div><strong>Help for OOP/DP Beginners: Rewind the State Pattern</strong></p>
<p>I started working on the State design pattern, and I realized how easy it is to quickly get lost. The idea of a state machine is pretty clear and simple, and so is the State Design Pattern. However, I find that if I&#8217;m away from it for any length of time, it gets all tangled up again—like fishing line in a well-used tackle box. So, in this inserted post between Part I and Part III of the Video Game Machine series of posts, this one is for beginners in the State design pattern.</p>
<p>The two examples used are simple, but both have to get through the tangle of the requests from the Client through the Context requests and on to the concrete states via the State interface. The first example is wholly abstract but really simple.  Play and download buttons provide access to get started:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/statevid/statevid.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignleft size-full wp-image-2020" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/blog/statevid/MinVidState/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p><strong>Overview</strong></p>
<p>Figure 1 shows the State class diagram and the ActionScript 3.0 files so that you can easily see the implementation of the pattern. The Client makes a request for a given state through the Context. The Context checks the current state (think <strong>lights on</strong>; <strong>lights off</strong> as two distinctive states) and then requests the action from the current state. So, if current state is <strong>lights on</strong> and the request is to turn on the lights, it wouldn&#8217;t do anything because the state already exists.<br />
<div id="attachment_5878" class="wp-caption alignnone" style="width: 509px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/StateDP.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Class and File Diagrams of State Design Pattern&lt;/em&gt;" title="StateDP" width="499" height="447" class="size-full wp-image-5878" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Class and File Diagrams of State Design Pattern</em></p></div></p>
<p>Rather than turning the lights on or off, this example starts with a &#8220;Starting&#8221; state and a state known as &#8220;East1.&#8221;  So the user is either in the <strong>Start State</strong> or the <strong>East1 State</strong>. These two states will be abstract with <strong>trace()</strong> statements in this first example that helps to see what&#8217;s going on, but in the second app in this post, we&#8217;ll use videos. Once the State design pattern is established, <em>it&#8217;s very easy to add and change objects without crashing the application</em>. That&#8217;s what makes the State pattern so valuable; a little extra work at the outset pays huge dividends in the long run.</p>
<p><strong>The Participants</strong></p>
<p>The State design pattern deals with variation in an object&#8217;s state. An adventure game can be treated as a series of states that the player enters and leaves. Depending on the state (situation) different things are possible. The state can change from the home base, to the wizard&#8217;s fortress to the jungle to an underground cavern maze, into and out of a worm hole, a parallel universe or anything the developer&#8217;s mind conjures.</p>
<p>The most important work in the State pattern is Context class. It keeps track of the current state to determine how to handle a request. Imagine three states known as:</p>
<ul>
<li><strong>State 1</strong></li>
<li><strong>State 2</strong></li>
<li><strong>State 3</strong></li>
</ul>
<p>State 1 can only remain in its current state or go to State 2. It cannot go directly to State 3. So if the Client requests State 3, the Context realizes that the current state is State 1 and so makes the call to State 1 where the options available in that state do not include a move to State 2.</p>
<p>The determinations are not made using conditional statements. Rather, as each state is called, it reports to the Context establishing itself as the current state. As a State pattern application grows in size, it must generate a separate concrete state for each new situation and possible changes in state. The State interface requires a new method for accessing the new states, and the concrete states must include that method in their structure—even if the method results as a null one. For example, in the State 1 concrete state class, the method to go to State 3 does nothing.<br />
<span id="more-5870"></span><br />
<strong>Leaving the Nest and Returning</strong></p>
<p> <div id="attachment_5922" class="wp-caption alignright" style="width: 195px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/startstate.png" alt="&lt;em&gt;&lt;strong&gt;Figure XX:&lt;/strong&gt; Basic State Transitions&lt;/em&gt;" title="startstate" width="185" height="119" class="size-full wp-image-5922" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Basic State Transitions</em></p></div>This first State example attempts to reveal the structure of the State design pattern. The statechart in Figure 2 shows that the starting state is &#8220;Start&#8221; and the other state is &#8220;East1.&#8221; (The blue arrow with the dot on the tail is the default starting point for the state machine.) The first example shows this most basic State design pattern. Two concrete states either stay put or move to the other state. When a request is made, trace statements show what is happening. To begin, we show the IState interface and the two ConcreteStates. Figure 2 shows the key state transitions for the following states:</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('p5870code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587024"><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
</pre></td><td class="code" id="p5870code24"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//State Interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> goEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
---------------
<span style="color: #808080; font-style: italic;">//Concrete State 1</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Starting State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> StartState <span style="color: #0066CC;">implements</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StartState<span style="color: #66cc66;">&#40;</span>context:Context<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;--Start State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<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>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Heading east&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getEast1State</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You're already at start&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>
---------------
<span style="color: #808080; font-style: italic;">//Concrete State 2</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//East1</span>
	<span style="color: #000000; font-weight: bold;">class</span> East1State <span style="color: #0066CC;">implements</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> East1State<span style="color: #66cc66;">&#40;</span>context:Context<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;--East1--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<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>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Heading East&quot;</span><span style="color: #66cc66;">&#41;</span>;
                       <span style="color: #808080; font-style: italic;">//add code for the next eastward movement (e.g., East2)</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Go to starting point&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStartState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In looking at the IState interface, you can see that two methods (<strong>goEast()</strong> and <strong>goStart()</strong>) must be implemented by all classes that implement the interface. As you can see, the two classes do that, but they are implemented in very different ways depending on which state is the current one. If the current state is East1, the <strong>goEast1()</strong> informs the user that he/she is heading east (for the next eastward state from the Start state). In the same state, the <strong>goStart()</strong> method informs the user that the program is heading to the starting point. More importantly though is the line:</p>
<p>     <strong>context.setState(context.getStartState());</strong></p>
<p>As soon as the method is launched, it changes the current state to the next state. That means that the target state is all set for the next request as soon as it is invoked. Further, note that all state constructor functions include a Context instance.</p>
<blockquote><p>Setting the current state in the Context is the key to understanding and using the State Design Pattern.</p></blockquote>
<p>Of course the methods can do much more, and in more complex states all kinds of different properties and additional methods may be added.</p>
<p><strong>The Context and Client</strong></p>
<p>Now that you&#8217;ve seen the two states, take a look at the request mechanisms in the Context and Client classes. First, take a look at the all-important context 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('p5870code25'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587025"><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
</pre></td><td class="code" id="p5870code25"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Context Class</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Context class</span>
	<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> east1State:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> startState:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateNow:IState;
&nbsp;
		<span style="color: #0066CC;">public</span> <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>;
			startState = <span style="color: #000000; font-weight: bold;">new</span> StartState<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			east1State = <span style="color: #000000; font-weight: bold;">new</span> East1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stateNow=startState;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">goStart</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> <span style="color: #000000; font-weight: bold;">function</span> doEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</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> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>stateNow:IState<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A new state is set&quot;</span><span style="color: #66cc66;">&#41;</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> <span style="color: #000000; font-weight: bold;">function</span> getStartState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">startState</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEast1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">east1State</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 Context first initializes instances of each state and a variable to hold the current state. Note that all three are initialized as interface types (IState). This allows for loose binding because the instances are not bound to a concrete class. (Remember the principle: <em>Program to the interface; not the implementation</em>.)</p>
<p>The two functions that call the state classes are essentially requests. The Client&#8217;s requests are passed through the Context&#8217;s methods that make requests through the current contextual state (<strong>stateNow</strong>).</p>
<p>The final three methods are getters and setters. The setter is called from the concrete states to establish the next (called) current state. Each state includes a context getter to the called state. As you can see in the two concrete states, the setter is set with the getter functions. (<strong>getEast1State()</strong>; <strong>getStartState()</strong>.)</p>
<p>To initiate the process, you need a Client class. This one includes buttons for firing off selected states, but if you want, you can have the requests in the constructor function fired off directly without the buttons.</p>
<p>In any case, the requests are through the Context and even though the requests are for different states, the Context makes the requests to the concrete states through the state interface (IState.)</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('p5870code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587026"><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="p5870code26"><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: #808080; font-style: italic;">//Test the State design pattern</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> <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> twoState: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> eastBtn:<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> startBtn:<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;">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: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			eastBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveEast<span style="color: #66cc66;">&#41;</span>;
			eastBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">500</span>,eastBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">200</span>,eastBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">40</span>;
			eastBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;East&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>eastBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			startBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveStart<span style="color: #66cc66;">&#41;</span>;
			startBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,startBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			startBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Start&quot;</span>,startBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>startBtn<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> moveEast<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>
			twoState.<span style="color: #006600;">doEast</span><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> <span style="color: #000000; font-weight: bold;">function</span> moveStart<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>
			twoState.<span style="color: #006600;">doStart</span><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 requests are made through the context and context methods using the format:</p>
<p>     <strong>Context.doWhatever();</strong></p>
<p>such as,</p>
<p>     <strong>twoState.doEast();</strong></p>
<p>So basically, all the Client has to do is to request the implied concrete state through the context, and the results will be in terms of the current context that is self-aware. The Client doesn&#8217;t have to even think about it. It makes the requests, and the Context and states take care of the details.</p>
<p>Play with this first example for a while to see the information that is coming back in the <strong>trace()</strong> statements. It will keep you enthralled of a minute or two, but still it&#8217;s important to understand.</p>
<p><strong>Getting Started on a Video State Game</strong></p>
<p>Once you get the basics down, let&#8217;s move on to an example that is a starting point for the State Video Game. Originally when the video game was introduced in <a href="http://www.as3dp.com/2011/06/19/what-if-actionscript-3-0-had-no-conditional-statements-part-i/">Part I</a> of this series, each direction had its own class made up of a class to play a video (North, East, West and South—NEWS). Using a State design pattern, each concrete state will call a video. However, instead of having separate classes for each video, the concrete state will just contain a string used to specify which video will play. In the context of design patterns, the classes that are not participants in the pattern but are used to perform an oft-repeated task are called &#8220;helper classes.&#8221; For this next step, we&#8217;ll use a helper class called <strong>Distributor</strong>. It plays any<strong> f4v</strong> video using progressive download. (The commented out line will do the same for <strong>flv</strong> files.</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('p5870code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587027"><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
</pre></td><td class="code" id="p5870code27"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//This is a 'helper class'. A helper class is one that is not a 'participant' of the</span>
	<span style="color: #808080; font-style: italic;">//Design Pattern, but it performs a task for one or more of the participants.</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</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;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Distributor <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> nc:<span style="color: #0066CC;">NetConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</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> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</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> hookup:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sniffer:<span style="color: #0066CC;">Object</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><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> Distributor<span style="color: #66cc66;">&#40;</span>vidName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			dir=<span style="color: #ff0000;">&quot;vids/&quot;</span>+vidName+<span style="color: #ff0000;">&quot;.f4v&quot;</span>;
			<span style="color: #808080; font-style: italic;">//dir=&quot;vids/&quot;+vidName+&quot;.flv&quot;;</span>
			nc.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,doConnect<span style="color: #66cc66;">&#41;</span>;
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">137.5</span>, vid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">80</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doConnect<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			hookup=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>hookup<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #006600;">client</span>=sniffer;
				sniffer.<span style="color: #006600;">onMetaData</span>=getMeta;
				vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>dir<span style="color: #66cc66;">&#41;</span>;
				addChild<span style="color: #66cc66;">&#40;</span>vid<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> <span style="color: #000000; font-weight: bold;">function</span> getMeta <span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Use if metadata required</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopHere<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span><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 string variable <strong>dir</strong> is the file name of the requested video. (Think of <strong>dir</strong> as short for &#8220;<strong>dir</strong>ection.&#8221;) It includes a stop method (the <strong>stopHere()</strong> function), but it has not been implemented in the rest of the application yet. As a result, the current video will not stop when the next one is selected. In the next (and last) post in this series, that will be included.</p>
<p>Because the states are playing videos, the <strong>IState</strong> methods need to return something other than <strong>void</strong>. Because the Distribution class extends Sprite, we know that we can create a Sprite object and return a variable that instantiates Distribution (and the video it&#8217;s playing) to the Client and the screen for viewing. So, first of all, we&#8217;ll look at the<strong> IState</strong> interface and its concrete implementations.</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('p5870code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587028"><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
</pre></td><td class="code" id="p5870code28"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//State Interface</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: #0066CC;">interface</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> goEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite;
		<span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
---------------------
<span style="color: #808080; font-style: italic;">//Start State</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>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Starting State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> StartState <span style="color: #0066CC;">implements</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidNow:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nullSprite: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> StartState<span style="color: #66cc66;">&#40;</span>context:Context<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;--Start State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<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>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Heading east&quot;</span><span style="color: #66cc66;">&#41;</span>;
			vidNow = <span style="color: #000000; font-weight: bold;">new</span> Distributor<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;east1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getEast1State</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> vidNow;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You're already at start&quot;</span><span style="color: #66cc66;">&#41;</span>;
			vidNow = nullSprite;
			<span style="color: #b1b100;">return</span> vidNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
---------------------
<span style="color: #808080; font-style: italic;">//East1 State</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>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//East</span>
	<span style="color: #000000; font-weight: bold;">class</span> East1State <span style="color: #0066CC;">implements</span> IState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidNow:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nullSprite: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> East1State<span style="color: #66cc66;">&#40;</span>context:Context<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;--East1--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span> = context;
		<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>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Going east would require entering into a new state</span>
			<span style="color: #808080; font-style: italic;">//Relative to the starting state, the second eastward</span>
			<span style="color: #808080; font-style: italic;">//move is &quot;east2&quot;;</span>
			<span style="color: #808080; font-style: italic;">//vidNow = new Distributor(&quot;east2&quot;);</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;heading east&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">return</span> nullSprite;
			<span style="color: #808080; font-style: italic;">//The following would have to be first built</span>
			<span style="color: #808080; font-style: italic;">//context.setState(context.getEast2State());</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			vidNow = <span style="color: #000000; font-weight: bold;">new</span> Distributor<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;intro&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Go to starting point&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStartState</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> vidNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Because the return is expecting a Sprite, I created a &#8220;null sprite.&#8221; It&#8217;s simply an instantiated Sprite object with no content. The <strong>return null</strong> worked, but it threw an error, and a quick fix without adding error handlers was to create an &#8220;empty&#8221; Sprite object.</p>
<p>Next, the Context class is changed little from the original. However, the initial state is set to <strong>East1</strong> and then the constructor class in the <strong>Client</strong> calls the <strong>StartState</strong> to change the current state to the StartState and plays the start video. Otherwise, the Context class is very similar to the original:</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('p5870code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587029"><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="p5870code29"><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: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<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> east1State:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> startState:IState;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateNow:IState;
&nbsp;
		<span style="color: #0066CC;">public</span> <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>;
			startState = <span style="color: #000000; font-weight: bold;">new</span> StartState<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			east1State = <span style="color: #000000; font-weight: bold;">new</span> East1State<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stateNow = east1State;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStart<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow.<span style="color: #006600;">goStart</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> <span style="color: #000000; font-weight: bold;">function</span> doEast<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<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> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>stateNow:IState<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A new state is set&quot;</span><span style="color: #66cc66;">&#41;</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> <span style="color: #000000; font-weight: bold;">function</span> getStartState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">startState</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getEast1State<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IState
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">east1State</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>All of the <strong>trace()</strong> statements have been left in place so you can see what&#8217;s happening under the hood while playing the videos requested.</p>
<p>Finally, the Client class makes requests while adding them to the stage. It too is very similar to the original abstract 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('p5870code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p587030"><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="p5870code30"><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> <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> twoState: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> eastBtn:<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> startBtn:<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;">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: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>twoState.<span style="color: #006600;">doStart</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			eastBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveEast<span style="color: #66cc66;">&#41;</span>;
			eastBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">500</span>,eastBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">200</span>;
			eastBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;East&quot;</span>, eastBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>eastBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			startBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveStart<span style="color: #66cc66;">&#41;</span>;
			startBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,startBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">20</span>;
			startBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Start&quot;</span>,startBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>startBtn<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> moveEast<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>
			addChild<span style="color: #66cc66;">&#40;</span>twoState.<span style="color: #006600;">doEast</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><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> moveStart<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>
			addChild<span style="color: #66cc66;">&#40;</span>twoState.<span style="color: #006600;">doStart</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the constructor function, the statement,</p>
<p><strong>addChild(twoState.doStart());</strong></p>
<p>has been added to kick start the Start video and then set the StartState to the current state. Because the Context was changed to make East1State the current state, this line re-sets the StartState to the current one.</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%2F2011%2F07%2Fvideo-game-state-machineno-conditionals-part-ii%2F&amp;title=Video%20Game%20State%20Machine%3ANo%20Conditionals%20Part%20II" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/07/video-game-state-machineno-conditionals-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What if ActionScript 3.0 had no Conditional Statements? : Part I</title>
		<link>http://www.as3dp.com/2011/06/what-if-actionscript-3-0-had-no-conditional-statements-part-i/</link>
		<comments>http://www.as3dp.com/2011/06/what-if-actionscript-3-0-had-no-conditional-statements-part-i/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 01:44:40 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Loose Coupling]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5762</guid>
		<description><![CDATA[On a couple of occasions, we&#8217;ve discussed working without conditional statements. You may recall that both the Strategy and State design patterns have no conditional statements, and so the idea isn&#8217;t particularly new. The issue came to mind when I was unraveling a PHP program that had nested conditional statements used in pulling out table [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5772" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/noCon.png" alt="Think in Non-Linear Concepts" title="noCon" width="250" height="193" class="size-full wp-image-5772" /><p class="wp-caption-text">Think in Non-Linear Concepts</p></div>On a couple of occasions, we&#8217;ve discussed working without conditional statements. You may recall that both the Strategy and State design patterns have no conditional statements, and so the idea isn&#8217;t particularly new. The issue came to mind when I was unraveling a PHP program that had nested conditional statements used in pulling out table data. Even though they seem to crop up like weeds everywhere, nested conditionals and loops represent poor practices . (Dave Shuck&#8217;s solution for <a href="http://daveshuck.com/2011/03/08/refactoring-avoiding-nested-conditional-statements/">refactoring nested conditionals</a> is worth a look.)</p>
<p>Other than the fact that nested conditionals can create the worst kind of object binding as each condition sends the program further down the binding rabbit hole, we seem to really need them for working out certain kinds of problems. For example, in a visual  game scenario, the user may be faced with choices. Suppose the game state is a crossroad where the player must choose to go North, East, West or South (NEWS). Alternatively, the choice may be video perspectives of different directions. Any one of the choices carries with it a whole set of other states, conditions, capabilities and properties.</p>
<p>One way to handle this kind of choice is with a conditional statement. The following shows two things. First, it shows that when a direction is selected, it typically has more than one thing being affected. (The more than one thing is furnished by &#8220;Everything else.&#8221;) Second, the binding for each is represented by the conditions within each conditional. (You could do the same thing with a Switch statement, but the binding wouldn&#8217;t change.)</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('p5762code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p576234"><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
</pre></td><td class="code" id="p5762code34"><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> NEWS <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> newsArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;north&quot;</span>,<span style="color: #ff0000;">&quot;east&quot;</span>,<span style="color: #ff0000;">&quot;west&quot;</span>,<span style="color: #ff0000;">&quot;south&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">Object</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> NEWS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>dir <span style="color: #b1b100;">in</span> newsArray<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;north&quot;</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;Hellloo Siberia&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;east&quot;</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;Visiting Bejing&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;west&quot;</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;Dude! You made it!&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;south&quot;</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;¡Recepción a la Argentina!&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That certainly can solve the issue of generating different responses from different object properties. It&#8217;s compact and saves ground-fills of wasted bits. It  illustrates a common method of dealing with multiple options. The loop iterates through the object, and each iteration is examined by four conditional statements.</p>
<p><strong>Unconditional Programming</strong></p>
<p>Let&#8217;s go back to the NEWS example, and instead of having trace statements representing different directions, this next example uses short videos pointing in different directions. (I live in the woods so the videos are fairly woodsy—but they show the four points of the compass—honest.) I want to make an app with four buttons that plays a video pointing in the selected direction. Plus, I don&#8217;t want to use any conditional statements. (Easier than you think.) Click the Play and Download buttons to see a working example and get the code and videos.<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/nocon/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/blog/nocon/nocon.zip" arget="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>In the next section where we examine the code you will see two things: No conditional statements and a non-linear solution. The user has several choices, and simply by making a choice she/he goes directly to the object desired—a directional video.<br />
<span id="more-5762"></span></p>
<p><strong>The Client, the UI and Requests</strong></p>
<p>Like any good Client class, this client takes care of the UI and makes requests from other classes. Most of it is just providing a non-conditonal and non-linear solution.</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('p5762code35'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p576235"><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
</pre></td><td class="code" id="p5762code35"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</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;">Sprite</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> 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> dummy: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: #000000; font-weight: bold;">var</span> nBtn:<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> eBtn:<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> wBtn:<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> sBtn:<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> north:North;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> east:East;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> west:West;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> south:South;
&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>
			nBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goNorth<span style="color: #66cc66;">&#41;</span>;
			nBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			nBtn.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">550</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #66cc66;">&#40;</span>nBtn.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>,nBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">10</span>;
			nBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;North&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>nBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			eBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goEast<span style="color: #66cc66;">&#41;</span>;
			eBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			eBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">550</span>-<span style="color: #66cc66;">&#40;</span>eBtn.<span style="color: #0066CC;">width</span>+<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>,eBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">210</span>;
			eBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;East&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>eBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			wBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goWest<span style="color: #66cc66;">&#41;</span>;
			wBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			wBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,wBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">210</span>;
			wBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;West&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>wBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goSouth<span style="color: #66cc66;">&#41;</span>;
			sBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			sBtn.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">550</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #66cc66;">&#40;</span>sBtn.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>,sBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">370</span>;
			sBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;South&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sBtn<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> goNorth<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>
			north=<span style="color: #000000; font-weight: bold;">new</span> North<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>north<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> goEast<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>
			east=<span style="color: #000000; font-weight: bold;">new</span> East<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>east<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> goWest<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>
			west=<span style="color: #000000; font-weight: bold;">new</span> West<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>west<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> goSouth<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>
			south=<span style="color: #000000; font-weight: bold;">new</span> South<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>south<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>As you can see from the Client and testing the app, the Client just fires a function that calls one of four classes that run a video. The user makes requests through the buttons that fire methods that call classes. These classes contain what&#8217;s required for each of the four states. We&#8217;ll use North as a generic example. (The other three classes are identical except for the name of the video they call.)</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('p5762code36'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p576236"><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
</pre></td><td class="code" id="p5762code36"><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;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</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;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> North <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> nc:<span style="color: #0066CC;">NetConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</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> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</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> hookup:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;north.f4v&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sniffer:<span style="color: #0066CC;">Object</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><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> North<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			nc.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,doConnect<span style="color: #66cc66;">&#41;</span>;
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">137.5</span>, vid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">80</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doConnect<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			hookup=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>hookup<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #006600;">client</span>=sniffer;
				sniffer.<span style="color: #006600;">onMetaData</span>=getMeta;
				vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>dir<span style="color: #66cc66;">&#41;</span>;
				addChild<span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getMeta <span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Use if metadata required</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 sharp-eyed among you will have spotted the conditional statement. It is fired by the <strong>NetConnection.Connect </strong>method and is more of a good practice than anything else. I could probably get away without it given that the videos are so small and load quickly; however, it is a good practice to check to make sure that a video is connected. So rather than trying to prove a point at the expense of a good practice, I&#8217;ll allow this one conditional to check connections and preserve a good practice. (I&#8217;ll also be tracking down another way to do the same thing without a conditional.)</p>
<p>You may be thinking that you could do the whole thing in the Client class by stuffing all the required methods and properties into the Client. You&#8217;d be right. In fact, while you&#8217;re at it you could stuff improvements in error checking and remove each video as a new one was played. However, as you put more and more into a class, you cease to <strong>delegate</strong> and continue to bind. Re-use and change becomes difficult because the less delegation you use, the more dependencies you stack up.</p>
<p>Next week, we&#8217;ll see how to refactor this set of classes into a state machine and a State design pattern. The State design pattern has a magic Context participant and other features that you&#8217;ll enjoy when you begin to abandon linear thinking and give OOP thinking a go.</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%2F2011%2F06%2Fwhat-if-actionscript-3-0-had-no-conditional-statements-part-i%2F&amp;title=What%20if%20ActionScript%203.0%20had%20no%20Conditional%20Statements%3F%20%3A%20Part%20I" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/06/what-if-actionscript-3-0-had-no-conditional-statements-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The ActionScript 3.0 Design Pattern Thrill Ride: Part II—Catalyst</title>
		<link>http://www.as3dp.com/2010/03/the-actionscript-3-0-design-pattern-thrill-ride-part-ii-catalyst/</link>
		<comments>http://www.as3dp.com/2010/03/the-actionscript-3-0-design-pattern-thrill-ride-part-ii-catalyst/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 20:09:50 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Pattern Tours]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2892</guid>
		<description><![CDATA[I&#8217;ve been working on a project with Adobe Catalyst, and if I didn&#8217;t do something for a Design Pattern tour now, it would be put off until I don&#8217;t know when. So I put together a non-design pattern application incorporating video and code snippets from the Aid Game. I simply have not had time (given [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2010/02/Thrill.png" alt="Thrill" title="Thrill" width="300" height="144" class="alignleft size-full wp-image-2852" /> I&#8217;ve been working on a project with Adobe Catalyst, and if I didn&#8217;t do something for a Design Pattern tour now, it would be put off until I don&#8217;t know when. So I put together a non-design pattern application incorporating video and code snippets from the <a href="http://www.as3dp.com/2010/02/02/new-aid-game-the-machine-moves/">Aid Game</a>. I simply have not had time (given the work I&#8217;m doing with Catalyst) to put together a more generic design pattern &#8220;thrill ride&#8221; that I planned; so while I&#8217;m working with Catalyst, I thought I&#8217;d might as well do something with design patterns and came up with this more mild tour instead of my planned thrill ride. Figure 1 shows what it looks like:<br />
<div id="attachment_2893" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2010/03/complex.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Tour through State Design Pattern&lt;/em&gt;" title="complex" width="500" height="439" class="size-full wp-image-2893" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Tour through State Design Pattern</em></p></div></p>
<p>As you can see it&#8217;s pretty simple—sort of a PowerPoint chat. (Nowhere near as flexible as the one created with the <a href="http://www.as3dp.com/category/design-patterns/memento/">Memento</a> pattern.) So, if you&#8217;re interested in a mild tour, click the Play button and hop on:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/stateride/"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/play.jpg" alt="play" title="play" width="106" height="46" class="alignnone size-full wp-image-2255" /></a></p>
<p>By the way, the Catalyst project has made me far more aware of Flash Builder/Flex code. It uses Flex in its engine. If you&#8217;re interested in Catalyst, you can <a href="http://labs.adobe.com/technologies/flashcatalyst/">download the Beta at Adobe Labs</a>. I tried importing programs (SWF files) made with design patterns, and it works great. About the only thing I didn&#8217;t do with this tour is to put the videos on Flash Media Server—sorry. I was in a hurry and wasn&#8217;t sure how to link up FMS to Catalyst. I could have written the code in Flash/Flex and created an SWF file to import into Catalyst, and that works fine, but I&#8217;m pretty pressed for time on this project.</p>
<p>As always, your comments and valuable feedback is encouraged. I&#8217;ll be back working on this blog as soon as I&#8217;m finished with the Catalyst project. Besides, Chandima has been writing knock-out posts to keep you interested!</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%2F2010%2F03%2Fthe-actionscript-3-0-design-pattern-thrill-ride-part-ii-catalyst%2F&amp;title=The%20ActionScript%203.0%20Design%20Pattern%20Thrill%20Ride%3A%20Part%20II%E2%80%94Catalyst" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/03/the-actionscript-3-0-design-pattern-thrill-ride-part-ii-catalyst/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New Aid Game: The Machine Moves!</title>
		<link>http://www.as3dp.com/2010/02/new-aid-game-the-machine-moves/</link>
		<comments>http://www.as3dp.com/2010/02/new-aid-game-the-machine-moves/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 18:54:54 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2538</guid>
		<description><![CDATA[I wanted to move on from the original Aid Game, at least as far as the movement was concerned; so I fully fixed it up. With the new set of rules from the previous post of repairing the movement, I realized that if I didn&#8217;t generate fully operating movement states, we&#8217;d end up overly focusing [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2543" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/02/fixed.png" alt="Helper Class is Helped" title="fixed" width="250" height="193" class="size-full wp-image-2543" /><p class="wp-caption-text">Helper Class is Helped</p></div>I wanted to move on from the original Aid Game, at least as far as the movement was concerned; so I fully fixed it up. With the new set of rules from the <a href="http://www.as3dp.com/2010/01/30/fixing-the-state-machine-aid-game-repair/">previous post</a> of repairing the movement, I realized that if I didn&#8217;t generate fully operating movement states, we&#8217;d end up overly focusing on the wrong things. So, I set up a class (TimeMachine) to encapsulate the Timer object, and then the Mover class became the child of the TimeMachine using the different TimeMachine Timer properties. Because I instantiated a Timer object in each of the properties in the TimeMachine, I do not believe it is properly an Abstract class. However, like an Abstract class, it is not invoked directly, but rather through inheritance.</p>
<p><strong>Playing the Game by the Rules: First Break the Rules</strong></p>
<p>The nice thing about a State design pattern is that once the rules are established, you can build your algorithms and the different states invoke them according to the rules you&#8217;ve established. You can have a number of different algorithms within the State framework, and they should all work in accordance with the rules—no matter what the rules are or what algorithms you use to invoke them.</p>
<p>To see if the revised game follows the movement rules, try and break the rules. The main rule that you can attempt to break is the <em>No reverse without stopping rule</em>. So if you&#8217;re going right and you press the Left button, your helicopter should first stop and then you&#8217;d have to press the Left button a second time. Or you can reverse direction by pressing the Stop button and then press the opposite direction. Click the Play button to give it a whirl:</p>
<p><a href="http://www.sandlight.net/dpBlog/aid2/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/play.jpg" alt="play" title="play" width="106" height="46" class="alignnone size-full wp-image-2255" /></a></p>
<p>So go ahead and try to break the rules! If you can; then the design fails. If not, it succeeds. Once you&#8217;re finishing playing, download the latest code and see how the rules were applied.</p>
<p><a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/AidGameRev.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a><br />
<span id="more-2538"></span></p>
<p><strong>Revising the Mover Class</strong></p>
<p>To make it easier, the Mover class was revised so that the Timer was a property of a parent class (TimeMachine) that the Mover could use. This would provide a single source for the properties that could be inherited and used by the Mover to start and stop specific Timer instances used to move the helicopter.</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('p2538code39'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p253839"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p2538code39"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Sort of an Abstract class</span>
	<span style="color: #808080; font-style: italic;">//(But not really!)</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Timer</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TimeMachine
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> timeRight:Timer=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> timeLeft:Timer=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> timeDown:Timer=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> timeUp:Timer=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All the <strong>TimeMachine</strong> does is create some properties, and even though it looks like an abstract class or interface, the properties are all instantiated and the Timer is set with a value. (Okay, so it&#8217;s concrete.)</p>
<p>Now that the Mover class has some inherited properties that are locked and loaded, it can focus on some simple algorithms to move the helicopter, as you can see in the following listing:</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('p2538code40'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p253840"><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
</pre></td><td class="code" id="p2538code40"><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;">TimerEvent</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> Mover <span style="color: #0066CC;">extends</span> TimeMachine
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sp:Sprite;
&nbsp;
		<span style="color: #808080; font-style: italic;">//Go to the right</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goRight<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeUp.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeDown.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeRight.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doRight<span style="color: #66cc66;">&#41;</span>;
			timeRight.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doRight<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">x</span>++;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Go to the left</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goLeft<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeUp.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeDown.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeLeft.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doLeft<span style="color: #66cc66;">&#41;</span>;
			timeLeft.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doLeft<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">x</span>--;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Go up</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goUp<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeRight.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeLeft.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeUp.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doUp<span style="color: #66cc66;">&#41;</span>;
			timeUp.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doUp<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">y</span>--;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Go down</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goDown<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeRight.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeLeft.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeDown.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doDown<span style="color: #66cc66;">&#41;</span>;
			timeDown.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doDown<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">y</span>++;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Stop everything</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStop<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeRight.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeLeft.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeUp.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeDown.<span style="color: #0066CC;">stop</span><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>With its new set of properties, the Mover class can more easily keep track of which timers are running and which are stopped. Also, it can follow the rules laid out for movement and minimize unnecessarily stopping timers for a given state. For example, in the MoveDown state, the helicopter can not go directly to the MoveUp state. So the <strong>goDown()</strong> method does not have to include <strong>timeUp.stop()</strong> because that timer has already been stopped because it had to go to the Stop state first (which invokes the <strong>goStop()</strong> method in the Mover class) resulting in all timer instances to stop.</p>
<p><strong>Walking through a Change of State</strong></p>
<p>The changes made following a change of state may help you see how everything works together. So we&#8217;ll begin in the client with a Right button press and follow it through to where the helicopter is moved or stopped. (Keep in mind that the default state is Stop.)</p>
<p><em><strong>//Client&#8211;request right movement from Stop state</strong></em><br />
private function doRight(e:MouseEvent):void<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//contextNow is Context instance<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>contextNow.rightMove();</strong><br />
}<br />
&#8212;&#8212;&#8212;&#8211;<br />
<em><strong>//Context</strong></em><br />
public function rightMove():void<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//stateNow is a State instance<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>stateNow.doMoveRight();</strong><br />
}<br />
&#8212;&#8212;&#8212;&#8211;<br />
<em><strong>//State > Stop</strong></em></p>
<p>//context = Context instance<br />
//context.mover = Mover object that<br />
//is a property of the Context class<br />
//context.chopper = Helicopter instance that<br />
//is property of the Context class</p>
<p>public function doMoveRight():void<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>context.mover.goRight(context.chopper);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.setState(context.getRight());</strong><br />
}<br />
&#8212;&#8212;&#8212;&#8211;<br />
<em><strong>//Mover class</strong></em></p>
<p>public function goRight(sp:Sprite):void<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeUp.stop();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeDown.stop();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.sp=sp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>timeRight.addEventListener(&#8220;timer&#8221;,doRight);</strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeRight.start();<br />
}<br />
&#8212;&#8212;&#8212;&#8211;<br />
private function doRight(e:TimerEvent):void<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong> sp.x++;</strong><br />
}</p>
<p>While that process is going on, a new State is set and held in the Context class. It is now ready for the next move—whatever it may be.</p>
<p><strong>Bring on the Algorithms, but Don&#8217;t Forget this Stated as an Aggregation!</strong></p>
<p>I know there are a lot of algorithm whizzes out there, and please feel free to provide algorithms that you think are more elegant, smarter or just plain better than what I cobbled together. However, please keep in mind that this discussion began because I was hoping to show how aggregation works in composition.</p>
<p>Also, why not add more states? You could add states to pick up and drop off aid supplies with the helicopter, for example. Or you could have states where a friendly Martian flying saucers help out in the first Intergalactic Aid effort!</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%2F2010%2F02%2Fnew-aid-game-the-machine-moves%2F&amp;title=New%20Aid%20Game%3A%20The%20Machine%20Moves%21" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/02/new-aid-game-the-machine-moves/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Fixing the State Machine: Aid Game Repair</title>
		<link>http://www.as3dp.com/2010/01/fixing-the-state-machine-aid-game-repair/</link>
		<comments>http://www.as3dp.com/2010/01/fixing-the-state-machine-aid-game-repair/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 22:05:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2516</guid>
		<description><![CDATA[Getting the States Straight In a recent post to illustrate the use of composition and delegation, I created a simple game using a State design Pattern—the Aid Game. A lot of people had lots of questions and suggestions for making it better and being one who thrives on improvement and change, I promised to have [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2515" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/stateRepair.png" alt="Fixing a State Design" title="stateRepair" width="250" height="303" class="size-full wp-image-2515" /><p class="wp-caption-text">Fixing a State Design Pattern</p></div><strong>Getting the States Straight</strong></p>
<p>In a recent post to illustrate the use of composition and delegation, I created a simple game using a State design Pattern—<a href="http://www.as3dp.com/2010/01/18/actionscript-3-0-state-design-pattern-an-aid-game/">the Aid Game</a>. A lot of people had lots of questions and suggestions for making it better and being one who thrives on improvement and change, I promised to have a post where we&#8217;d review it as a State design pattern and not a quick and dirty example of composition.</p>
<p>In this post I&#8217;d like to go over some key elements of a State design and to see if we can improve on the original Aid Game by better design of state classes. Also, I&#8217;m going to leave the final repairs up to you—I&#8217;ll provide some guidelines for improved states, and you can implement them. Also, I&#8217;ll provide a little helper class that is an example of continuous movement based on one from our book that Chandima used to illustrate MVC. (I simplified it from the original; so if you look at the MVC chapter—Chapter 12—, you&#8217;ll only see pieces of it.)</p>
<p><strong>Why a State Design and What are the States?</strong></p>
<p>To get started, we have to ask the same key question that we should ask for every design we do. Namely,</p>
<blockquote><p>What varies?</p></blockquote>
<p>In moving the helicopter around in the Aid Game, the main thing that <em>varies</em> is the state of flight. (Each of the different directions and a stopped state represents a variation in state.) Ergo, I selected the State Design Pattern.</p>
<p>In our simple State design, I used five different states:</p>
<ol>
<li>Stop</li>
<li>Move Right</li>
<li>Move Left</li>
<li>Move Up</li>
<li>Move Down</li>
</ol>
<p>Next, I&#8217;ll add some rules that were not in the first version:</p>
<ul>
<li>Before reversing direction, you must first go to the Stop state.</li>
<li>If a current state is called, it cannot invoke itself.</li>
<li>Movement is continuous</li>
</ul>
<p><span id="more-2516"></span><br />
Let&#8217;s look at these rules applied to the Move Right state:</p>
<ul>
<li><strong>Stop</strong>—call Stop state</li>
<li><strong>Left</strong> —call Stop state (Cannot reverse direction—must stop first)</li>
<li><strong>Right</strong>—already moving right&#8211;null operation</li>
<li><strong>Up</strong>—call Up state</li>
<li><strong>Down</strong>—call Down state</li>
</ul>
<p>In the initial version, (The Aid Game) states called themselves, but that&#8217;s not allowed now. So, you can change the states to reflect these new rules.</p>
<p>The final rule is really one that better reflects state—continuous movement. To reflect this new game characteristic I put together a simple helper class,<strong> Mover</strong>. Using this class, the helicopter keeps a constant state moving left, right, up, down or stop.</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('p2516code42'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p251642"><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
</pre></td><td class="code" id="p2516code42"><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;">utils</span>.<span style="color: #006600;">Timer</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TimerEvent</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> Mover <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> timeOn:Timer=<span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sp:Sprite;
&nbsp;
		<span style="color: #808080; font-style: italic;">//Go to the right</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goRight<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeOn.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeOn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doRight<span style="color: #66cc66;">&#41;</span>;
			timeOn.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doRight<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">x</span>++;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Go to the left</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goLeft<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeOn.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeOn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doLeft<span style="color: #66cc66;">&#41;</span>;
			timeOn.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doLeft<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">x</span>--;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Go up</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goUp<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeOn.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeOn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doUp<span style="color: #66cc66;">&#41;</span>;
			timeOn.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doUp<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">y</span>--;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Go down</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goDown<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeOn.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			timeOn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timer&quot;</span>,doDown<span style="color: #66cc66;">&#41;</span>;
			timeOn.<span style="color: #0066CC;">start</span><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> <span style="color: #000000; font-weight: bold;">function</span> doDown<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sp.<span style="color: #006600;">y</span>++;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Stop everything</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goStop<span style="color: #66cc66;">&#40;</span>sp:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			timeOn.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sp</span>=sp;
			sp.<span style="color: #006600;">x</span>=sp.<span style="color: #006600;">x</span>;
			sp.<span style="color: #006600;">y</span>=sp.<span style="color: #006600;">y</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>You don&#8217;t have to use that class, and if you want you can put the operations in the individual states. However, to see how it works,  test it out with the helicopter in the Aid Game. Then you can decide whether you want to use it or not. Generally, I&#8217;d assume that you&#8217;d have some way of invoking movement through the Context by a call in the State child classes.</p>
<p><strong>Let&#8217;s See What You&#8217;ve Made</strong></p>
<p>Okay, now&#8217;s a good chance to implement those algorithms. Put them into the State structure and let&#8217;s see how you&#8217;ve repaired the state machine. We&#8217;re not looking for a better algorithm <em>per se</em>, but rather how it can be used with the State design pattern. It doesn&#8217;t have to be ready for prime it, but it must be invoked within the context of the State design. (By the way, did you notice that there are no conditional statements in the State design?)</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%2F2010%2F01%2Ffixing-the-state-machine-aid-game-repair%2F&amp;title=Fixing%20the%20State%20Machine%3A%20Aid%20Game%20Repair" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/01/fixing-the-state-machine-aid-game-repair/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 State Design Pattern: An Aid Game</title>
		<link>http://www.as3dp.com/2010/01/actionscript-3-0-state-design-pattern-an-aid-game/</link>
		<comments>http://www.as3dp.com/2010/01/actionscript-3-0-state-design-pattern-an-aid-game/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 15:14:11 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2433</guid>
		<description><![CDATA[After creating the post on the acquaintance relationship between classes, I started thinking about the aggregate relationship and delegation. One of the best examples of delegation can be found in the State design patterns. All of the states are delegated to objects that handle each state. So I thought it&#8217;d be a good idea to [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_750" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/03/bucket.png" alt="Take This One to Work" title="bucket" width="150" height="133" class="size-full wp-image-750" /><p class="wp-caption-text">Take This One to Work</p></div>After creating the post on the <a href="http://www.as3dp.com/2010/01/16/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/">acquaintance relationship</a> between classes, I started thinking about the aggregate relationship and delegation. One of the best examples of delegation can be found in the State design patterns. All of the states are delegated to objects that handle each state. So I thought it&#8217;d be a good idea to create a little game that demonstrates both delegation and a state engine at work. Then, when I do the post on aggregation relations, I&#8217;ll have a simple reference point. This one is set up so that you can put it into your lunch bucket and take it to work. What&#8217;s more, it&#8217;s designed so that you can extend it into a more interesting game.</p>
<p>I decided that a good starting point would be to have a helicopter fly aid packages to different places on the screen. It would start at the airport and then fly the aid to different sprite objects representing groups of people in need. However, to get started, all I did was to set up five buttons to move the helicopter around the screen beginning at the airport. Figure 1 shows what the game looks like. (It is set up on an 800 by 600 display; so I had to cut off part of the right side to fit in this blog format.) The movement buttons are in the lower left portion of the screen.<br />
<div id="attachment_2449" class="wp-caption alignnone" style="width: 460px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/AidGame.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; State Pattern moves Helicopter Object&lt;/em&gt;" title="AidGame" width="450" height="600" class="size-full wp-image-2449" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> State Pattern moves Helicopter Object</em></p></div></p>
<p>Click on the Play button to get an idea of how it works:<br />
<a href="http://www.sandlight.net/dpBlog/aid/"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/play.jpg" alt="play" title="play" width="106" height="46" class="alignnone size-full wp-image-2255" /></a></p>
<p><span id="more-2433"></span><br />
<strong>The Little State Engine that Could</strong></p>
<p>The concept of a state machine or state engine brings the focus to changing states. Each state is delegated to a separate class, and as an object&#8217;s state changes, the state is handled by (delegated to) the appropriate class. In order to keep track of the current state, all requests are handled by a Context class. The Context class acts as an interface for the Client, and so the Client need not deal with the State objects directly. Figure 2 shows the class diagram for the State pattern.<br />
<div id="attachment_2441" class="wp-caption alignnone" style="width: 434px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/stateDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;State Design Pattern Class Diagram&lt;/em&gt;" title="stateDiagram" width="424" height="199" class="size-full wp-image-2441" /><p class="wp-caption-text"><em><strong>Figure 2: </strong>State Design Pattern Class Diagram</em></p></div></p>
<p>If you look at the class diagram, you may be thinking that it looks a lot like the <a href="http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/">Strategy design pattern</a>. You&#8217;d be right! They look very similar, but their intent is quite different. In the class diagram, you&#8217;ll see the little red <strong>state</strong> label. Therein lies the difference between the State and Strategy patterns. The State pattern delegates different <em>states</em> and the Strategy pattern delegates different <em>algorithms</em>. Thus, the intent is quite different even though the class diagrams are similar.</p>
<p><strong>Direction and Movement</strong></p>
<p>In the past, I&#8217;ve used State designs for doing work with video players changing states such as play, record, pause and stop. For this one, I wanted to change directions of a moving object, and I selected a relatively simple design for changing the direction (movement state) of a helicopter. Using the vertical plane of a computer monitor for a reference point, the movement states are up, down, left, right, and stop. I used a very simple movement algorithm—the helicopter crabs along at 8 pixels per button click. (If you&#8217;re interested in more robust movement, see page 458 where Chandima has a nice algorithm for moving along continuously.) <em>Remember</em>, the purpose of this blog is to help understand design patterns, and so we try to focus on relatively clean examples. Once you get the design pattern down, it&#8217;s easy to add some really great algorithms.</p>
<p>With that in mind, I created the different classes for each of the different states along with classes for a Client and Context that work with a State interface. Figure 3 shows the file class diagram for the example:<br />
<div id="attachment_2454" class="wp-caption alignnone" style="width: 491px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/FileDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3: &lt;/strong&gt;State File Class Diagram&lt;/em&gt;" title="FileDiagram" width="481" height="217" class="size-full wp-image-2454" /><p class="wp-caption-text"><em><strong>Figure 3: </strong>State File Class Diagram</em></p></div></p>
<p>The Client requests are through the Context class. In turn, the Context class contains the moving object (chopper) as a property, and so it&#8217;s available to the different concrete state classes that change its state.</p>
<p><strong>The Delegation Master</strong></p>
<p>When working with the State Design Pattern, the state files tend to grow with added states. That&#8217;s because each state class must have the complete interface of the State class, which means all of the states. So, to save some time, I&#8217;ve put it online for you in CS3 and CS4 files. If you&#8217;re using Flex (<em>Flash Builder</em>) all you&#8217;ll need is a sprite object for the helicopter and the graphics you can see in Figure 1.) Click the Download button to get all of the files you need:<br />
<a href="http://www.sandlight.net/dpBlog/aid/AidGame.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>Okay, we&#8217;re all set to look at the Context class. The role of the Context class is to keep track of the current state and set the state requested by the Client.</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('p2433code48'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p243348"><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
</pre></td><td class="code" id="p2433code48"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Context class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</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> stoph:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moveup:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> movedown:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moveleft:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moveright:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateNow:State;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> chopper:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span> chopper:Sprite <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">chopper</span>=chopper;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;State is here&quot;</span><span style="color: #66cc66;">&#41;</span>;
			moveup=<span style="color: #000000; font-weight: bold;">new</span> MoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stoph=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			movedown=<span style="color: #000000; font-weight: bold;">new</span> MoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			moveleft=<span style="color: #000000; font-weight: bold;">new</span> MoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			moveright=<span style="color: #000000; font-weight: bold;">new</span> MoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stateNow=stoph;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> upMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">doMoveUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">doStopNow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> downMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">doMoveDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> leftMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">doMoveLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rightMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stateNow.<span style="color: #006600;">doMoveRight</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> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>stateNow:State<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A new stateNow is set:&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateNow</span>=stateNow;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> stateNow;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">moveup</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">stoph</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">movedown</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">moveleft</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">moveright</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;chopper&#8221; is the helicopter object passed from the Client through a parameter. It is typed to the Sprite parent (interface) to follow the principle of programming to the interface instead of the implementation. In Flash, the Helicopter class is in the Library, while in Flex, you can put it anywhere you want as long as it can be instantiated in the Client as a Sprite type and passed to the Context class.</p>
<p>As you can see, several variables are initialized as State objects. The default state is Stop, and so when the program starts, the chopper object is in the stop state. Then, you will see a series of setter functions such as:</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('p2433code49'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p243349"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2433code49"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> leftMove<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	stateNow.<span style="color: #006600;">doMoveLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Such setter functions are called from the Client, but the Client never sees the concrete state object that they are delegated to by the Context. The Context passes on the request to another function that sets the current state and calls the requested state. Voila! Delegation at work.</p>
<p><strong>The States</strong></p>
<p>The next step is to set up a State interface with all of the different methods that will be set up in each state. That&#8217;s easy because we are using an ActionScript interface, and not the more general <em>interface</em> that is virtually any parent 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('p2433code50'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p243350"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p2433code50"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Interface</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> doMoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> doMoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> doStopNow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> doMoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> doMoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Next, each of the concrete state classes must implement the full interface. However, only certain methods are used in each state to implement the state. The way I chose to implement the state was in the state itself. What results is that when the state is initially called, nothing happens because that current state (or calling state) does not contain the new state. This is a bit different from other state designs I&#8217;ve done, but it works well enough when the state change is movement. The following listing contains all of the five states:</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('p2433code51'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p243351"><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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
</pre></td><td class="code" id="p2433code51"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//MoveDown State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> MoveDown <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//private var chopper:Sprite=new Helicopter();</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> MoveDown<span style="color: #66cc66;">&#40;</span>context:Context<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;--MoveDown State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStopNow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">chopper</span>.<span style="color: #006600;">y</span>+=<span style="color: #cc66cc;">8</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getRight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//MoveLeft State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> MoveLeft <span style="color: #0066CC;">implements</span> State
	<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> MoveLeft<span style="color: #66cc66;">&#40;</span>context:Context<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;--MoveLeft State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStopNow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">chopper</span>.<span style="color: #006600;">x</span>-=<span style="color: #cc66cc;">8</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getRight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//MoveRight State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> MoveRight <span style="color: #0066CC;">implements</span> State
	<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> MoveRight<span style="color: #66cc66;">&#40;</span>context:Context<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;--MoveRight State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStopNow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">chopper</span>.<span style="color: #006600;">x</span>+=<span style="color: #cc66cc;">8</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getRight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//MoveUp State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> MoveUp <span style="color: #0066CC;">implements</span> State
	<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> MoveUp<span style="color: #66cc66;">&#40;</span>context:Context<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;--MoveUp State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">chopper</span>.<span style="color: #006600;">y</span>-=<span style="color: #cc66cc;">8</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStopNow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getRight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/////</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Stop State;</span>
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Stop</span> <span style="color: #0066CC;">implements</span> State
	<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> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span>context:Context<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;--Stop State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doStopNow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveLeft<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getLeft</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doMoveRight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getRight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Each movement state simply increments or decrements the relative value of the <strong>x</strong> and <strong>y</strong> properties of the chopper. So while it may look complex, it really is not. Also, it is very easy to change; so if you want to add continuous movement, you should be able to do so.</p>
<p><strong>The Client</strong></p>
<p>This particular Client could have been set up to get all of its buttons elsewhere, but otherwise, all it does is to fire off event handlers to make requests. However, it is essential that it begins by instantiating a Helicopter instance that it passes to the Context class through a Sprite typed parameter. So, the Client, never even talks to the state objects. Instead, it just sends all requests for changing the direction and movement of the Helicopter instance through the Context.</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('p2433code52'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p243352"><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
</pre></td><td class="code" id="p2433code52"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Client makes requests</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> <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;">public</span> <span style="color: #000000; font-weight: bold;">var</span> chopper:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Helicopter<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> contextNow:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span>chopper<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> upBtn:<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> downBtn:<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> stopBtn:<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> leftBtn:<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> rightBtn:<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> testBtn:<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> const AEROPORTX:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">84</span>;
		<span style="color: #0066CC;">private</span> const AEROPORTY:<span style="color: #0066CC;">Number</span>=<span style="color: #cc66cc;">64</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>
			chopper.<span style="color: #006600;">x</span>=AEROPORTX,chopper.<span style="color: #006600;">y</span>=AEROPORTY,
			addChild<span style="color: #66cc66;">&#40;</span>chopper<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Default</span>
			contextNow.<span style="color: #006600;">stopMove</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			upBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">132</span>,upBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">465</span>;
			upBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">45</span>, upBtn.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">25</span>;
			upBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Up&quot;</span>;
			upBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, doUp<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>upBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			stopBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">132</span>,stopBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">513</span>;
			stopBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">45</span>,stopBtn.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">25</span>;
			stopBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Stop&quot;</span>;
			stopBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, doStop<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>stopBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			downBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">132</span>,downBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">561</span>;
			downBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">45</span>,downBtn.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">25</span>;
			downBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Down&quot;</span>;
			downBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, doDown<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>downBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			leftBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">69</span>,leftBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">513</span>;
			leftBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">45</span>,leftBtn.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">25</span>;
			leftBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Left&quot;</span>;
			leftBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, doLeft<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;">200</span>,rightBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">513</span>;
			rightBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">45</span>,rightBtn.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">25</span>;
			rightBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Right&quot;</span>;
			rightBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, doRight<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rightBtn<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> doUp<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>
			contextNow.<span style="color: #006600;">upMove</span><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> <span style="color: #000000; font-weight: bold;">function</span> doStop<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>
			contextNow.<span style="color: #006600;">stopMove</span><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> <span style="color: #000000; font-weight: bold;">function</span> doDown<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>
			contextNow.<span style="color: #006600;">downMove</span><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> <span style="color: #000000; font-weight: bold;">function</span> doLeft<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>
			contextNow.<span style="color: #006600;">leftMove</span><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> <span style="color: #000000; font-weight: bold;">function</span> doRight<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>
			contextNow.<span style="color: #006600;">rightMove</span><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>As you can see, it can make five different requests based on the button event handlers, and all of them are through Context methods (e.g., <code>contextNow.rightMove()</code>).</p>
<p>Because the example is so simple, you may be thinking that you could have done the same thing with a single class and not an exhaustive amount of code. You&#8217;d be absolutely right. Also, you would not lose an ounce of functionality. However, the focus of this example is to see how delegation is used and how easy it would be to use the exact same code for all different kinds of movement. Further, you could re-use the code for moving as many objects as you wanted. Because the entire program is so loosely coupled, substitution and change is very simple.</p>
<p>Anyway, you can do a lot with the State Design Pattern. For starters, why not improve on this little application and turn it into a game? Fo example, you could add an independent sprite to be picked up at the airport and dropped at the 10 different locations. Add one timer for continuous movement and another for keeping score of how fast you could move all of the aid packages to the needy orange sprites.</p>
<p>This might be a good resource for our next Golden LunchBox contest!</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%2F2010%2F01%2Factionscript-3-0-state-design-pattern-an-aid-game%2F&amp;title=ActionScript%203.0%20State%20Design%20Pattern%3A%20An%20Aid%20Game" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/01/actionscript-3-0-state-design-pattern-an-aid-game/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>Friends with Benefits: State and Factory Method Together at Last—Part II</title>
		<link>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/</link>
		<comments>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 09:23:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1106</guid>
		<description><![CDATA[In the first part of this article, Friends with Benefits: Refactoring with Multiple Design Patterns—Part I you saw how a Factory Method design pattern was used to hold and deliver data to a DataProvider object. The Factory Method design pattern allowed for easy update by simply adding items to concrete products from a Product interface [...]]]></description>
			<content:encoded><![CDATA[<p>In the first part of this article, <a href="http://www.as3dp.com/2009/06/16/friends-with-benefits-refactoring-with-multiple-design-patterns—part-i/"> Friends with Benefits: Refactoring with Multiple Design Patterns—Part I</a> you saw how a Factory Method design pattern was used to hold and deliver data to a DataProvider object. The Factory Method design pattern allowed for easy update by simply adding items to concrete products from a Product interface (DataSupply) or new concrete Product classes. New data can be added to the Concrete Products and the Client makes requests through the Creator interface. The request is filtered through the Concrete Creator to the Product interface to the concrete Product classes. Now, all we need to do is to refactor the player with a design pattern. However, before starting <strong> download </strong> the application (With 12 classes in the design patterns, a Client class plus three folders of videos, there’s a lot!)  So first off, <a href="http://www.sandlight.net/dpBlog/FactoryMethodState.zip">click here to Download All Files </a>. You can see a <a href="http://www.sandlight.net/dpBlog/dualdp.html">working version of the player and multiple data sets here</a>. These will help pull together all of the elements used in this project.</p>
<p><strong>Here Comes the Big Bad State Machine</strong></p>
<p>Before we get going on refactoring the video player application, we need to see where it will go in the context of both the State and the Factory Method design patterns. Figure 1 shows the relationship between the refactored player (State) and the factory that delivers the data (Factory Method).<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/dualdpfilessm.png" alt="dualdpfilessm" title="dualdpfilessm" width="500" height="392" class="alignnone size-full wp-image-1103" /><br />
<em><strong>Figure 1:</strong> Files for two design patterns in class diagram</em></p>
<p>The images are organized in an “open” folder, and the videos are placed in separate folders showing their relative position to the classes making up the two design patterns. The Client makes a request through the Factory Method to populate a List component with data from a DataProvider. Then, the request from the Client to the player can be made through the List component by clicking on the video selection. Figure 2 shows what the final product looks like so that you can more easily follow the refactoring:<br />
<span id="more-1106"></span><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/dualvid500.png" alt="dualvid500" title="dualvid500" width="500" height="365" class="alignnone size-full wp-image-1114" /></p>
<p><em><strong>Figure 2:</strong> Selected video running in state machine player</em></p>
<p>If Figure 2 looks different from what we started in Part I of this article, it’s because the application changed. Originally, the plan was to take the application made for showing F4V files (H.264 formatted HD files) using the latest version of Flash Media Server (FMS) and show how it was refactored. However, since most readers do not develop with FMS, the plan was changed to show a progressive download application that anyone could use. The State design pattern is almost identical to the one used in our book to develop a player. Likewise, we don’t plan to try your patience by rehashing Chapter 10 or the article on <a href= "http://www.adobe.com/devnet/flashmediaserver/articles/video_state_machine_as3.html" > DevNet </a> that cover statecharts, the details of state machines and the State Design Pattern. Instead, I’d like to focus on the Client and show how it makes requests to different sets of classes using different design patterns. Therefore, I’ll only <em>briefly</em> go over the State Design Pattern to help you orient yourself to its use in the current context with another design pattern.</p>
<p><strong>State Machine as Time Machine</strong></p>
<p>I like to imagine the state machine as a time machine. Each time represents a different state. As you go from one state to another, things look different and the possibilities are different. If you go to the mid 18th century you’ll find horse-powered transportation and in that state you’ll find no air transportation beyond hot air balloons. Conversely, if you go into the future to the 22nd century you may find transportation that runs only on hydrogen and air travel only occurs beyond the stratosphere. In different time periods you expect different capabilities. Likewise, in different states, you have different expectations. For example, in the video player, if you are in a <em>stop state</em> you cannot stop. That’s because in that state, you’re already stopped. Likewise, in a <em>play state</em> you cannot go to play, because you’re already there.</p>
<p>Some states are trickier than others, but you have options. In one video player I made, I would not allow the user to go from Pause to Play or Stop. The user had to first Un-Pause by going to Pause again. That was when the main Pause state was set up as a toggle. One blogger complained that he just couldn’t deal with a Player that would not allow one to go from Pause to Stop or Play. The reason behind my decision was that some funny things happened when one left the Pause state without un-pausing it first. (Now that ActionScript 3.0 has a <strong>NetStream.resume()</strong> method, the toggle is no longer required.) Of course, if one decides that it’s perfectly fine to go from a Pause state to a Play or Stop; all you’ve got to do is to change the code in the Pause state—in fact it’s really easy to do and is one of the advantages of the State Design Pattern. (In case you haven’t noticed, I have no god-like powers to enforce code decisions—you’re programmers; so change the code all you want!)</p>
<p>By setting up your program as state machines, you have the option of easily changing the states. You can change the contents of the states (as suggested in the previous paragraph) or you can add new states. As your application changes and grows, all you need to do is to change the states to reflect the application’s functionality.</p>
<p><strong>Avoiding Conditional Statements</strong></p>
<p>My favorite feature of the State Design Pattern is that it has <em>no conditional statements</em>. Count ‘em—none. In fact, in this entire program, you’ll find only a single <strong>switch</strong> statement in the <strong>ConcreteCreatorVid</strong> class in the Factory Method pattern portion of the program. If you look at different Factory Method patterns in most languages, you’ll find that the concrete creator classes contain one or more conditional statement; so, sometime you <em>do</em> need them. (I’ve concealed one more conditional in the Client that takes care of a little bling, but you’ll have to search to find it—besides it doesn’t count because it’s in the Client.)</p>
<p><a href="http://www.as3dp.com/2007/12/21/lets-get-rid-of-conditionals/">What do I have against conditional statements?</a> Well, it’s not so much that I have anything against conditionals as it is the fact I like the idea of not using them. The State design makes you stop and think of what possibly can happen in a given state. Then instead of first having to filter through conditional statements, you simply do exactly what the current state allows. The logic flow is more pleasing and forces me to be a better programmer.</p>
<p><strong>The Mighty Client</strong></p>
<p>One of the original goals of refactoring my video player was to give it more flexibility and to cut down on the size of a single big class—essentially a huge self-serving Client. The idea of the Client is to make requests, and it <strong>should not be used as</strong> <em>an everything class</em>. Several design patterns include the Client as a participant, but even they recognize the role of the Client as a request-maker.</p>
<p>In Figure 1, you can see that the Client makes requests through the Creator interface in the Factory Method pattern and through the Context class in the State pattern.  A Client making requests from multiple classes or interfaces is not unusual—consider the <a href="http://www.as3dp.com/category/design-patterns/abstract-factory-pattern/"> Abstract Factory </a> pattern. The request-making involves a set of UIs for different data sets for the DataProvider object. It provides the List UI with selections used to request the different videos available for displaying in the player. The following listing shows that the great majority of the Client is taken up by UIs and methods for making request with the UIs.</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('p1106code57'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110657"><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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
</pre></td><td class="code" id="p1106code57"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Progressive Download</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> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">List</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">ListEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</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;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</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> 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> btnGp1:<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> btnGp2:<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> btnGp3:<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> playList:<span style="color: #0066CC;">List</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">List</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> stopBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resumeBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nc:<span style="color: #0066CC;">NetConnection</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</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> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</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> 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> flv:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> metaCap:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopState:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mset:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> group1:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<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> group2:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<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> group3:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<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> create:Creator=<span style="color: #000000; font-weight: bold;">new</span> ConcreteCreatorVid<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>
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			ns = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Handle end of flv play status</span>
			ns.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,flvCheck<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setProviders<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Set up object to capture metadata</span>
			metaCap = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span>  ;
			ns.<span style="color: #006600;">client</span> = metaCap;
			metaCap.<span style="color: #006600;">onMetaData</span> = getMeta;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Video Buttons and List Components</span>
			playList.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,playList.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>playList<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			group1 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group1;
&nbsp;
			stopBtn=<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>;
			stopBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Stop&quot;</span>;
			stopBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>;
			stopBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">142</span>,stopBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>stopBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			pauseBtn=<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>;
			pauseBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Pause&quot;</span>;
			pauseBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>;
			pauseBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">206</span>,pauseBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pauseBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			resumeBtn=<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>;
			resumeBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Resume&quot;</span>;
			resumeBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">55</span>;
			resumeBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">270</span>,resumeBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>resumeBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Add Event Listeners</span>
			playList.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ListEvent.<span style="color: #006600;">ITEM_CLICK</span>,doPlay<span style="color: #66cc66;">&#41;</span>;
			stopBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,doStop<span style="color: #66cc66;">&#41;</span>;
			pauseBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,pauseNow<span style="color: #66cc66;">&#41;</span>;
			resumeBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,resumeNow<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> setProviders<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Data provider buttons</span>
			btnGp1.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp1.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">160</span>;
			btnGp1.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group1&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnGp2.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp2.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">190</span>;
			btnGp2.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group2&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnGp3.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp3.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">220</span>;
			btnGp3.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group3&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp3<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Add Event Listeners</span>
			btnGp1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp1<span style="color: #66cc66;">&#41;</span>;
			btnGp2.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp2<span style="color: #66cc66;">&#41;</span>;
			btnGp3.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp3<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> getMeta<span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#40;</span>mset<span style="color: #66cc66;">&#41;</span> ? removeChildAt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">null</span>;
			mset = <span style="color: #000000; font-weight: bold;">true</span>;
			vid.<span style="color: #0066CC;">width</span> = mdata.<span style="color: #0066CC;">width</span>;
			vid.<span style="color: #0066CC;">height</span> = mdata.<span style="color: #0066CC;">height</span>;
			<span style="color: #000000; font-weight: bold;">var</span> backdrop:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x611427,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>vid.<span style="color: #006600;">x</span>,vid.<span style="color: #006600;">y</span>,vid.<span style="color: #0066CC;">width</span>,vid.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>backdrop,<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> <span style="color: #000000; font-weight: bold;">function</span> getGp1<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>
			group1 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group1;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp2<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>
			group2 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group2&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group2;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp3<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>
			group3 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group3&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group3;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> flvCheck<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stopState=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetStream.Play.Stop&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#40;</span>stopState<span style="color: #66cc66;">&#41;</span> ? halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">stopPlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #0066CC;">clear</span><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> <span style="color: #000000; font-weight: bold;">function</span> doPlay<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ListEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			flv = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">item</span>.<span style="color: #0066CC;">data</span>;
			context.<span style="color: #006600;">startPlay</span><span style="color: #66cc66;">&#40;</span>ns,flv<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> doStop<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>
			halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> pauseNow<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>
			context.<span style="color: #006600;">pausePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> resumeNow<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>
			context.<span style="color: #006600;">resumePlay</span><span style="color: #66cc66;">&#40;</span>ns<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>I’ve got to admit that for a Client class, this one seems pretty big. However, all of the data for selecting the videos and working the video player are either part of the Factory Method or State design patterns. With only slight modifications <em>in the Client</em> this same player can handle FMS files, including F4V files. I’ve used this same State pattern for several different kinds of players, including the one operating FMIS 3.2 and playing high definition videos (<a href="http://www.sandlight.net/prague/"> such as these</a>) that we discussed in Part I of this post.</p>
<p><strong>The Context Class</strong></p>
<p>The Context class is the key to understanding the State Design Pattern as a <em>pattern</em> and not simply a state machine.  When you look at it,  the class appears to be nothing more than a <em>getter/setter</em> class. However, it is far more. It establishes which state is the <strong>current state</strong>. By setting up the current state, it provides for one of the best examples of polymorphism that can be found in OOP. The State interface provides four methods, but depending on the <em>current</em> concrete state class, each method is implemented differently. The following listing shows the Context class used in this 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('p1106code58'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110658"><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
</pre></td><td class="code" id="p1106code58"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Context Class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</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> playState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resumeState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> state:State;
		<span style="color: #0066CC;">public</span> <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;Video Player is on&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Play</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stopState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			pauseState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			resumeState = <span style="color: #000000; font-weight: bold;">new</span> Resume<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			state=stopState;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">startPlay</span><span style="color: #66cc66;">&#40;</span>ns,flv<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">stopPlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">pausePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">resumePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>state:State<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A new state is set&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span>=state;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> state;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPlayState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">playState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStopState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">stopState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPauseState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pauseState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResumeState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">resumeState</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 Context class delegates requests (from the Client) to a State object. All methods either use or return a State object. The trick in the Context is to keep track of the current state so that when a requested method is returned to the Client, it is done in the appropriate manner. (e.g., a stop play request <em>will not</em> be sent from a stop state.)</p>
<p><strong>The State Interface and the States</strong></p>
<p>The state interface and the concrete states are relatively easy. Using either an interface or abstract class, the State interface provides the essential methods that can be implemented in the concrete state classes.  First, the State interface shows four simple methods—playing, stopping, pausing and resuming video play:</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('p1106code59'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110659"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1106code59"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//State Interface</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">interface</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All of the methods have the same parameters except for the <strong>startPlay()</strong> method that has an additional string parameter for the video URL. The rest are for directing actions in the <strong>NetStream()</strong> object.</p>
<p>Finally, the concrete State classes that implement the State interface are <em>set states</em>. That means that the action associated with the state has been set so that you can possibly do whatever that state allows. For example, if you’re in the Stop state, you can go to the Play state, but since you’re already in the Stop state, it doesn’t make any sense to go there. (Think of yourself at a stop sign in traffic. You can go to “drive” but since you’re at stop, you cannot “go” there.) So, without further ado, let’s look at the four state classes:</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('p1106code60'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110660"><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
</pre></td><td class="code" id="p1106code60"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Stop State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Stop</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span>context:Context<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;--Stop State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>flv<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Begin playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPlayState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You're already stopped&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot go to Pause from Stop.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot go to Resume from Stop.&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;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Play State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Play</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Play</span><span style="color: #66cc66;">&#40;</span>context:Context<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;--Play State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You're already playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop playing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStopState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Begin pause.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPauseState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot resume from a play state.&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;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Pause State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Pause</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Pause</span><span style="color: #66cc66;">&#40;</span>context:Context<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;--Pause State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You have to go to resume&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Don't go to Stop from Pause&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Allow going from Pause state to Stop state</span>
			<span style="color: #808080; font-style: italic;">//ns.close();</span>
			<span style="color: #808080; font-style: italic;">//context.setState(context.getStopState());</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot pause from Pause state.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #006600;">resume</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Quit pausing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getResumeState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Resume</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Resume <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Resume<span style="color: #66cc66;">&#40;</span>context:Context<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;--Resume State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Start over&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPlayState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop playing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStopState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pause.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPauseState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Already in resume state.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In addition to providing a set of methods that direct the program to the next possible set of states, the constructor function in each state class sets itself as the current state. Put differently, each state sets the current context to itself.</p>
<p><strong>Using Multiple Design Patterns</strong></p>
<p>Creating programs with multiple design patterns is no more unusual than writing programs with multiple methods or properties. Further, this particular application suggests that you can mix and match design patterns and reuse the major components for any number of projects. Further, the Client doesn’t know (or care) what changes are made in the different components as long as they follow the rules laid out by the interface and the request to the appropriate component in the pattern is unchanged.</p>
<p>The difficult part is doing it right the first time. Then, later as the program requires changes (<em>as it always does</em>), it’s easy to do so. Further, you can take the exact same design patterns and use them for different applications, which saves time as well.</p>
<p>As a final note, I’ve left several trace statements in the code to help you better see what happens in different states. Once in a given state and running the program from within either Flex or Flash, note the trace output. Also, try requesting “impossible” states—such as pressing the Stop button when the program is already stopped. Finally, if you do not like a condition placed on a state (such as disallowing movement from one state to a certain different state), go ahead and change the 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%2F2009%2F06%2Ffriends-with-benefits-state-and-factory-method-together-at-last-part-ii%2F&amp;title=Friends%20with%20Benefits%3A%20State%20and%20Factory%20Method%20Together%20at%20Last%E2%80%94Part%20II" 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>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

