<?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>mandarin.no &#187; swf</title>
	<atom:link href="http://mandarin.no/tag/swf/feed/" rel="self" type="application/rss+xml" />
	<link>http://mandarin.no</link>
	<description></description>
	<lastBuildDate>Tue, 17 Aug 2010 09:35:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Actionscript: Accessing MovieClips from an externally loaded SWF</title>
		<link>http://mandarin.no/as3/actionscript-accessing-movieclips-from-an-externally-loaded-swf/</link>
		<comments>http://mandarin.no/as3/actionscript-accessing-movieclips-from-an-externally-loaded-swf/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 22:51:39 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[assets]]></category>
		<category><![CDATA[big spaceship]]></category>
		<category><![CDATA[bulkloader]]></category>
		<category><![CDATA[document class]]></category>
		<category><![CDATA[external]]></category>
		<category><![CDATA[getdefinitionbyname]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[swf]]></category>
		<guid isPermaLink="false">http://mandarin.no/?p=26</guid>
		<description><![CDATA[For the time I&#8217;m working on a website and so far I&#8217;ve only spent some time doing research around all the ideas I have. One of them was to put all MovieClips in one externally loaded SWF. Loading the SWF is easy, but how do you access the individual MovieClips? Easy as 1-2-3! For loading [...]]]></description>
			<content:encoded><![CDATA[<p>For the time I&#8217;m working on a website and so far I&#8217;ve only spent some time doing research around all the ideas I have. One of them was to put all MovieClips in one externally loaded SWF. Loading the SWF is easy, but how do you access the individual MovieClips? Easy as 1-2-3!</p>
<p>For loading the assets I use <a href="http://code.google.com/p/bulk-loader/">BulkLoader</a>; so far it seems like a pretty good alternative to writing a bunch of code myself. I like.</p>
<p>In the assets.swf I have only stuffed the library with MovieClips, and the ones I&#8217;d like to access from the outside are set to linked. Nothing is placed on stage. The document root to the SWF is set to a custom class. It is within this class the trick lays. And the trick is to set up a public function that returns any MovieClip to the parent SWF.</p>
<p>The parent SWF loads assets.swf and puts it in a variable. The public function in the assets document class is now accessible by dot syntax. By calling this function we can easily add MovieClips from the assets library to any MovieClip in the parent SWF.</p>
<p>Here&#8217;s the document class for assets.swf:</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
16
17
18
</pre></td><td class="code"><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> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getDefinitionByName</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Assets <span style="color: #0066CC;">extends</span> <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> Assets<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAsset<span style="color: #66cc66;">&#40;</span>s:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> a:<span style="color: #000000; font-weight: bold;">Class</span> = getDefinitionByName<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> a<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>What this does is first of all import <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName()">getDefinitionByName</a> from the flash.utils package. This is a function that will return any object based on the string you give it. Second, and last, is setting up a generic public function that will look for and return an object with the name you provide it with.</p>
<p>In the parent SWF you would set up an eventHandler that puts the loaded SWF in a variable, and then call getAsset(). Like this (note that I&#8217;m using BulkLoader):</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> mclibLoaded<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> lib:<span style="color: #0066CC;">MovieClip</span> = loader.<span style="color: #006600;">getContent</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mclib&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> p1:<span style="color: #0066CC;">MovieClip</span> = lib.<span style="color: #006600;">getAsset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Page2&quot;</span><span style="color: #66cc66;">&#41;</span>;
	keeper.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>p1<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>
<p>loader is the name of the BulkLoader object, and keeper is a MovieClip on stage.</p>
<p>Rather easy, right?</p>
<p>If you&#8217;re interested in knowing more about <a href="http://code.google.com/p/bulk-loader/">BulkLoader</a>, take a look at the <a href="http://code.google.com/p/bulk-loader/w/list">documentation</a>.<br />
The generic function I&#8217;ve written here was taken from <a href="http://labs.bigspaceship.com/2007/11/30/as3-linked-movieclips-from-remote-swfs/">Big Spaceship Labs</a>. They did it in a more elegant way than I initially did. Don&#8217;t thank me, thank them.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/actionscript-accessing-movieclips-from-an-externally-loaded-swf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
