<?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>digitaldogbyte.com &#187; dynamically attach</title>
	<atom:link href="http://www.digitaldogbyte.com/tag/dynamically-attach/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.digitaldogbyte.com</link>
	<description>powered by kilobits n' bytes</description>
	<lastBuildDate>Sat, 16 Jul 2011 03:01:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Dynamically attaching library objects in ActionScript 3.0</title>
		<link>http://www.digitaldogbyte.com/2008/07/02/dynamically-attaching-library-objects-in-actionscript-30/</link>
		<comments>http://www.digitaldogbyte.com/2008/07/02/dynamically-attaching-library-objects-in-actionscript-30/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 21:39:47 +0000</pubDate>
		<dc:creator>Herm Wong</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[dynamically attach]]></category>

		<guid isPermaLink="false">http://www.digitaldogbyte.com/?p=6</guid>
		<description><![CDATA[There are some small differences in syntax when dynamically attaching library objects in ActionScript 2.0 and ActionScript 3.0. In this post, I will provide a very simplified example in both ActionScript 2.0 and ActionScript 3.0. ActionScript 2.0 Example: You will need to make sure the library object that you are trying to attach has been [...]]]></description>
			<content:encoded><![CDATA[<p>There are some small differences in syntax when dynamically attaching library objects in ActionScript 2.0 and ActionScript 3.0.</p>
<p>In this post, I will provide a very simplified example in both ActionScript 2.0 and ActionScript 3.0.</p>
<p><strong>ActionScript 2.0 Example:</strong></p>
<p>You will need to make sure the library object that you are trying to attach has been configured, to do this you need to right click on the object in the library, next select properties and then check the &#8220;Export for ActionScript&#8221; and &#8220;Export in first frame&#8221; options. Make sure to enter an Identifier name for the library object; the Identifier name is used to reference the library object in when you try to use it in the ActionScript code.</p>
<p>In this example we will use &#8220;myClip&#8221; as the Identifier name.</p>
<p><img src="/images/dynamic_attach/as2_export.jpg" /></p>
<p>Below is the ActionScript 2.0 code that will dynamically create 5 instances of the myClip object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> numberOfClips<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> xStart<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> yStart<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> xVal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = xStart<span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> xOffset<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">&lt;</span>numberofclips<span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span> 
     <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>attachMovie<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span>i<span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>getNextHighestDepth<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
     <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>_y = yStart<span style="color: #000066; font-weight: bold;">;</span> 
     <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>_x = xVal<span style="color: #000066; font-weight: bold;">;</span> 
     xVal = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>_x <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>_width <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>xOffset<span style="color: #000066; font-weight: bold;">;</span> 
     <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>label_txt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = i<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>ActionScript 3.0 Example:</strong></p>
<p>You will need to make sure the library object that you are trying to attach has been configured, to do this you need to right click on the object in the library, next select properties and then check the &#8220;Export for ActionScript&#8221; and &#8220;Export in first frame&#8221; options. Make sure to enter an Class name for the library object; the Class name is used to reference the library object in when you try to use it in the ActionScript code. You don&#8217;t need to worry about the Base class, Flash will set a default Base class.</p>
<p><em>If you are attempting to attach a custom class that you created, you will need to use the Class name of the your custom class; you may also need to set the Base class if your custom class is derived from another custom class that you have created. </em></p>
<p><img src="/images/dynamic_attach/as3_export.jpg" /></p>
<p>When you check the &#8220;Export for ActionScript&#8221; option, you will likely get a warning telling you that there is no class file found; for this basic example you can just click OK.</p>
<p><img src="/images/dynamic_attach/as3_export_class_warning.jpg" /></p>
<p>Below is the ActionScript 3.0 code that will dynamically create 5 instances of the myClip object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> numberOfClips<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> xStart<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> yStart<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> xVal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = xStart<span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #6699cc; font-weight: bold;">var</span> xOffset<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">&lt;</span>numberofclips<span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span> 
     <span style="color: #6699cc; font-weight: bold;">var</span> mc<span style="color: #000066; font-weight: bold;">:</span>myClip = <span style="color: #0033ff; font-weight: bold;">new</span> myClip<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
     mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;myClip&quot;</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
     <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span> <span style="color: #000000;">&#40;</span>mc<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
     mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = yStart<span style="color: #000066; font-weight: bold;">;</span> 
     mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = xVal<span style="color: #000066; font-weight: bold;">;</span> 
     xVal = mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>xOffset<span style="color: #000066; font-weight: bold;">;</span> 
     mc<span style="color: #000066; font-weight: bold;">.</span>label_txt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Here are the <a href="/downloads/dynamic_attach_example.zip">example source files</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.digitaldogbyte.com/2008/07/02/dynamically-attaching-library-objects-in-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

