<?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; getDefinitionByName</title>
	<atom:link href="http://ufx.kr/blog/tag/getdefinitionbyname/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>도큐먼트 클래스 *.as 파일을 연결하지 않은 fla 파일의 도큐먼트 클래스 이름은?</title>
		<link>http://ufx.kr/blog/393</link>
		<comments>http://ufx.kr/blog/393#comments</comments>
		<pubDate>Thu, 25 Feb 2010 04:24:33 +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[class]]></category>
		<category><![CDATA[FD]]></category>
		<category><![CDATA[flashdevelop]]></category>
		<category><![CDATA[getChildByName]]></category>
		<category><![CDATA[getDefinitionByName]]></category>
		<category><![CDATA[getter]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[MainTimeline]]></category>
		<category><![CDATA[setter]]></category>
		<category><![CDATA[런타임]]></category>
		<category><![CDATA[무비클립]]></category>
		<category><![CDATA[클래스]]></category>
		<category><![CDATA[타임라인]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=393</guid>
		<description><![CDATA[작년 7월에 작성한 &#8220;AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기&#8221; 를 보면, ApplicationDomain 클래스의 getDefinition() 메서드를 이용해서 다른 로더를 통해 가지고 온 다른 swf 파일의 클래스를 사용하는 방법을 알아본 바 있습니다. 그런데 이 포스트에 &#8220;료&#8221;님이, fla 파일의 타임라인에 직접 코딩한 경우에는 어떻게 해야 하느냐고 질문하셨길래 방법을 찾아 보았습니다. 우리가 Fla 에 도큐먼트 클래스를 연결해서 [...]]]></description>
			<content:encoded><![CDATA[<p>작년 7월에 작성한 <a href="http://ufx.kr/blog/281">&#8220;AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기&#8221;</a> 를 보면, ApplicationDomain 클래스의 getDefinition() 메서드를 이용해서 다른 로더를 통해 가지고 온 다른 swf 파일의 클래스를 사용하는 방법을 알아본 바 있습니다.<br />
그런데 이 포스트에 &#8220;료&#8221;님이, fla 파일의 타임라인에 직접 코딩한 경우에는 어떻게 해야 하느냐고 질문하셨길래 방법을 찾아 보았습니다.</p>
<p>우리가 Fla 에 도큐먼트 클래스를 연결해서 swf 를 만들었을 경우, Main.as 도큐먼트 클래스에서 trace( this ) 를 하면 [object Main] 이 출력 됩니다. <a href="http://ufx.kr/blog/281">&#8220;AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기&#8221;</a> 의 예제 코드를 보면 다른 swf 의 도큐먼트 클래스의 이름을 통해 런타임으로 클래스로 만들고 그것을 객체화 하여 도큐먼트 클래스 내부에 있는 여러가지 자원들을 참조하게 됩니다. 도큐먼트 클래스의 이름이 곧 ApplicationDomain 이 되는 것이죠. 그것은 결국 도큐먼트 클래스의 클래스 이름을 알 수 없다면 getDefinition() 으로는 참조가 불가능하다는 이야기가 됩니다.</p>
<p>그런데 만약 도큐먼트 클래스를 사용하지 않은 fla 파일로 만든 swf 파일이라면 어떻게 되는 걸까요? <a href="http://ufx.kr/blog/281/comment-page-1#comment-1089">&#8220;료&#8221;님의 질문</a>을 받고 저역시 이 부분에서 살짝 당황했습니다. 도큐먼트 클래스 as 파일이 없는데 도큐먼트 클래스의 이름을 어떻게 알아낼 수 있을까 하는 것이었죠.</p>
<p><span id="more-393"></span>그런데 결국 추론를 통해 도큐먼트 클래스의 이름을 알아냈습니다.<br />
라이브러리에 클래스 정의를 한 무비클립에도 물리적 as 파일을 연결하지 않으면, 컴파일 과정에서 자동으로 as 파일을 생성한다는것에 힌트를 얻었습니다.</p>
<div id="attachment_355" class="wp-caption aligncenter" style="width: 400px"><a href="http://ufx.kr/blog/wp-content/uploads/2009/12/making_class_alert.png"><img class="size-full wp-image-355" title="making_class_alert" src="http://ufx.kr/blog/wp-content/uploads/2009/12/making_class_alert.png" alt="" width="390" height="203" /></a><p class="wp-caption-text">라이브러리의 속성창에서 클래스 이름만 써 넣고 as 파일은 연결하지 않을때 나타나는 경고 다이알로그 박스</p></div>
<p>위의 다이알로그 박스의 메세지 내용인 즉슨, 클래스가 필요한 객체는 어떠한 형태로든 클래스 파일을 생성한다는 것이죠. 이와 마찬가지로 Flash IDE에서 도큐먼트 클래스 없이 컴파일을 하더라도 어딘가 가상으로 파일을 만들어 낼 거라는 생각이 들었습니다.</p>
<p>이때 빛을 발휘하는 프로그램이 바로 FlashDevelop(이하 FD) 이죠. FD 의 Project 패널은 as 파일과 디렉토리를 네비게이션 하는 1차적 용도를 가지고 있지만, 컴파일된 swf 파일이나 swc 파일의 구조를 분석할 수 있도록 기능도 가지고 있습니다.</p>
<div id="attachment_394" class="wp-caption aligncenter" style="width: 279px"><a href="http://ufx.kr/blog/wp-content/uploads/2010/02/fla_getDefinition_1.png"><img class="size-full wp-image-394" title="fla_getDefinition_1" src="http://ufx.kr/blog/wp-content/uploads/2010/02/fla_getDefinition_1.png" alt="" width="269" height="228" /></a><p class="wp-caption-text">FlashDevelop 의 Project 패널</p></div>
<p>FlaSubSystem.fla 은 도큐먼트 클래스가 없는 fla 파일 입니다. 이 파일로 FlaSubSystem.swf 파일을 컴파일 했습니다. 이 파일의 1프레임에는 간단한 무비클립과 코드 몇줄을 넣어놓았습니다.<br />
Main.as 파일은 FD에서 프로젝트를 생성할때 만든 도큐먼트 클래스 입니다. 이 도큐먼트 클래스로는 GetDefinition.swf 파일을 만들었죠.</p>
<p>그러니까 <a href="http://ufx.kr/blog/281">&#8220;AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기&#8221;</a> 의 내용과 매칭하자면 FlaSubSystem.swf 파일이 SubSystem.swf 이 되는 것입니다.</p>
<p>컴파일된 FlaSubSystem.swf 파일의 왼쪽에 있는 + 기호를 이용해 펼치면 swf 파일 내부에 정의되어 있는 클래스들을 모두 볼 수 있는데요, 보시는 바와 같이 <span style="color: #33cccc;">fla 의 파일명에 _fla 라는 문자가 붙고 점 찍은 후</span>에 MainTimeline 이 클래스 화 되어 있는것을 볼 수 있습니다. 1프레임에 코드를 넣게 되면 위와 같이 MainTimeline 이 클래스 화 되고, 프레임에 코드가 없으면 클래스가 생성되지 않습니다.</p>
<p>그러면 Main.as 파일의 코드를 살펴보겠습니다.</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: #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>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.system</span>.<span style="color: #004993;">ApplicationDomain</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;FlaSubSystem.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: #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;">INIT</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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span> <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;FlaSubSystem_fla.MainTimeline&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>
&nbsp;
			<span style="color: #009900;">// 아래의 member 들은 모두 fla 파일의 stage 또는 타임라인의 1프레임에 정의한 내용.</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> subSystemObj.myInstance <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// stage 에 인스턴스네임 부여한 무비클립 객체. 참조 가능 // 출력 : [object MovieClip]</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> subSystemObj.myString <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 변수에는 접근안됨 // 출력 : null</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> subSystemObj.myGetter <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// getter/setter 메서드 접근 가능</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> subSystemObj.myFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 함수 접근 가능</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() 메서드를 보면 fla 파일의 메인 타임라인 클래스에 접근하기 위해 FlaSubSystem_fla.MainTimeline 라는 문자열을 사용한것을 볼 수 있습니다. 이 ApplicationDomain 을 런타임 클래스 화 하고 객체로 만들어 내는 과정은 동일합니다.</p>
<p>각 요소들에 대한 참조 여부는 코드 내부의 주석에 적었으므로 그것으로 설명을 대신합니다.</p>
<p>타임라인 코드에 var 키워드를 이용해 생성한 변수에는 접근이 되지 않는데, 차후에 방법을 알게되면 추가하겠습니다. 그러나 getter/setter 가 가능하므로 굳이 필요하다면 변수 대신 사용할 수 있을것 같습니다.</p>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/393/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>AS3.0 getDefinitionByName() 메서드를 이용한 라이브러리 자원 이용 &#8211; AS2.0 attachMovie 대응</title>
		<link>http://ufx.kr/blog/269</link>
		<comments>http://ufx.kr/blog/269#comments</comments>
		<pubDate>Wed, 24 Jun 2009 17:03:53 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[AS3.0 API]]></category>
		<category><![CDATA[addChild]]></category>
		<category><![CDATA[AS2.0]]></category>
		<category><![CDATA[AS3.0]]></category>
		<category><![CDATA[attachMovie]]></category>
		<category><![CDATA[DisplayObject]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[getDefinitionByName]]></category>
		<category><![CDATA[Linkage]]></category>
		<category><![CDATA[MovieClip]]></category>
		<category><![CDATA[Sprite]]></category>
		<category><![CDATA[동적]]></category>
		<category><![CDATA[라이브러리]]></category>
		<category><![CDATA[링키지]]></category>
		<category><![CDATA[무비클립]]></category>
		<category><![CDATA[참조]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=269</guid>
		<description><![CDATA[AS2.0 시절, 프레임에서 신나게 코딩할때 라이브러리 패널에 링키지 네임을 준 무비클립을 attachMovie() 메서드를 이용해 스테이지에 불러와서 자유롭게 사용할 수 있었습니다. AS3.0 에 들어와서는 라이브러리 패널의 링키지는 사용할 수 없지만 클래스 네임을 부여하여 별도의 클래스 파일을 만들지 않고도[01] new ClassName() 을 하여 마찬가지의 기능을 수행할 수 있습니다. 그런데 new ClassName() 에서 ClassName 부분은 정확한 클래스 이름이 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://ufx.kr/blog/wp-content/uploads/2009/07/blue_swf_icon.png" alt="blue_swf_icon" title="blue_swf_icon" width="256" height="256" class="alignleft size-full wp-image-282" />AS2.0 시절, 프레임에서 신나게 코딩할때 라이브러리 패널에 링키지 네임을 준 무비클립을 attachMovie() 메서드를 이용해 스테이지에 불러와서 자유롭게 사용할 수 있었습니다.</p>
<p>AS3.0 에 들어와서는 라이브러리 패널의 링키지는 사용할 수 없지만 클래스 네임을 부여하여 별도의 클래스 파일을 만들지 않고도<sup>[<a href="http://ufx.kr/blog/269#footnote_0_269" id="identifier_0_269" class="footnote-link footnote-identifier-link" title="정확하게는 컴파일 타임에 자동으로 클래스 파일이 생성됩니다. 그러나 개발자가 이에 관여할 필요는 없습니다.">01</a>]</sup> new ClassName() 을 하여 마찬가지의 기능을 수행할 수 있습니다.</p>
<p>그런데 new ClassName() 에서 ClassName 부분은 정확한 클래스 이름이 와야 하고, this["str"] 나 _root["str"] 같이 AS2.0에서 흔히 사용했던 레퍼런스 참조 방법을 new 연산자 다음에는 사용할 수 없으므로 AS3.0에서 new ClassName() 으로는 아래와 같은 AS2.0의 for 문을 이용한 동적인 attachMovie() 는 사용할수가 없습니다.<sup>[<a href="http://ufx.kr/blog/269#footnote_1_269" id="identifier_1_269" class="footnote-link footnote-identifier-link" title=" new this[&quot;mc&quot;+i]() 이런식으로 될리가 없다는거죠. ">02</a>]</sup></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//AS2.0의 동적 attachMovie 예시</span>
<span style="color: #000000; font-weight: bold;">var</span> arr:<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;메뉴1&quot;</span>, <span style="color: #ff0000;">&quot;메뉴2&quot;</span>, <span style="color: #ff0000;">&quot;메뉴3&quot;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">var</span> len:<span style="color: #0066CC;">Number</span> = arr.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> len; i++ <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	 <span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;mc&quot;</span>, <span style="color: #ff0000;">&quot;mc&quot;</span>+i, i <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>그럼 어떻게 해야할까요?</p>
<p><span id="more-269"></span>먼저 라이브러리 패널에 있는 무비클립 심볼의 속성창을 열고 아래와 같이 클래스 이름을 지정해 줍니다.<br />
<img src="http://ufx.kr/blog/wp-content/uploads/2009/06/lib_properties.png" alt="lib_properties" title="lib_properties" width="599" height="339" class="alignnone size-full wp-image-270" /><br />
클래스 이름은 for 문의 i 값으로 호출할 수 있게끔 숫자를 붙여 순차적으로 지정합니다. 그리고,</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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//AS3.0에서 라이브러리의 클래스 이름을 이용하여 무비클립을 동적으로 사용</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;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; 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> ClassReference<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Class</span> = <span style="color: #004993;">getDefinitionByName</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;mc&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> i <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> ClassReference<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	displayObj.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, clickHandler <span style="color: #000000;">&#41;</span>;
	displayObj.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;mc&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> i;
	displayObj.<span style="color: #004993;">x</span> = i <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">51</span>;
&nbsp;
	<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> displayObj <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</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: #004993;">trace</span><span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">currentTarget</span>.<span style="color: #004993;">name</span> <span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>이렇게 getDefinitionByName() 메서드를 이용해 Class의 레퍼런스로 캐스팅하고 new 연산자로 클래스를 호출하여 DisplayObject 객체에 담을 수 있습니다.<br />
클래스가 다형성을 가지지 않고 이렇게 클래스의 이름을 이용하여 호출하는 주객이 전도가 된듯한 사용법은 객체지향 프로그래밍과는 약간 거리가 있는것 같긴 합니다만, 이런 사용법을 원하는 분들이 있고, 어도비에서도 이런 메서드를 제공하고 있으니 필요하다면 사용할 수 있는 거겠죠.</p>
<p>만약 buttonMode 등의 Sprite 속성을 사용해야 할 경우 DisplayObject 에는 해당 속성이 없으므로 아래와 같이 Sprite 로 캐스팅을 해야 합니다.</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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><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;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; 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> ClassReference<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Class</span> = <span style="color: #004993;">getDefinitionByName</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;mc&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> i <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> ClassReference<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">//buttonMode 등의 Sprite 속성을 사용하려면 한번 더 캐스팅</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> instance<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = displayObj <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Sprite</span>;
	instance.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
&nbsp;
	instance.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, clickHandler <span style="color: #000000;">&#41;</span>;
	instance.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;mc&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> i;
	instance.<span style="color: #004993;">x</span> = i <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">51</span>;
&nbsp;
	<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> instance <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</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: #004993;">trace</span><span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">currentTarget</span>.<span style="color: #004993;">name</span> <span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://help.adobe.com/ko_KR/AS3LCR/Flash_10.0/flash/utils/package.html#getDefinitionByName()" target="_blank">관련 레퍼런스</a></p>
<a href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=13" title="Downloaded 372 times"><img src="http://ufx.kr/blog/wp-content/uploads/2009/07/zip_icon.gif" />&nbsp; getDefinitionByName 예제 다운로드</a> - fla (Flash CS4에서 CS3 버전으로 저장, Flash 8 이하에서는 열리지 않습니다) , swf
<p><span style="color: #808080;">* Loader 를 통해 로드한 다른 swf 의 라이브러리 자원 또는 클래스를 사용하는 방법은 &#8220;<a href="./281">AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기</a>&#8221; 를 읽어보세요.</span></p>
<ol class="footnotes"><li id="footnote_0_269" class="footnote">정확하게는 컴파일 타임에 자동으로 클래스 파일이 생성됩니다. 그러나 개발자가 이에 관여할 필요는 없습니다.</li><li id="footnote_1_269" class="footnote"> new this["mc"+i]() 이런식으로 될리가 없다는거죠. </li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/269/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

