<?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; Closer Look</title>
	<atom:link href="http://www.as3dp.com/category/closer-look/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 Developers Caught in the Middle</title>
		<link>http://www.as3dp.com/2010/04/actionscript-3-0-developers-caught-in-the-middle/</link>
		<comments>http://www.as3dp.com/2010/04/actionscript-3-0-developers-caught-in-the-middle/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 01:16:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Closer Look]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2995</guid>
		<description><![CDATA[Why Me? When two companies that are intertwined with my fate get into a spat, I get itchy. Because we are all linked to ActionScript 3.0 in one way or another, and Adobe is linked to ActionScript 3.0 through Flex and Flash this sad state of affairs may have consequences for us all. However, those [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2996" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/04/middle.png" alt="The Pincers" title="middle" width="250" height="248" class="size-full wp-image-2996" /><p class="wp-caption-text">The Pincers</p></div><strong>Why Me?</strong></p>
<p>When two companies that are intertwined with my fate get into a spat, I get itchy. Because we are all linked to ActionScript 3.0 in one way or another, and Adobe is linked to ActionScript 3.0 through Flex and Flash this sad state of affairs may have consequences for us all. However, those of us who do our primary development using Apple computers (Macs), our fates are doubly impacted.</p>
<p>Until now, I&#8217;ve always considered ActionScript 3.0 to be a friend of the world. A large proportion of Apple&#8217;s success can be traced to the applications produced by Adobe, and yet this current parting of the ways seems to be more than a hiccup in a long symbiotic relationship between these two high technology giants.</p>
<p>Many of this group were primed to begin writing applications for the iPhone and iPad (and some may have), but now that seems out of the question. In a strongly <a href="http://www.apple.com/hotnews/thoughts-on-flash/">worded broadside</a> against Adobe, Steven Jobs, Apple&#8217;s Boss. Now, look what we&#8217;ve got&#8230;</p>
<blockquote><p>With Ate by his side come hot from hell,<br />
Shall in these confines with a monarch&#8217;s voice<br />
Cry &#8216;Havoc,&#8217; and let slip the dogs of war;<br />
That this foul deed shall smell above the earth<br />
With carrion men, groaning for burial. </p></blockquote>
<p><a href="http://www.flashstreamworks.com/archive.php?post_id=1272653989">So Adobe responded to Jobs rant.</a></p>
<p>Anyway, here are my comments, and we&#8217;d like to hear from you as well:</p>
<p>1. He rightfully acknowledges that Apple was one of Adobe’s initial customers. However, Apple needed  Adobe’s fonts as well as the PostScript language that made their initial laser printer more than really good dot-matrix printer.</p>
<p>2. Also, while Jobs is probably correct in noting that about half of Adobe’s products are purchased by Apple owners; most of the Apple upgrades we purchase are because of the improvements Adobe makes in its CS5 suite. I’d still have my old iMac GooseNeck were it not for the fact that Adobe’s new products required new Apple hardware.</p>
<p>3. In looking at the apps in my Dock, the ones I use the most are by Adobe. Were it not for iTunes, I would not use Apple software (aside from the OS) all that often. The second most-used is Microsoft Office. It’s down the line when I find Apple software not related to iTunes that I find using a good deal. QT is sometimes used, but rarely anymore because Adobe Media Player is more responsive.</p>
<p>4. Several software developers have argued that, &#8220;It’s just not worth it to create Macintosh versions.&#8221; Adobe was always there for Apple when a lot of other software developers threw in with PCs only.</p>
<p>Being a fan of both camps, I truly hope that Apple and Adobe find a common ground. Even the Mafia knows it’s more profitable to cooperate than bicker.</p>
<p>What do you all think?</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%2F04%2Factionscript-3-0-developers-caught-in-the-middle%2F&amp;title=ActionScript%203.0%20Developers%20Caught%20in%20the%20Middle" 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/2010/04/actionscript-3-0-developers-caught-in-the-middle/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>A Closer Look: Using MovieClip.addFrameScript()</title>
		<link>http://www.as3dp.com/2009/09/a-closer-look-using-movieclipaddframescript/</link>
		<comments>http://www.as3dp.com/2009/09/a-closer-look-using-movieclipaddframescript/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 08:54:31 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Closer Look]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1789</guid>
		<description><![CDATA[In a recent post, I looked at using the undocumented MovieClip.addFrameScript() method. It is a solution for accessing MovieClip objects with embedded tweens using external ActionScript 3.0. Not only is this important for controlling animations, but it is also a way to add code in classes that are part of a Design Pattern with ActionScript [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1393" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/08/closerlook.png" alt="A Closer Look" title="closerlook" width="150" height="143" class="size-full wp-image-1393" /><p class="wp-caption-text">A Closer Look</p></div> In a <a href="http://www.as3dp.com/2009/08/25/artists-animators-and-actionscript-30/">recent post</a>, I looked at using the undocumented <strong>MovieClip.addFrameScript()</strong> method. It is a solution for accessing MovieClip objects with embedded tweens using external ActionScript 3.0. Not only is this important for controlling animations, but it is also a way to add code in classes that are part of a Design Pattern with ActionScript 3.0 files. In other words, you don&#8217;t have to use the Timeline or the Actions panel at all in taking advantage of the animation capabilities of Flash and at the same time have all of the advantages of using Design Patterns. In the initial use of the MovieClip.addFrameScript(), I didn&#8217;t take time to really look at what the method can do or even exactly how it works other than by example. This short post, tries to provide better documentation for this undocumented feature and an example of using it. Clicking the Play button provides an experience of the final product.(It just plays explosion flashes from the tweens in the movie clip in the library.)<br />
<a href="http://www.sandlight.net/dpBlog/addFrame/" 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><br />
<span id="more-1789"></span><br />
<strong>Documenting the Undocumented</strong></p>
<p>In the initial example  using the MovieClip.addFrameScript() (noted above), all I wanted was some way to have an animation play once and stop. As soon as the playhead reached the last frame, it jumped to a function named in the second parameter of the addFrameScript() method. However, I didn&#8217;t do much by way of clarifying that. So I&#8217;d like to start with a general view of the method shown in Figure 1:<br />
<div id="attachment_1776" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/addframescript.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Structure of the addFrameScript() method&lt;/em&gt;" title="addframescript" width="500" height="236" class="size-full wp-image-1776" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Structure of the addFrameScript() method</em></p></div></p>
<p>Oddly, the trick to using the method effectively is not just in the ending frame to fire the function, but also in the starting frame. In looking around, one thing I found is that the method can have multiple <em>trigger frames</em> that launch a function. The following shows the format:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>mc.addFrameScript(n1, func1, n2, func2, n3, func3);</strong></p>
<p>Each trigger frame is the frame number in the movie clip minus 1. However, when you have multiple triggers, you want to play the movie clip from some starting point other than the one that fires the function. To be purposeful, I believe that using this method must take advantage of multiple animations that can be generated in a single movie clip either at different starting points and/on on different layers. In other words, anywhere in the movie clip. The general setup for this kind of functionality looks like the following:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>mc.addFrameScript(n1, func1, n2, func2);<br />
&nbsp;&nbsp;&nbsp;&nbsp;mc.gotoAndStop(1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;. . . .<br />
&nbsp;&nbsp;&nbsp;&nbsp;mc.gotoAndPlay(seqBegin);</strong></p>
<p>In this case the parameter, <strong>seqBegin</strong> is where the animation segment begins. It plays until it hits the trigger frame that fires the function that stops the animation or causes some other action.</p>
<p><strong>Adding an Exoskeleton</strong></p>
<p>When placing code on the Timeline or inside the MovieClips themselves, the code is hidden and unavailable to the larger structures provided by Design Patterns. So, I continue to think of the code in terms of an exoskeleton that can reach all parts of the MovieClip objects in the Library and at the same time be available for ActionScript 3.0 in ActionScript files.  In order to illustrate something useful with the multiple function addFrameScript() method, I decided to continue with an example for the bellicose <a href="http://www.as3dp.com/2009/08/19/wrong-way-warrior-getting-flexibility-with-design-patterns—part-ii/">Wrong Way Warrior</a>.  I create three animations for an exploding grenade, rocket and mortar. The only difference is in the size of the explosion, but the animations could be as complex or simple as desired.</p>
<p>Taking an eight-sided star drawing, I placed it into a graphic symbol. Then, I created a MovieClip giving it the class name Boomer. It used motion tweens to expand the graphic star from small to large. Depending on the weapon, the explosion varied. Figure 2 shows the MovieClip&#8217;s timeline with the motion tweens:<br />
<div id="attachment_1796" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/movieclip.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt;Animated Explosions in MovieClip&lt;/em&gt;" title="movieclip" width="500" height="430" class="size-full wp-image-1796" /><p class="wp-caption-text"><em><strong>Figure 2: </strong>Animated Explosions in MovieClip</em></p></div></p>
<p>Each tween was either 5 or 6 frames — explosions are quick and short. Further, each started in a different position on the timeline and on a different layer. Given that the same symbol was re-used on all three, I suppose I could have put them all on the same layer, but it was clearer to place the tweens on separate layers.</p>
<p>Once that was done, all I had to do was to write a script that made use of the animations in the MovieClip object. Because I set it up for Export for ActionScript, I was able to address each in turn. (The example is a test bench, and they are not integrated into the Wrong Way Warrior or connected to sounds.) The following script shows how the ActionScript controlled the MovieClip&#8217;s playhead movement.</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('p1789code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17892"><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="p1789code2"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> 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> AddScript <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Create MovieClip instance</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> boomer:<span style="color: #0066CC;">MovieClip</span>=<span style="color: #000000; font-weight: bold;">new</span> Boomer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//Starting frame</span>
		<span style="color: #0066CC;">private</span> const THROW_GRENADE:uint = <span style="color: #cc66cc;">1</span>;
		<span style="color: #0066CC;">private</span> const FIRE_ROCKET:uint = <span style="color: #cc66cc;">7</span>;
		<span style="color: #0066CC;">private</span> const LAUNCH_MORTAR:uint = <span style="color: #cc66cc;">12</span>;
		<span style="color: #808080; font-style: italic;">//Frame to trigger function (minus 1)</span>
		<span style="color: #0066CC;">private</span> const GRENADE:uint=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> const ROCKET:uint=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> const MORTAR:uint=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">17</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//UI</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grenade:<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> rocket:<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> mortar:<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> AddScript<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			boomer.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">250</span>,boomer.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">200</span>;
			boomer.<span style="color: #006600;">addFrameScript</span><span style="color: #66cc66;">&#40;</span>GRENADE,ceaseFire, ROCKET,ceaseFire, MORTAR,ceaseFire<span style="color: #66cc66;">&#41;</span>;
			boomer.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</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> 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>
			grenade.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,grenade.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">20</span>;
			grenade.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Throw Grenade&quot;</span>;
			grenade.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,fireWeapon<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenade<span style="color: #66cc66;">&#41;</span>;
			rocket.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,rocket.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			rocket.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Fire Rocket&quot;</span>;
			rocket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,fireWeapon<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocket<span style="color: #66cc66;">&#41;</span>;
			mortar.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,mortar.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">80</span>;
			mortar.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Launch Mortar&quot;</span>;
			mortar.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,fireWeapon<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mortar<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> fireWeapon<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>
			addChild<span style="color: #66cc66;">&#40;</span>boomer<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> weapon:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>weapon<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Throw Grenade&quot;</span> :
					boomer.<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span>THROW_GRENADE<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Fire Rocket&quot;</span> :
					boomer.<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span>FIRE_ROCKET<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Launch Mortar&quot;</span> :
					boomer.<span style="color: #0066CC;">gotoAndPlay</span><span style="color: #66cc66;">&#40;</span>LAUNCH_MORTAR<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> ceaseFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			boomer.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>boomer<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 setting up the constants for the values where the timeline position would fire the functions, I used expressions to remind us that the addFrameScript() function uses a zero-base numbering system for frame reference. That is, the first frame is zero (0), and all of the rest are the frame number minus 1.</p>
<p><strong>Why Undocumented?</strong></p>
<p>It always makes me nervous to get too close to an undocumented feature in ActionScript 3.0. That means at any moment the method will be deported like an unwanted immigrant. However, the method has been around since Flash CS3 and I&#8217;ve seen it referenced in some Flex listings. Why it remains undocumented is a mystery to me, but I hope that they decide to keep it or replace it with a documented method that allows direct access to the timeline with code that is in an ActionScript 3.0 file in either Flash or Flex (Flash Builder).</p>
<p>As an alternative to <strong>onEnterFrame()</strong>, this method is much cleaner and far less processor intensive. It patiently waits until the playhead reaches the trigger and then launches the appropriate function. Compare this to the <strong>onEnterFrame()</strong> method that keeps running back and fourth asking, <em>Are we there yet?</em> like some annoying kid in the back seat.</p>
<p>In the meantime, the addFrameScript() method is quite valuable for developers who want to free themselves from the limitations of script on the Timeline and keep all of the advantages of controlling the play on the Timeline. If you feel the same as I do, we can launch a whining campaign at Adobe—<em>just like the annoying kid in the backseat</em>.</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%2F09%2Fa-closer-look-using-movieclipaddframescript%2F&amp;title=A%20Closer%20Look%3A%20Using%20MovieClip.addFrameScript%28%29" 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/2009/09/a-closer-look-using-movieclipaddframescript/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Hitchhiker’s Guide to ActionScript 3.0:  Dragging through the Galaxy—Part 1</title>
		<link>http://www.as3dp.com/2009/09/hitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1/</link>
		<comments>http://www.as3dp.com/2009/09/hitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 09:00:08 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Closer Look]]></category>
		<category><![CDATA[Examples]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1455</guid>
		<description><![CDATA[I have no idea how things pop into my head or why. However, they do, and like unscratched itches, if I don’t attend to them they stay put. One such idea that cropped up was… How do you drag a live video across the screen? I have no idea why someone would want to do [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1393" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/08/closerlook.png" alt="A Closer Look" title="closerlook" width="150" height="143" class="size-full wp-image-1393" /><p class="wp-caption-text">A Closer Look</p></div>
<p>I have no idea how things pop into my head or why. However, they do, and like unscratched itches, if I don’t attend to them they stay put. One such idea that cropped up was… </p>
<blockquote><p><em>How do you drag a live video across the screen?</em></p>
</blockquote>
<p> I have no idea <em>why</em> someone would want to do that either; so don’t ask. For 95% of you reading this, the answer is simple, but for the 5% of you in rehab this post may be of some use—at least until the pink elephants go away.</p>
<p>Because this is a Design Pattern blog, I should include <em>some</em> design pattern. I could slap a Singleton in front of it, but I pretty well shot my chances of doing that with my ongoing persecution of that miserable pattern. So I need to pose my question in a broader context,</p>
<blockquote><p>What design pattern could be used to drag <em>anything</em> across the screen?</p>
</blockquote>
<p>Well, put <em>that way</em>, we can make a more interesting project. This short post simply examines how to embed and drag a non-Sprite object in a Sprite object. In Part II, we’ll see how to create a design pattern that easily allows production of objects and placing them into a Sprite instance for dragging <em>anything</em>.</p>
<p><strong>Hitch Your Wagon to a Sprite</strong></p>
<p>Back in the days before ActionScript 3.0, about the only thing you could address on the stage was a <strong>MovieClip</strong> instance—or a derivative of a MovieClip. For some of us, the lesson was too well learned, and I often think, <em>MovieClip</em> instead of <em>Sprite</em>. Anyway, you can address a Sprite just as well and you don’t have the Timeline to go along for the ride.  The trick back in the day was to dump anything you wanted to go along with the MovieClip into the movie clip. So if you had a graphic symbol, for instance, that you wanted to drag, you’d just put it into the MovieClip object and drag the movie clip. However, because the key methods of <strong>startDrag()</strong> and <strong>stopDrag()</strong> are part of the Sprite class, you can use the Sprite to do the same thing.</p>
<p><span id="more-1455"></span></p>
<p>Adding one object to another object in ActionScript 3.0 takes many forms, but when it comes to adding an object to a Sprite, a simple method is to use the <strong>addChild()</strong> method. The statement,</p>
<p>&nbsp; &nbsp; &nbsp; mySprite.addChild(myObject);</p>
<p>does the trick. By referencing all drag methods to the Sprite instance, you can drag the Sprite and anything that’s been added to it. The following program takes care of the initial query I had—it allows you to drag a video.</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('p1455code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14554"><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="p1455code4"><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;">media</span>.<span style="color: #0066CC;">Camera</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> DragVid <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> const WIDE:uint=<span style="color: #cc66cc;">320</span>;
		<span style="color: #0066CC;">private</span> const HIGH:uint=<span style="color: #cc66cc;">240</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidSled: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> 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>WIDE,HIGH<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cam:<span style="color: #0066CC;">Camera</span> = <span style="color: #0066CC;">Camera</span>.<span style="color: #006600;">getCamera</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> DragVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		setMedia<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		vidSled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>,doDrag<span style="color: #66cc66;">&#41;</span>;
		vidSled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,unDrag<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> setMedia<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			cam.<span style="color: #0066CC;">setMode</span><span style="color: #66cc66;">&#40;</span>WIDE,HIGH,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			cam.<span style="color: #0066CC;">setQuality</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #0066CC;">smoothing</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			vid.<span style="color: #006600;">attachCamera</span><span style="color: #66cc66;">&#40;</span>cam<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">100</span>,vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>vidSled<span style="color: #66cc66;">&#41;</span>;
			vidSled.<span style="color: #006600;">addChild</span><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> doDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">startDrag</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> unDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stopDrag</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>You may notice that no <strong>net</strong> elements are included, such as NetConnection or NetStream. However, they could be added for anything from a video playback to a Flash Media Interactive Server application. The objects associated with the dragging lose none of their functionality. As you can see the code sets the mode and quality of the Camera instance. Likewise, the Video instance takes advantage of mipmapping by setting the <strong>smoothing</strong> property to true.</p>
<p> Hook up your camera and give it a try by clicking the &#8220;Play&#8221; button:<br />
<a href="http://www.sandlight.net/hitchhiker/"><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>Figure 1 shows this simple process. The hand icon in the upper right corner of the video object shows where the mouse has seized the image and is dragging the video embedded in the Sprite object.<br />
<div id="attachment_1456" class="wp-caption alignleft" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/hitch.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Video hitchhiking on a Sprite gets dragged as startled subject looks on&lt;/em&gt;" title="hitch" width="500" height="385" class="size-full wp-image-1456" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Video hitchhiking on a Sprite gets dragged as startled subject looks on</em></p></div></p>
<p>Originally, I added a graphic in the Sprite to provide a visual background, but that was just added <em>bling</em> with no function other than distracting readers from the purpose of the Sprite. However, for those who like backdrops, you can add one using the graphics property and methods in the Sprite object.</p>
<p><strong>What Varies?</strong></p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/06/magicapp.png" alt="magicapp" title="magicapp" width="111" height="65" class="alignleft size-full wp-image-1071" /></p>
<p>For some food for thought for Part II of this post, get the <a href="http://www.sandlight.com/magic/"> Magic Table</a> (if you don’t already have it) to determine what variation you need to control. That will give you a guide to the design pattern we can use. (Hint: We <em>do not need to vary the sole instance of a class</em>). </p>
<p>Essentially, here’s what we need to figure out:</p>
<blockquote><p> What varies that we need to deal with when creating <em>any</em> dragable object?</p>
</blockquote>
<p>Once that question is answered, we can begin working on the program using the design pattern that takes into account the variation. In the meantime, we’d like to get your comments on both the variation and the task of dragging objects using OOP and ActionScript 3.0.</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%2F09%2Fhitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1%2F&amp;title=Hitchhiker%E2%80%99s%20Guide%20to%20ActionScript%203.0%3A%20%20Dragging%20through%20the%20Galaxy%E2%80%94Part%201" 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/2009/09/hitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>A Closer Look: Programming Library MovieClips to an Interface</title>
		<link>http://www.as3dp.com/2009/08/a-closer-look-programming-library-movieclips-to-an-interface/</link>
		<comments>http://www.as3dp.com/2009/08/a-closer-look-programming-library-movieclips-to-an-interface/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 21:05:14 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Closer Look]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1392</guid>
		<description><![CDATA[Gentle Reader Every now and then I run into something of possible interest to ActionScript 3.0 and Design Patterns. Since I spend a lot of time in the forest (Design Patterns), I may miss the trees (Program Elements). So what I may run across might be known (and obvious) to all but me. On the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1393" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/08/closerlook.png" alt="A Closer Look" title="closerlook" width="150" height="143" class="size-full wp-image-1393" /><p class="wp-caption-text">A Closer Look</p></div>
<p><em><strong>Gentle Reader</strong> Every now and then I run into something of possible interest to ActionScript 3.0 and Design Patterns. Since I spend a lot of time in the forest (Design Patterns), I may miss the trees (Program Elements).  So what I may run across might be known (and obvious) to all <em>but me</em>. On the other hand, some may find these short digressions of possible interest and use. So as to alert readers to these random snippets I put together a <strong>Closer Look</strong> logo for these small posts that live in the nooks and crannies of OOP and Design Patterns.</em></p>
<p><strong>Programming with MovieClips in the Library</strong></p>
<p>When you create a MovieClip in the Library and set it up for Export to ActionScript, you can treat it as a child of the MovieClip class.  Basically, a named MovieClip in the Library is the same as setting it up as:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <strong>public class MyMC extends MovieClip … </strong></p>
<p>One of the fundamental design pattern principles is <a href="http://www.as3dp.com/2009/02/14/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/"> program to an interface; not an implementation</a>. For example, instead of declaring a movie clip instance as,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <strong>private var myInstance: MyMC = new MyMC()</strong></p>
<p><span id="more-1392"></span><br />
we’d write it, thusly to <em>program to the interface</em>:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <strong>private var myInstance: MovieClip = new MyMC()</strong></p>
<p>We could even make it looser by typing it as a Sprite instance;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <strong>private var myInstance: Sprite = new MyMC()</strong></p>
<p>Does it really matter? I believe that programming to the interface instead of the implementation <em>definitely</em> matters, but whether it is to the Sprite or MovieClip depends on what you need.</p>
<p>Create two movie clips that you can place in the Library. One is a simple green block. The second movie clip does a shape tween turning from blue to green as it moves from left to right. Now, consider the following program:</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('p1392code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13926"><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
</pre></td><td class="code" id="p1392code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
&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> sansTL:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> conTL:<span style="color: #0066CC;">MovieClip</span>;
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//No Timeline use</span>
				sansTL=<span style="color: #000000; font-weight: bold;">new</span> NoTL<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				sansTL.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>, sansTL.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
				addChild<span style="color: #66cc66;">&#40;</span>sansTL<span style="color: #66cc66;">&#41;</span>;
				sansTL.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">20</span>;
<span style="color: #808080; font-style: italic;">//rotation property assigned value</span>
&nbsp;
				<span style="color: #808080; font-style: italic;">//Needs Timeline method</span>
				conTL =<span style="color: #000000; font-weight: bold;">new</span> McTL<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				conTL.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>, conTL.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">200</span>;
				addChild<span style="color: #66cc66;">&#40;</span>conTL<span style="color: #66cc66;">&#41;</span>;
				conTL.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">//gotoAndStop() method used</span>
<span style="color: #808080; font-style: italic;">//(cannot be used by Sprite)</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>If you type a movie clip instance as a Sprite, you can use all of the Sprite properties and methods, but you cannot use the additional MovieClip properties and methods that use the timeline. Figure 1 illustrates what the above program does.</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/sp_mc-300x214.png" alt="sp_mc" title="sp_mc" width="300" height="214" class="alignnone size-medium wp-image-1394" /></p>
<p><em><strong>Figure 1:</strong>Two differently typed movie clip instances</em></p>
<p>Like I said, this was one of those little things that hit me and amounts to no great shakes. However, if I plan to use drawn figures in the Library as imagery and nothing more, I would type them as Sprites—why bring along the Timeline if I’m not going to use it? (In <a href= "http://www.as3dp.com/2009/08/19/wrong-way-warrior-getting-flexibility-with-design-patterns—part-ii/">Part II of the Wrong Way Warrior</a>, all of the images were static MovieClip instances typed as Sprites.)</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%2F08%2Fa-closer-look-programming-library-movieclips-to-an-interface%2F&amp;title=A%20Closer%20Look%3A%20Programming%20Library%20MovieClips%20to%20an%20Interface" 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/2009/08/a-closer-look-programming-library-movieclips-to-an-interface/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

