<?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; 참조</title>
	<atom:link href="http://ufx.kr/blog/tag/%ec%b0%b8%ec%a1%b0/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 클래스 구조의 여러가지 상황에서 부모, 자식 객체의 참조 방법</title>
		<link>http://ufx.kr/blog/404</link>
		<comments>http://ufx.kr/blog/404#comments</comments>
		<pubDate>Mon, 29 Mar 2010 09:35:02 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[고수들은 가르쳐주지 않는 AS3.0 입문]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[dispat]]></category>
		<category><![CDATA[dispatchEvent]]></category>
		<category><![CDATA[DisplayObject]]></category>
		<category><![CDATA[DisplayObjectContainer]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[EventDispatcher]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[고수들는 가르쳐주지 않아요]]></category>
		<category><![CDATA[무비클립]]></category>
		<category><![CDATA[이벤트]]></category>
		<category><![CDATA[참조]]></category>
		<category><![CDATA[통신]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=404</guid>
		<description><![CDATA[fla 파일의 프레임에 코드를 잔뜩 늘어놓았던 시절에는, 모든 함수와 변수의 스코프가 동일하므로 참조하는 방법에 대한 고민을 거의 하지 않아도 좋았습니다. 말하자면 fla 프레임의 코드는 하나의 클래스이면서, 생성자 함수외에는 존재하지 않는 클래스라고 볼 수 있을겁니다. 그러나 클래스를 이용하여 객체를 생성하는 경우에는 그렇게 단순하게만 돌아가지는 않습니다. 필연적으로, 원하지 않아도 부모 객체와 자식 객체를 생성하게 되는데, 몇 가지 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-406" title="hidden_object" src="http://ufx.kr/blog/wp-content/uploads/2010/03/hidden_object.jpg" alt="" width="300" height="200" />fla 파일의 프레임에 코드를 잔뜩 늘어놓았던 시절에는, 모든 함수와 변수의 스코프가 동일하므로 참조하는 방법에 대한 고민을 거의 하지 않아도 좋았습니다. 말하자면 fla 프레임의 코드는 하나의 클래스이면서, 생성자 함수외에는 존재하지 않는 클래스라고 볼 수 있을겁니다.</p>
<p>그러나 클래스를 이용하여 객체를 생성하는 경우에는 그렇게 단순하게만 돌아가지는 않습니다. 필연적으로, 원하지 않아도 부모 객체와 자식 객체를 생성하게 되는데, 몇 가지 규칙을 알고 있어야만 올바른 객체간 통신을 할 수 있게 됩니다.</p>
<p>이 내용은 AS2.0을 다루던 초보 개발자들이 AS3.0에 와서 가장 먼저 부딪히는 부분이며, 가장 많이 헤메는 부분이기도 합니다. 이제까지 MovieClip 이나 Sprite 객체를 만들어 마우스 클릭 이벤트만 걸고 노는 것에만 익숙했던 분들은 이 포스트를 정독하시면 많은 것을 얻으실 수 있을겁니다. 그런 즉슨 이 포스트는 AS3.0 에 막 입문한 초보분들을 위한 포스트 입니다.</p>
<p>아래에 설명한 방법들은 객체간 통신을 하기 위해 사용하는 여러가지 방법들 입니다. 물론 이 외에도 다른 방법이 있을 수 있습니다만, 가장 사용빈도가 높고 반드시 알아야 하는 몇 가지 방법을 소개하겠습니다.</p>
<p><span id="more-404"></span></p>
<h3>A. 부모 객체가 자식 객체를 참조</h3>
<p>객체간 관계에서 가장 흔한 상황이 되겠습니다. 부모 객체는 다수의 자식 객체를 가질 수 있고, 자식 객체에 public 으로 선언된 변수나 함수에 대해서는 자식 객체의 객체이름을 통하여 참조할 수 있다는것을 모두들 잘 알고 계실겁니다. 코드로 보면 아래와 같죠.<br />
아래는 부모 객체를 생성한 부모 클래스의 코드 입니다.</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
</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;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ParentClass 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> ParentClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> _childInstance<span style="color: #000000; font-weight: bold;">:</span>ChildClass = <span style="color: #0033ff; font-weight: bold;">new</span> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _childInstance <span style="color: #000000;">&#41;</span>;
			_childInstance.doSomething<span style="color: #000000;">&#40;</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>아래는 자식 객체를 생성한 자식 클래스의 코드 입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
</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;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChildClass 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> doSomething<span style="color: #000000;">&#40;</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> <span style="color: #990000;">&quot;하위 클래스 : do something in ChildClass !!&quot;</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>

<h3>B. 자식 객체가 부모 객체를 참조 &#8211; 첫 번째 방법</h3>
<p>자, 그럼 거꾸로 자식 객체가 부모 객체에 있는 함수나 변수를 참조하려면 어떻게 하면 될까요? 자식 객체에서 parent 키워드를 사용하면 자신을 addChild 한 부모에게 접근할 수 있습니다만, DisplayObject 상속구조상의 메서드, 속성만 사용 가능할뿐 부모 객체에 정의된 것을 사용할 수는 없습니다.<br />
이것의 논리적 원인을 따져보자면 이렇습니다. 우리가 부모, 자식 객체를 맺어주는데 사용한 메서드는 addChild() 입니다. addChild() 메서드는 Sprite 클래스가 바로 윗 단계에서 상속한 DisplayObjectContainer 클래스의 메서드죠. 아래의 레퍼런스 스샷에서 보시는 바와 같이 parent 를 사용해도 반환되어 나오는 것은 DisplayObjectContainer 입니다.</p>
<div id="attachment_405" class="wp-caption alignnone" style="width: 610px"><a href="http://help.adobe.com/ko_KR/AS3LCR/Flash_10.0/flash/display/DisplayObjectContainer.html"><img class="size-full wp-image-405" title="DisplayObjectContainer_parent" src="http://ufx.kr/blog/wp-content/uploads/2010/03/DisplayObjectContainer_parent.png" alt="" width="600" height="146" /></a><p class="wp-caption-text">DisplayObjectContainer 의 읽기 전용 속성인 parent. 반환되어 나오는 것도 DisplayObjectContainer.</p></div>
<p>다시말해, DisplayObject 상속 체인 구조<sup>[<a href="http://ufx.kr/blog/404#footnote_0_404" id="identifier_0_404" class="footnote-link footnote-identifier-link" title="Object &amp;#8211; EventDispatcher &amp;#8211; DisplayObject &amp;#8211; InteractiveObject &amp;#8211; DisplayObjectContainer &amp;#8211; Sprite">01</a>]</sup> 에서 부모와 자식 객체간의 결합은 DisplayObjectContainer 가 담당하고 있다는 것이죠&#8230; 라는 내용은 당연스러운 사실이지만, 무심코 사용한 parent 역시 DisplayObjectContainer 형(type)을 반환 하므로, DisplayObjectContainer 를 상속한 Sprite 도, 그 Sprite 를 상속한 개별 클래스(ParentClass) 역시 DisplayObjectContainer 까지의 메서드, 속성만 사용할 수 있습니다.<sup>[<a href="http://ufx.kr/blog/404#footnote_1_404" id="identifier_1_404" class="footnote-link footnote-identifier-link" title="Object, EventDispatcher, DisplayObject, InteractiveObject,  DisplayObjectContainer 의 메서드, 속성만 사용할 수 있고, Sprite 이하의 메서드, 속성은 사용할 수 없습니다.">02</a>]</sup><br />
약간 어려운가요? 논리적 원인을 따지자니 오히려 어려워진 감이 있는데, 어쨌건 ParentClass 의 메서드나 속성을 사용하기 위해서는 ParentClass로 형 변환을 해줘야 한다는 것만 이해하고 있다면 오케이 입니다.</p>
<p>아래는 부모 객체를 만드는데 사용한 클래스 입니다.</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
</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;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ParentClass 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> ParentClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> childInstance<span style="color: #000000; font-weight: bold;">:</span>ChildClass = <span style="color: #0033ff; font-weight: bold;">new</span> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> childInstance <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> doSomething<span style="color: #000000;">&#40;</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> <span style="color: #990000;">&quot;상위 클래스 : do something in ParentClass !!&quot;</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>아래는 자식 객체를 만드는데 사용한 클래스 입니다.</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
</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;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChildClass 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> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// 클릭을 위한 RoundRect</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x336699 <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<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>;
		<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> 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> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 출력 : [object ParentClass]</span>
&nbsp;
			<span style="color: #009900;">// this.parent 는 DisplayObject 구조의 메서드나 속성만을 사용할 수 있음.</span>
			<span style="color: #009900;">// 상위 클래스 형(type)으로 형변환(casting:캐스팅) 하면 public 멤버를 사용할 수 있음</span>
			ParentClass<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span> <span style="color: #000000;">&#41;</span>.doSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// 아래는 컴파일 에러</span>
			<span style="color: #009900;">// this.parent.doSomething();</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>trace( this.parent ) 출력 결과에서 보는것과 같이 parent 는 확실히 부모 객체 자체 까지는 참조 가능하지만 parent 가 가리키고 있는 것은 부모 객체의 DisplayObjectContainer 형태이기 때문에 부모 객체의 doSomething() 을 호출하게 되면 컴파일 에러가 발생합니다.<br />
그래서 위와 같이 ParentClass로 형변환을 해야 부모 객체의 메서드를 사용할 수가 있습니다.</p>
<p>또한 형변환은 아래와 같이 할 수도 있습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> parentInstance<span style="color: #000000; font-weight: bold;">:</span>ParentClass = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span> <span style="color: #0033ff; font-weight: bold;">as</span> ParentClass;
parentInstance.doSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<h3>C. 자식 객체가 부모 객체를 참조 &#8211; 두 번째 방법</h3>
<p>자식 객체에서 부모 객체의 public 메서드를 사용하는 방법은 B 케이스와 같이 형변환 하는 방법 외에도 한가지 더 있습니다. 부모객체가 자식객체에게 부모 객체 자신을 참조할 수 있는 참조(레퍼런스)를 넘겨주는 방법입니다. 아래와 같이 말이죠.</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
</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;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ParentClass 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> ParentClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> childInstance<span style="color: #000000; font-weight: bold;">:</span>ChildClass = <span style="color: #0033ff; font-weight: bold;">new</span> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> childInstance <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// 이렇게 자식 객체에게 this(부모 자신의 객체 레퍼런스)를 넘겨줌</span>
			childInstance.updateParent<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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> doSomething<span style="color: #000000;">&#40;</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> <span style="color: #990000;">&quot;상위 클래스 : do something in ParentClass !!&quot;</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>


<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
</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;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChildClass 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> _parent<span style="color: #000000; font-weight: bold;">:</span>ParentClass;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// 클릭을 위한 RoundRect</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x336699 <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<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>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * 부모 객체의 레퍼런스를 저장함 (부모 객체에서 호출)
		 * @param	부모객체를 인자로 받아 전역변수에 저장
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> updateParent<span style="color: #000000;">&#40;</span> $parent<span style="color: #000000; font-weight: bold;">:</span>ParentClass <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: #0033ff; font-weight: bold;">this</span>._parent = $parent;
		<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> 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: #0033ff; font-weight: bold;">this</span>._parent.doSomething<span style="color: #000000;">&#40;</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>마치&#8230;부모가 통장과 도장과 비밀번호를 자식에게 알려주는 것과 같은 느낌이네요.</p>
<h3>D. 자식 객체가 부모 객체에게 이벤트를 보냄</h3>
<p>위의 B, C 케이스를 이벤트로 통신을 하는 형태로 바꿔보면 어떨까요? 흔히 사용하는 MouseEvent 도 이에 해당하지만, MouseEvent 같이 사용자의 인터랙션에 의한 이벤트는, 이벤트를 발생하는 객체(dispatcher:디스패쳐)가 하는일이 없는것처럼 보이므로 아래와 같이 일부러 디스패쳐를 사용하는 상황을 구성해보았습니다.</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: #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>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ParentClass 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> ParentClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> childInstance<span style="color: #000000; font-weight: bold;">:</span>ChildClass = <span style="color: #0033ff; font-weight: bold;">new</span> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> childInstance <span style="color: #000000;">&#41;</span>;
			childInstance.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, animationComplete <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> animationComplete<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;부모 객체에서 자식 객체에 걸어놓은 Event.COMPLETE 를 캐치함 !!&quot;</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>자식 객체를 만든 클래스 입니다. 클릭을 하게 되면 객체 자신에게 Event.ENTER_FRAME 을 걸게 되고 매 프레임마다 enterFrameHandler() 핸들러를 실행하게 됩니다. 자세한 내용은 주석을 참고하세요.</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;">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.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChildClass 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> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// 클릭을 위한 RoundRect</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x336699 <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<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>;
		<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> 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: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, enterFrameHandler <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> enterFrameHandler<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: #009900;">// x 가 200이 될 때까지 감속운동 (easeOut)</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.1</span>;
&nbsp;
			<span style="color: #009900;">// 200이 되면 엔터프레임을 중단하고 Event.COMPLETE 를 dispatchEvent() 함</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">200</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: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, enterFrameHandler <span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span> <span style="color: #000000;">&#41;</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>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>실제 작업에서는 이런 종류의 코드를 작성해야 하는 경우가 많을 겁니다. 자식 객체에서 발생한 어떤 상황을 모두 실행한 후, 그 결과를 부모에게 알리고 싶은거죠.</p>
<h3>E. 부모 객체가 자식 객체에게 이벤트를 보냄</h3>
<p>부모 객체가 자식 객체에게 이벤트를 보낼 경우도 있겠죠? 부모 객체 입장에서는 자식 객체의 public 멤버들은 모두 참조가 가능하므로 자식 객체의 메서드나 변수를 직접 참조하여 데이터를 전달하거나 메서드를 호출할수도 있습니다. 바로 A케이스와 같은거죠.<br />
그러나 A 케이스의 경우 부모 객체가 자식객체의 public 멤버들이 뭔지 알고 있어야 합니다. 물론 Flash(Flex) Builder 나 FlashDevelop, 또는 FDT, 그리고 Flash CS5 같이 코드 힌트를 지원하는 에디터에서는 자식객체가 가지고 있는 public 멤버들을 볼 수 있지만, 여기서 말하는 것은 그런 기능적인 부분을 이야기 하는 것이 아니라 객체지향 프로그래밍에서의 설계 원칙의 문제입니다. 예를 들어보죠.</p>
<p>만약 부모 객체와 자식 객체가 서로 분리되야할 상황이 생겼습니다. A나 B 케이스는 부모 자식객체간에 서로 직접 참조하고 있으므로, 이 두 객체를 만든 클래스가 분리되는 순간 서로를 참조하고 있던 부분은 모두 컴파일 에러가 발생합니다. 이런 것을 보고 두 객체간 강한 결합을 했다고 표현하죠.<br />
한편, D나 E 케이스는 이벤트를 통해 이 결합을 다소 약하게 만들 수 있습니다. 데이터를 이벤트를 통해 전달하고 이벤트를 보냈다고 어떤 행동을 강제하지 않습니다. 이벤트를 보내는 입장에서는 보내는 걸로 역할을 다 한 것이고 받는 입장에서는 받긴 받았지만 이것을 실행할지 말지는 자신이 결정할 수 있기 때문입니다.</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
</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;">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.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ParentClass 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> _roundRect<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: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _childInstance<span style="color: #000000; font-weight: bold;">:</span>ChildClass = <span style="color: #0033ff; font-weight: bold;">new</span> ChildClass;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ParentClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// 클릭을 위한 RoundRect</span>
			_roundRect.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x996633 <span style="color: #000000;">&#41;</span>;
			_roundRect.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			_roundRect.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			_roundRect.<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>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _roundRect <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _childInstance <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> 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: #009900;">// _roundRect 의 MouseEvent.CLICK 를 캐치하면</span>
			<span style="color: #009900;">// _childInstance 객체에 MouseEvent.ROLL_OUT 이벤트를 보냄</span>
			_childInstance.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">ROLL_OUT</span> <span style="color: #000000;">&#41;</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>부모 객체에서 사용자의 인터랙션에 의해 발생한 MouseEvent.CLICK과는 다른 이벤트를 고르느라 자식 객체에는 MouseEvent.ROLL_OUT 를 보내는 다소 엉뚱한 예제 입니다만, 이벤트는 이렇게도 할 수 있다는것을 보여주는 예시라고 볼 수도 있겠습니다.</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
</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;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChildClass 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> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// ChildClass 객체의 존재를 확인하기 위한 RoundRect</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x336699 <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">120</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">ROLL_OUT</span>, rollOutHandler <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> rollOutHandler<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> <span style="color: #990000;">&quot;부모 객체가 자식 객체에게 dispatchEvent() 한 MouseEvent.ROLL_OUT 을 캐치함 !!&quot;</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>부모 객체(왼쪽에 그려진 RoundRect 그림)를 클릭하면 자식 객체에게 MouseEvent.ROLL_OUT 이벤트를 발생시킬수가 있습니다. 또한 자식객체에는 addEventListener( MouseEvent.ROLL_OUT ) 가 걸려 있으므로 자식 객체를 표시한 오른쪽 RoundRect 그림에 마우스를 올렸다가 밖으로 빼도 동일한 이벤트가 발생하게 됩니다.</p>
<h3>F. 이벤트 발생위치를 바꿈</h3>
<p>E 케이스의 코드는 부모객체가 자식객체에게<sup>[<a href="http://ufx.kr/blog/404#footnote_2_404" id="identifier_2_404" class="footnote-link footnote-identifier-link" title="즉, 자식객체의 입장에서 보면 this.dispatchEvent() 된 것입니다.">03</a>]</sup> dispatchEvent() 메서드를 사용한 것이지만, 반대로 부모객체 내부에서 dispatchEvent() 를 하고 자식객체에서 이것을 addEventListener() 할 수도 있습니다.</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
</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;">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.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ParentClass 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> _roundRect<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: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _childInstance<span style="color: #000000; font-weight: bold;">:</span>ChildClass = <span style="color: #0033ff; font-weight: bold;">new</span> ChildClass;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ParentClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// 클릭을 위한 RoundRect</span>
			_roundRect.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x996633 <span style="color: #000000;">&#41;</span>;
			_roundRect.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			_roundRect.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			_roundRect.<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>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _roundRect <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _childInstance <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> 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: #009900;">// 이번에는 부모 객체인 this 에 dispatchEvent() 를 함</span>
			<span style="color: #009900;">// _childInstance 객체에서 addEventListener() 하고 있건 말건 무조건 이벤트가 발생</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">SELECT</span> <span style="color: #000000;">&#41;</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>아래는 자식 객체를 만드는 클래스 입니다. 자세한 내용은 주석을 참고하세요.</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
</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;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChildClass 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> ChildClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// ChildClass 객체의 존재를 확인하기 위한 RoundRect</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x336699 <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">120</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// 이 객체가 addChild() 되면 핸들러 실행</span>
			<span style="color: #009900;">// 즉, 어떤 객체의 자식 객체가 된 상황</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED</span>, addedHandler <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> addedHandler<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: #009900;">// 어떤 부모 객체인지는 모르겠지만 부모 객체에 addEventListener() 를 걸어놓음</span>
			<span style="color: #009900;">// addEventListener() 는 ParentClass 형(type)이 아니더라도 사용할 수 있으므로 캐스팅이 필요 없음</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">SELECT</span>, selectHandler <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> selectHandler<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;부모 객체의 이벤트를 자식 객체에서 캐치함 !!&quot;</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>또는 반대로, 자식 객체에서 this.addEventListener() 하지 않고, 부모객체에서 이벤트가 일어나도록this.parent.dispatchEvent() 한 후, 부모객체가 this.addEventListener() 까지 하게 할 수도 있습니다. 이벤트가 발생(dispatchEvent)하는 위치만 다를뿐 부모, 자식 객체간은 서로를 자유롭게 참조할 수 있으므로 addEventListener() 메서드는 어디나 걸어 놓을 수 있는 것이죠.</p>
<p>이렇게 부모, 자식 객체간의 여러가지 상황에서 서로를 참조하는 방법에 대해 알아보았습니다. 어떤 방법이 절대적으로 옳지 않으므로 상황에 따라 적절하게 사용하면 됩니다. 또한, 어떤 방법이 절대적으로 옳은건 아니지만, 모든 방법을 제대로 이해하고 있어야 합니다. 그 만큼 객체 지향 프로그래밍에서 가장 기초적이고 중요한 내용입니다.</p>
<p>이해가 잘 안되는 케이스가 있다면, 빈 fla 파일에 ParentClass 를 도큐먼트 클래스로 설정하고 컴파일을 해 보세요.</p>
<p style="text-align: center;">* * *</p>
<p>만약 이벤트와 함께 데이터를 보내야 할 필요가 있을 경우에는 커스텀 이벤트를 사용하면 됩니다. 커스텀 이벤트는 이 포스트의 범위를 벗어나는 내용이므로 &#8220;<a href="http://ufx.kr/blog/228">이벤트에 뭔가 같이 보내보자 &#8211; 커스텀 이벤트 만들고 사용해보기</a>&#8221; 를 읽어보시기 바랍니다.</p>
<p>또한, 객체간 더욱 약한 결합을 구현하기 위한 별도의 방법으로는 옵저버 디자인 패턴을 이용하는 방법이 있습니다. &#8220;<a href="http://ufx.kr/blog/233">Observer Pattern 옵저버 패턴 – 이벤트 디스패처를 이용해 구현</a>&#8221; 을 참고하세요.</p>
<p>이 포스트는 부모, 자식 객체간 참조 방법을 다룬 글이었습니다. 객체가 아닌 상위 클래스와 하위 클래스의 참조 방법에 대한 글 &#8220;<a href="http://ufx.kr/blog/407">AS3.0 클래스의 상속(extends)구조에서 상위, 하위 클래스의 메서드 호출방법</a>&#8221; 도 초보 개발자 여러분들에게 많은 도움이 될 것입니다.</p>
<ol class="footnotes"><li id="footnote_0_404" class="footnote">Object &#8211; EventDispatcher &#8211; DisplayObject &#8211; InteractiveObject &#8211; DisplayObjectContainer &#8211; Sprite</li><li id="footnote_1_404" class="footnote">Object, EventDispatcher, DisplayObject, InteractiveObject,  DisplayObjectContainer 의 메서드, 속성만 사용할 수 있고, Sprite 이하의 메서드, 속성은 사용할 수 없습니다.</li><li id="footnote_2_404" class="footnote">즉, 자식객체의 입장에서 보면 this.dispatchEvent() 된 것입니다.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/404/feed</wfw:commentRss>
		<slash:comments>28</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 394 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>
		<item>
		<title>AS2.0과 AS3.0의 무비클립 부모자식 관계 차이점 이해</title>
		<link>http://ufx.kr/blog/128</link>
		<comments>http://ufx.kr/blog/128#comments</comments>
		<pubDate>Thu, 30 Apr 2009 06:19:14 +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[Flash]]></category>
		<category><![CDATA[getChildByName]]></category>
		<category><![CDATA[MovieClip]]></category>
		<category><![CDATA[무비클립]]></category>
		<category><![CDATA[인스턴스]]></category>
		<category><![CDATA[참조]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=128</guid>
		<description><![CDATA[AS2.0의 무비클립의 포함 관계에서는 대상을 참조하기 위해서 . (dot:점) 을 찍고 계속 참조할 수 있습니다. 실제로 무비클립의 참조에서는 부모 무비클립 안에 자식 무비클립이 들어가 있죠. 그런데 AS3.0의 addChild 는 메소드명이 그래서 그런지 AS2.0처럼 실제 부모안으로 자식을 집어넣는다고 이해하는 경우가 종종 있는것 같습니다. 가령 아래와 같은 코드가 있을때 0 1 2 3 4 //mc는 스테이지에 놓여져 있는 [...]]]></description>
			<content:encoded><![CDATA[<p>AS2.0의 무비클립의 포함 관계에서는 대상을 참조하기 위해서 . (dot:점) 을 찍고 계속 참조할 수 있습니다.<br />
실제로 무비클립의 참조에서는 부모 무비클립 안에 자식 무비클립이 들어가 있죠.</p>
<p>그런데 AS3.0의 addChild 는 메소드명이 그래서 그런지 AS2.0처럼 실제 부모안으로 자식을 집어넣는다고 이해하는 경우가 종종 있는것 같습니다.</p>
<p>가령 아래와 같은 코드가 있을때</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//mc는 스테이지에 놓여져 있는 무비클립</span>
<span style="color: #6699cc; font-weight: bold;">var</span> k<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
mc.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> k <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> mc.k <span style="color: #000000;">&#41;</span> <span style="color: #009900;">// 출력 : undefined</span></pre></td></tr></table></div>

<p>이렇게 참조하려고 할 수 있겠습니다. 그러나 출력 결과에서 보시듯이 undefined 가 나오게 됩니다. 이것은 addChild를 AS2.0의 무비클립의 포함관계, 또는 계층구조로 이해하려고 하는 습관 때문입니다.<br />
아래의 코드를 보시면,</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="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> k<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
k.<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;name_k&quot;</span>; <span style="color: #009900;">// 이렇게 인식하기 좋도록 name 속성을 주고.</span>
mc.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> k <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> mc.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;name_k&quot;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//출력 : [object MovieClip]</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> mc.<span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>.<span style="color: #004993;">name</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 출력 : name_k (mc 안에 도형이 하나만 있어서 도형이 index가 0이고 k의 index가 1이 되므로 k를 가리키게 됨.)</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> k <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 출력 : [object MovieClip]</span></pre></td></tr></table></div>

<p>이 trace 구문들을 보고 뭔가 무릎을 탁 치고 느끼는게 있다면 AS3.0 앞에 놓인 벽 하나를 넘은 셈이 됩니다.</p>
<p>AS2.0에서의 무비클립의 부모자식 관계와 마찬가지로, AS3.0에서도 드로잉 툴에서 무비클립을 만들어 자식을 포함시켜 나가면 AS2.0과 동일하게 점찍고 참조도 가능합니다. AS3.0에서도 당연히 물리적으로(시각적) 무비클립을 포함하는 관계를 만들수도 있죠.</p>
<p>그러나 addChild 로 엮어진 관계는 그런 포함관계가 아니라 수평적으로 엮인 관계 입니다. 실제 인간세상에 비유하자면 양부모와 양자녀 관계라고 할 수 있을것 같습니다.<br />
즉 물리적으로 부모클립에 들어가는 관계가 아니라 <strong><span style="text-decoration: underline;">논리적으로 엮인 관계</span></strong>인 것이죠.<br />
위의 코드에서 보듯이 점찍고 호적문의 ( getChildByName ) 를 해서 참조도 가능하고, 직접 k 를 잡고 핸들링도 가능합니다. AS2.0 에 비해 참조 방법이 늘어나서 좀더 수월하다고 느낄 수도 있을 것입니다.</p>
<p> </p>
<p>이 글은 플생사모의 질문 글에 대한 답변으로 작성되었습니다. ( <a href="http://cafe.naver.com/flashdev/37371">http://cafe.naver.com/flashdev/37371</a> )</p>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/128/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

