<?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>세계의끝과 플래시 원더랜드 &#187; getDefinition</title>
	<atom:link href="http://ufx.kr/blog/tag/getdefinition/feed" rel="self" type="application/rss+xml" />
	<link>http://ufx.kr/blog</link>
	<description>Flash + ActionScript &#38; Design</description>
	<lastBuildDate>Fri, 27 Jan 2012 21:51:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기</title>
		<link>http://ufx.kr/blog/281</link>
		<comments>http://ufx.kr/blog/281#comments</comments>
		<pubDate>Sun, 05 Jul 2009 09:43:19 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[AS3.0 API]]></category>
		<category><![CDATA[addChild]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[ApplicationDomain]]></category>
		<category><![CDATA[AS3.0]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[contentLoaderInfo]]></category>
		<category><![CDATA[DisplayObject]]></category>
		<category><![CDATA[getDefinition]]></category>
		<category><![CDATA[Linkage]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[라이브러리]]></category>
		<category><![CDATA[캐스팅]]></category>
		<category><![CDATA[클래스]]></category>
		<category><![CDATA[형변환]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=281</guid>
		<description><![CDATA[이 문제는 AS3.0 의 swf 간 로드 구조를 구성할 때 반드시 한번은 만나게 되는 문제이므로 정리해 둡니다. Main.swf 에서 Sub.swf 를 Loader 를 이용해 로드한 후에 Main.swf 에서 Sub.swf 의 클래스나 메서드를 사용해야 할 때가 있습니다. AS2.0 라면 MovieClipLoader 와 loadClip 을 이용하여 로드 완료 이벤트를 확인 한 후 인스턴스네임으로 참조해 들어가면 되지만, AS3.0의 경우에는 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="blue_swf_icon" src="http://ufx.kr/blog/wp-content/uploads/2009/07/blue_swf_icon.png" alt="blue_swf_icon" width="256" height="256" />이 문제는 AS3.0 의 swf 간 로드 구조를 구성할 때 반드시 한번은 만나게 되는 문제이므로 정리해 둡니다.</p>
<p>Main.swf 에서 Sub.swf 를 Loader 를 이용해 로드한 후에 Main.swf 에서 Sub.swf 의 클래스나 메서드를 사용해야 할 때가 있습니다.</p>
<p>AS2.0 라면 MovieClipLoader 와  loadClip 을 이용하여 로드 완료 이벤트를 확인 한 후 인스턴스네임으로 참조해 들어가면 되지만, AS3.0의 경우에는 그런 방법으로 다른 swf의 클래스를 사용할수 없습니다. 이런 경우 ApplicationDomain 클래스의 getDefinition() 메서드를 이용해 참조해야 합니다.</p>
<p><span id="more-281"></span>이 예제에는 3개의 클래스가 사용됩니다. 각 클래스에 대한 설명은 본문과 코드 주석에 포함되어 있습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">// SubSystem.as 의 내용.</span>
<span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>
&nbsp;
	<span style="color: #3f5fbf;">/*
	 * 이 클래스는 별도로 SubSystem.swf로 컴파일 하여 Main.swf 에서 Loader를 통해 로드하는 대상으로 삼음
	 * 즉, 이 클래스는 SubSystem.swf 의 도큐먼트 클래스 입니다.
	 */</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SubSystem extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SubSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SubSystem !!!&quot;</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> createItem<span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span>Circle = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<span style="color: #000000;">&#40;</span> $name, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> item <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> item;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>생성자에서는 제대로 로드가 되었는지 확인하는 용도의 trace 외에는 하는 일이 없고, public 으로 공개된 createItem() 메서드는 Circle 클래스를 new 해주고, 객체를 반환하고 있습니다.</p>
<p>Circle 클래스는 예제용으로 밖에 사용할 일이 없는 궁극의 원그리기 클래스 입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">// Circle.as 의 내용.</span>
<span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Circle extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Circle<span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #0033ff; font-weight: bold;">null</span>, $x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>, $y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>, $radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">50</span>, $color<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = 0x000000, $alpha<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span> $x, $y, $radius, $color, $alpha <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>._name = $name
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span> $x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>, $color<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>, $alpha<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			item.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> $color, $alpha <span style="color: #000000;">&#41;</span>;
			item.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> $x, $y, $radius <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> item <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> item
		<span style="color: #000000;">&#125;</span>
&nbsp;
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">name</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _name; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>SubSystem 클래스에서 Circle 클래스를 따로 import 하지 않았으므로, 이 두개의 클래스는 같은 폴더에 넣어야 동작하겠군요.</p>
<p>다음은 Main 클래스 입니다. 이 파일은 Main.swf 에 사용되는 도큐먼트 클래스 입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Main.as 의 내용.</span>
<span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">DisplayObject</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Loader</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #004993;">URLRequest</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Loader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Loader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> request<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SubSystem.swf&quot;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//Loader는 직접 addEventListener 하지 못하고 contentLoaderInfo 를 통하여야 함</span>
			<span style="color: #004993;">loader</span>.<span style="color: #004993;">contentLoaderInfo</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, completeHandler <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">loader</span>.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span> request <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> completeHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> runtimeClassRef<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Class</span> = <span style="color: #004993;">loader</span>.<span style="color: #004993;">contentLoaderInfo</span>.<span style="color: #004993;">applicationDomain</span>.<span style="color: #004993;">getDefinition</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SubSystem&quot;</span> <span style="color: #000000;">&#41;</span>  <span style="color: #0033ff; font-weight: bold;">as</span>  <span style="color: #004993;">Class</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> displayObj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span> = <span style="color: #0033ff; font-weight: bold;">new</span> runtimeClassRef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//addChild 하려면 DisplayObject 로 캐스팅</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> displayObj <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> subSystemObj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = displayObj <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Object</span>; <span style="color: #009900;">//메서드를 호출하려면 다시 Object 로 캐스팅</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> item1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = subSystemObj.createItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;item1&quot;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> item2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = subSystemObj.createItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;item2&quot;</span> <span style="color: #000000;">&#41;</span>;
			item2.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span>;
&nbsp;
			<span style="color: #009900;">//로드한 SubSystem.swf 의 자원은 이제 subSystemObj 로 참조하면 됩니다.</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;subSystemObj.numChildren : &quot;</span>, subSystemObj.<span style="color: #004993;">numChildren</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>getDefinition() 과는 직접 관련은 없지만, 한가지 짚고 넘어가야할 내용이 있습니다. Loader 의 다운로드 진행상황이나 로드 완료 등의 이벤트를 받고 싶을 경우 Loader 에 contentLoaderInfo 를 통해 addEventListener 를 걸어야 합니다.<sup>[<a href="http://ufx.kr/blog/281#footnote_0_281" id="identifier_0_281" class="footnote-link footnote-identifier-link" title=" 로더객체.contentLoaderInfo.addEventListener(&amp;#8230;&amp;#8230;) ">01</a>]</sup> Loader 에 직접 addEventListener 를 걸고 아무리 기다려본들 이벤트는 발생하지 않습니다.<sup>[<a href="http://ufx.kr/blog/281#footnote_1_281" id="identifier_1_281" class="footnote-link footnote-identifier-link" title=" 로더객체.addEventListener(&amp;#8230;&amp;#8230;) &amp;lt;&amp;#8211; (잘못된 사용) ">02</a>]</sup></p>
<p>다시 내용으로 돌아와서, Main.as 는 SubSystem.swf 를 Loader 를 통해 로드하고, 로드 완료된 이벤트 확인한 이후, SubSystem 의 내부 메서드를 호출하여 SubSystem 로 하여금 Circle 객체를 new 로 생성하게 하고 있습니다.</p>
<p>만약 SubSystem.swf 를 통해 얻고 싶은 것이 fla의 라이브러리에 Class 네임으로 정의한<sup>[<a href="http://ufx.kr/blog/281#footnote_2_281" id="identifier_2_281" class="footnote-link footnote-identifier-link" title="AS2.0의 Linkage 사용하듯">03</a>]</sup> 무비클립이라면 getDefinition( &#8220;ClassName&#8221; ) 과 같이 해당 무비클립의 클래스 네임을 넣어주고 addChild 까지만 해주면 됩니다. 메서드 호출 까지도 필요 하지 않은 경우라면 이렇게 훨씬 더 간단하죠.</p>
<p>참고 : Main.swf 가 AS3.0 이고 SubSystem.swf 가 AS2.0 인 경우 ApplicationDomain 은 사용할 수 없습니다.</p>
<p>관련 레퍼런스 링크<br />
<a href="http://help.adobe.com/ko_KR/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e07.html" target="_blank">ApplicationDomain 클래스 사용</a><br />
<a href="http://help.adobe.com/ko_KR/AS3LCR/Flash_10.0/flash/system/ApplicationDomain.html" target="_blank">ActionScript 3.0 언어 및 구성 요소 참조 설명서 : ApplicationDomain</a></p>
<p><span style="color: #808080;">* 다른 swf 가 아닌 하나의 swf에서 fla 라이브러리에 정의한 클래스 이름으로 무비클립을 사용하려면 &#8220;<a href="./269">AS3.0 getDefinitionByName() 메서드를 이용한 라이브러리 자원 이용 – AS2.0 attachMovie 대응</a>&#8221; 을 읽어보세요..</span></p>
<ol class="footnotes"><li id="footnote_0_281" class="footnote"> 로더객체.contentLoaderInfo.addEventListener(&#8230;&#8230;) </li><li id="footnote_1_281" class="footnote"> 로더객체.addEventListener(&#8230;&#8230;) &lt;&#8211; (잘못된 사용) </li><li id="footnote_2_281" class="footnote">AS2.0의 Linkage 사용하듯</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/281/feed</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

