<?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; addEventListener</title>
	<atom:link href="http://ufx.kr/blog/tag/addeventlistener/feed" rel="self" type="application/rss+xml" />
	<link>http://ufx.kr/blog</link>
	<description>Flash + ActionScript &#38; Design</description>
	<lastBuildDate>Fri, 03 Sep 2010 05:15:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>8</slash:comments>
		</item>
		<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]]></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 에 도큐먼트 클래스를 연결해서 swf 를 [...]]]></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>5</slash:comments>
		</item>
		<item>
		<title>AS3.0 로더(Loader)로 불러온 다른 swf 의 클래스 사용하기</title>
		<link>http://ufx.kr/blog/281</link>
		<comments>http://ufx.kr/blog/281#comments</comments>
		<pubDate>Sun, 05 Jul 2009 09:43:19 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[AS3.0]]></category>
		<category><![CDATA[addChild]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[ApplicationDomain]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[contentLoaderInfo]]></category>
		<category><![CDATA[DisplayObject]]></category>
		<category><![CDATA[getDefinition]]></category>
		<category><![CDATA[Linkage]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[라이브러리]]></category>
		<category><![CDATA[캐스팅]]></category>
		<category><![CDATA[클래스]]></category>
		<category><![CDATA[형변환]]></category>

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

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

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

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

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

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//Main.as 의 내용.</span>
<span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">DisplayObject</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Loader</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #004993;">URLRequest</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Loader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Loader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> request<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SubSystem.swf&quot;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//Loader는 직접 addEventListener 하지 못하고 contentLoaderInfo 를 통하여야 함</span>
			<span style="color: #004993;">loader</span>.<span style="color: #004993;">contentLoaderInfo</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, completeHandler <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">loader</span>.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span> request <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> completeHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> runtimeClassRef<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Class</span> = <span style="color: #004993;">loader</span>.<span style="color: #004993;">contentLoaderInfo</span>.<span style="color: #004993;">applicationDomain</span>.<span style="color: #004993;">getDefinition</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SubSystem&quot;</span> <span style="color: #000000;">&#41;</span>  <span style="color: #0033ff; font-weight: bold;">as</span>  <span style="color: #004993;">Class</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> displayObj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span> = <span style="color: #0033ff; font-weight: bold;">new</span> runtimeClassRef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//addChild 하려면 DisplayObject 로 캐스팅</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> displayObj <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> subSystemObj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = displayObj <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Object</span>; <span style="color: #009900;">//메서드를 호출하려면 다시 Object 로 캐스팅</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> item1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = subSystemObj.createItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;item1&quot;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> item2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = subSystemObj.createItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;item2&quot;</span> <span style="color: #000000;">&#41;</span>;
			item2.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span>;
&nbsp;
			<span style="color: #009900;">//로드한 SubSystem.swf 의 자원은 이제 subSystemObj 로 참조하면 됩니다.</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;subSystemObj.numChildren : &quot;</span>, subSystemObj.<span style="color: #004993;">numChildren</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

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

		<guid isPermaLink="false">http://ufx.kr/blog/?p=258</guid>
		<description><![CDATA[브라우저의 전체화면으로 플래시 웹 사이트를 만들고자 하는 디자이너들의 열망은, 아마도 Flash 3.0, 그러니까 지난 세기 말이 될 텐데요, 당시 유고 나카무라의 웹 사이트를 보고 눈이 휘둥그레 졌던 시절부터 쭉 이어졌던거 같습니다.
작년에 AS2.0 에서 onResize 이벤트 리스너를 이용한 풀플래시 무비클립 제어에 대한 포스트를 작성하였습니다. 원래 그 포스트는 플생사모에 올라오는 매번 똑같은 질문에 대한 답변으로 시작되었습니다만, 현재는 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ufx.kr/blog/wp-content/uploads/2009/05/fl_icon.png"><img class="alignleft size-full wp-image-133" title="fl_icon" src="http://ufx.kr/blog/wp-content/uploads/2009/05/fl_icon.png" alt="fl_icon" width="175" height="175" /></a>브라우저의 전체화면으로 플래시 웹 사이트를 만들고자 하는 디자이너들의 열망은, 아마도 Flash 3.0, 그러니까 지난 세기 말이 될 텐데요, 당시 유고 나카무라의 웹 사이트를 보고 눈이 휘둥그레 졌던 시절부터 쭉 이어졌던거 같습니다.</p>
<p>작년에 AS2.0 에서 <a href="http://ufx.kr/blog/62">onResize 이벤트 리스너를 이용한 풀플래시 무비클립 제어</a>에 대한 포스트를 작성하였습니다. 원래 그 포스트는 <a href="http://cafe.naver.com/flashdev" target="_blank">플생사모</a>에 올라오는 매번 똑같은 질문에 대한 답변으로 시작되었습니다만, 현재는 이 블로그에서 가장 조회수가 높은 포스트가 되어있죠.</p>
<p>일전에 AS3.0 버전으로도 만들어 달라는 요청을 어떤 분에게 받은 바도 있고, 역시 이런 기능을 원하는 분들은 역시 AS3.0 에 익숙하지 않은 디자이너일 가능성이 높기 때문에, 그런 분들에게 약간이나마 도움이 되고자 이 포스트를 작성하게 되었습니다.</p>
<p><span id="more-258"></span>먼저 <a href="http://ufx.kr/blog/wp-content/uploads/2009/06/stage_resize_example.html" target="_blank">어떤 모양새인지 살펴보시기 바랍니다.</a> 본문안에 배치하면 브라우져 크기에 따라 swf 의 크기가 달라지지 않으므로 새 창을 띄웁니다. 전체 소스코드는 이 포스트의 가장 하단에 있습니다.</p>
<p>아래 코드는 도큐먼트 클래스 입니다. 다운로드 파일에 fla 파일도 포함되어 있긴 합니다만, 아래의 도큐먼트 클래스를 연결하는 내용 외에는 아무 내용도 없는 텅빈 내용입니다. 스테이지에 올라오는 내용이 간단한 내용이라 모든 그림 그리기는 클래스에서 처리하게 만들었습니다.</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
37
38
39
</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: #000000; font-weight: bold;">*</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> 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;">rect</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Rect<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> circle1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<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> circle2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<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> circle3<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<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> circle4<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<span style="color: #000000;">&#40;</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; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">scaleMode</span> = <span style="color: #004993;">StageScaleMode</span>.<span style="color: #004993;">NO_SCALE</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">align</span> = <span style="color: #004993;">StageAlign</span>.<span style="color: #004993;">TOP_LEFT</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">rect</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle1 <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle2 <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle3 <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle4 <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">RESIZE</span>, stageResizeHandler <span style="color: #000000;">&#41;</span>;
&nbsp;
			stageResizeHandler<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//로드시에 초기화 한번</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> stageResizeHandler<span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span> = <span style="color: #0033ff; font-weight: bold;">null</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;stageWidth :&quot;</span>, <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span>, <span style="color: #990000;">&quot;/ stageHeight :&quot;</span>, <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">rect</span>.<span style="color: #004993;">x</span> = <span style="color: #004993;">rect</span>.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">50</span>
			<span style="color: #004993;">rect</span>.<span style="color: #004993;">width</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">100</span>;
			<span style="color: #004993;">rect</span>.<span style="color: #004993;">height</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">100</span>;
			circle2.<span style="color: #004993;">x</span> = circle4.<span style="color: #004993;">x</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span>;
			circle3.<span style="color: #004993;">y</span> = circle4.<span style="color: #004993;">y</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</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>StageScaleMode.NO_SCALE 부분은 AS2.0 때처럼 문자열로 &#8220;noScale&#8221; 써도 되지만, 오타에 대한 보장은 해주지 않죠.<sup>[<a href="http://ufx.kr/blog/258#footnote_0_258" id="identifier_0_258" class="footnote-link footnote-identifier-link" title="오타가 나더라도 폭발이 일어나거나 하진 않지만, 원하는 화면이 나오지 않는데다가 그 원인이 오타로 인한 것임을 깨닫게 되기까지 한참 걸릴 가능성이 높습니다.">01</a>]</sup> StageScaleMode.NO_SCALE 과 같이 상수 형태로 사용하면 오타가 있다고 하더라도 컴파일 타임에 오타가 체크 되는 장점이 있습니다.<br />
StageAlign.TOP_LEFT 도 마찬가지로 &#8220;LT&#8221; 문자열 대신 사용하였습니다.</p>
<p>중앙의 사각형은 Rect 클래스가 그리고, 네 귀퉁이의 원은 Circle 클래스가 그려줍니다.(다운로드한 as 파일 참조) 도큐먼트 클래스에서는 stage 에 올라간 5개의 요소를 addChild 해 주고 stage에 Event.RESIZE 를 걸어 swf 에 크기 변화가 일어날때마다 stageResizeHandler() 메서드를 호출하고 스테이지의 크기 변화에 따라 각 요소들의 위치와 크기를 변경해 줍니다.</p>
<p>위의 도큐먼트 클래스는 그래픽 자원들을 모두 코드에서 생성하였습니다만, 스테이지에 만들어 놓은 무비클립을 제어하는 경우라고 하더라도 stageResizeHandler() 메서드 안에서 무비클립의 인스턴스 네임에 x, y, width, height 등을 조절하면 됩니다.<br />
풀플래시의 화면 제어는 stage.scaleMode = StageScaleMode.NO_SCALE 과,<br />
stage.align = StageAlign.TOP_LEFT 두가지를 설정하고 이해하였다면 그것으로 끝입니다.</p>
<p>AS2.0 에서는 중앙의 사각형의 중심점을 정 중앙에 놓았는데 이번 AS3.0코드에서는 왼쪽 위에 중심점을 놓은 점을 제외하면 AS2.0 의 onResize 와 모두 동일합니다.</p>
<p><span style="color: #008080;"><strong>html 에서의 주의점.</strong></span></p>
<p>플래시가 앉혀지는 html 의 object와 embed 태그의 width 와 height 를 모두 100% 로 해줘야 합니다. 즉, 플래시의 크기 자체는 html 상의 상위 요소의 width 와 height 에 따라 꽉 채워지도록 100%로 설정하고, 나머지는 브라우져 크기 변화에 맏기는 겁니다.<br />
한가지 주의할 점은, html 상단에 정의하는 DOCTYPE 이 XHTML 1.0 인 경우에는 스타일 시트의 html과 body 요소까지 height 속성을 100% 으로 설정해야 swf 가 의도된대로 표현됩니다. (DOCTYPE 이 HTML 4.0인 경우에는 div 까지만 height=”100%” 설정해도 동작합니다.)</p>
<blockquote><a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=12" title=" downloaded 749 times" >stage RESIZE 전체 소스코드와 결과 다운로드 (749)</a>
<ol>
<li> stage_resize_example.fla</li>
<li> stage_resize_example.swf</li>
<li> Main.as</li>
<li> Circle.as</li>
<li> Rect.as</li>
<li> stage_resize_example.html</li>
</ol>
</blockquote>
<p>AS2.0 버전의 풀플래시 화면 제어가 필요한 분은 <a href="http://ufx.kr/blog/62">여기를 클릭</a>하여 이동하세요.</p>
<ol class="footnotes"><li id="footnote_0_258" class="footnote">오타가 나더라도 폭발이 일어나거나 하진 않지만, 원하는 화면이 나오지 않는데다가 그 원인이 오타로 인한 것임을 깨닫게 되기까지 한참 걸릴 가능성이 높습니다.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/258/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Observer Pattern 옵저버 패턴 &#8211; 이벤트 디스패처를 이용해 구현</title>
		<link>http://ufx.kr/blog/233</link>
		<comments>http://ufx.kr/blog/233#comments</comments>
		<pubDate>Sun, 07 Jun 2009 19:59:17 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[CustomEvent]]></category>
		<category><![CDATA[dispatcher]]></category>
		<category><![CDATA[EventDispatcher]]></category>
		<category><![CDATA[Observer]]></category>
		<category><![CDATA[subject]]></category>
		<category><![CDATA[디스패처]]></category>
		<category><![CDATA[디자인패턴]]></category>
		<category><![CDATA[옵저버]]></category>
		<category><![CDATA[옵저버패턴]]></category>
		<category><![CDATA[이벤트 디스패처]]></category>
		<category><![CDATA[주제]]></category>
		<category><![CDATA[주체]]></category>
		<category><![CDATA[커스텀 이벤트]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=233</guid>
		<description><![CDATA[지난 옵저버 패턴과 커스텀 이벤트에 대한 포스트에서 AS3.0 의 이벤트 시스템이 옵저버 패턴과 밀접한 관련이 있다고 하였는데요, 이번에는 trace 만 찍지 않고, 실제 액션스크립트로 비주얼 하게 구현을 해 보도록 하겠습니다.
요건은 다음과 같습니다.

AS3.0 의 이벤트 디스패처를 이용할 것.
화면에 그래픽 객체가 각자 움직이다가 마우스 클릭을 하면 한 지점으로 모인다.
다시 한번 클릭하면 그래픽 객체들이 다시 각자 움직인다.


옵저버 패턴이므로 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-235" title="bubble" src="http://ufx.kr/blog/wp-content/uploads/2009/06/bubble.jpg" alt="bubble" width="240" height="160" />지난 <a href="http://ufx.kr/blog/170">옵저버 패턴</a>과 <a href="http://ufx.kr/blog/228">커스텀 이벤트에 대한 포스트</a>에서 AS3.0 의 이벤트 시스템이 옵저버 패턴과 밀접한 관련이 있다고 하였는데요, 이번에는 trace 만 찍지 않고, 실제 액션스크립트로 비주얼 하게 구현을 해 보도록 하겠습니다.</p>
<p><span style="color: #008080;">요건은 다음과 같습니다.</span></p>
<ul>
<li>AS3.0 의 이벤트 디스패처를 이용할 것.</li>
<li>화면에 그래픽 객체가 각자 움직이다가 마우스 클릭을 하면 한 지점으로 모인다.</li>
<li>다시 한번 클릭하면 그래픽 객체들이 다시 각자 움직인다.</li>
</ul>
<p><span id="more-233"></span></p>
<p><span style="color: #008080;">옵저버 패턴이므로 등장인물 3인이 필요하겠습니다.</span></p>
<ol>
<li><strong>주제 객체</strong><br />
이벤트 디스패처 (EventDispatcher) 가 주제 역할을 하게 됩니다. 이벤트 시스템의 중심에서 이벤트를 전파하는 역할을 하고 있죠.</li>
<li><strong>옵저버 객체</strong><br />
여기서는 화면에서 움직이는 그래픽 객체가 옵저버 입니다. 옵저버들은 평소에는 다른 기능을 하다가<sup>[<a href="http://ufx.kr/blog/233#footnote_0_233" id="identifier_0_233" class="footnote-link footnote-identifier-link" title="화면에서 각자 움직이다가">01</a>]</sup> 이벤트가 발생하면 지정된 곳으로 모이게 됩니다.</li>
<li><strong>인포 객체</strong><br />
이벤트 디스패처가 옵저버 들에게 전달하는 Event 내부에 같이 실어 보내는 데이터가 인포 객체 입니다.</li>
</ol>
<p><span style="color: #008080;">화면과 기능을 어떻게 구현할 것인가 좀더 구체화 시킬 필요가 있을 것 같습니다.</span></p>
<ul>
<li>옵저버 객체들이 움직이는 방식으로는 Timer 를 이용해 2초 주기의 TweenMax 의 베지어 트윈을 이용해 랜덤한 위치로 결정.</li>
<li>주제가 옵저버에게 전달하는 정보로는 MouseEvent.MOUSE_DOWN 이 일어난 mouseX 와 mouseY 좌표값으로하여 그 정보에 따라 옵저버 객체들이 모이도록 한다.</li>
<li>옵저버 객체들이 주제에 등록되고 등록해제 되는 것을 옵저버 객체에 대한 MouseEvent.MOUSE_DOWN 으로 구현한다.</li>
<li>최초 도큐먼트 클래스에서 모두 옵저버 등록을 하고, 옵저버 객체에 MouseEvent.MOUSE_DOWN 이 한번 일어나면 옵저버 리스트에서 빠지고 (동시에 시각적으로 차이를 내기 위해 alpha = 0.5 해줌) 다시한번 MouseEvent.MOUSE_DOWN 이 일어나면 다시 옵저버 리스트로 들어가도록 한다.</li>
</ul>
<p>그 외에 옵저버와 직접적인 기능과는 관계 없는 내용으로, MouseEvent.ROLL_OVER 가 일어난 객체를 가장 상위로 올리도록 한 후 상하 관계를 쉽게 알 수 있도록 옵저버 객체 중앙에 십자 표시를 하였고, 주제 객체가 정보를 새로 생성했는지 아닌지 시각적으로 구분하기 위해 정보가 생성되면<sup>[<a href="http://ufx.kr/blog/233#footnote_1_233" id="identifier_1_233" class="footnote-link footnote-identifier-link" title="즉, 배경에 MouseEvent.MOUSE_DOWN 이 일어나면">02</a>]</sup> 배경 이미지의 색상을 바꾸는 내용이 추가로 들어갔습니다.</p>
<p>그럼 이제 주제 인터페이스를 보도록 하겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
</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;">public</span> interface ISubject
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> addObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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: #339966; font-weight: bold;">function</span> removeObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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: #339966; font-weight: bold;">function</span> notifyObserver<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>지난번 옵저버 패턴과 완전히 동일합니다.</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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</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.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;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Subject implements ISubject 
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> dispatcher<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">EventDispatcher</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> _arrObserver<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</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> _infoObject<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Subject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//ISubject 를 구현 - 옵저버 등록</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			dispatcher.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> ObserverEvent.NOTIFY, $o.update <span style="color: #000000;">&#41;</span>;
			addObject<span style="color: #000000;">&#40;</span> $o <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//옵저버에 등록된 대상 객체를 배열에 저장</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> addObject<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			arrObserver.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> $o <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//ISubject 를 구현 - 옵저버 제거</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> removeObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			dispatcher.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> ObserverEvent.NOTIFY, $o.update <span style="color: #000000;">&#41;</span>;
			removeObject<span style="color: #000000;">&#40;</span> $o <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//옵저버를 제거하면 배열에서 제거</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> removeObject<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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> <span style="color: #004993;">index</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = arrObserver.<span style="color: #004993;">indexOf</span><span style="color: #000000;">&#40;</span> $o, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
			arrObserver.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">index</span>, <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//ISubject 를 구현 - 이벤트 발생 --&gt; 정보 전달</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> notifyObserver<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>
			dispatcher.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> ObserverEvent<span style="color: #000000;">&#40;</span> ObserverEvent.NOTIFY, <span style="color: #0033ff; font-weight: bold;">this</span>.infoObject <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//도큐먼트 클래스에서 호출할 메서드</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setClick<span style="color: #000000;">&#40;</span> $x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> <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>
			makeInfoObject<span style="color: #000000;">&#40;</span> $x, $y <span style="color: #000000;">&#41;</span>
			notifyObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//정보를 Object 로 만들어 변수에 저장</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> makeInfoObject<span style="color: #000000;">&#40;</span> $x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> _infoObject = <span style="color: #000000;">&#123;</span> infoX <span style="color: #000000; font-weight: bold;">:</span> $x, infoY <span style="color: #000000; font-weight: bold;">:</span> $y <span style="color: #000000;">&#125;</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> <span style="color: #0033ff; font-weight: bold;">get</span> infoObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _infoObject; <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> <span style="color: #0033ff; font-weight: bold;">get</span> arrObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _arrObserver; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>EventDispatcher 객체를 생성했고 구상 객체 목록을 관리할 수 있도록 배열을 하나 만들었습니다.<br />
이벤트를 통해 전달할 정보를 저장할 Object 객체도 하나 만들었네요.<br />
ObserverEvent.NOTIFY 가 발생하면 dispatcher가 각각의 옵저버들이 걸어놓은 $o.update 메서드를 실행 하는 것입니다.</p>
<p>디스패처용 커스텀 이벤트 입니다. <a href="http://ufx.kr/blog/228">커스텀 이벤트에 대한 포스트</a>에서도 있듯이 커스텀 이벤트의 형태는 변수명이나 상수명을 제외하고는 다른 커스텀 이벤트와 동일합니다.</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
</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.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> ObserverEvent extends <span style="color: #004993;">Event</span>
	<span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> const NOTIFY<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;notify&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _info<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ObserverEvent<span style="color: #000000;">&#40;</span> $type<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $info<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, $bubble<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span>, $cancelable<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span> $type, $bubble, $cancelable <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>._info = $info;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> CustomEvent<span style="color: #000000;">&#40;</span> <span style="color: #004993;">type</span>, _info, <span style="color: #004993;">bubbles</span>, <span style="color: #004993;">cancelable</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> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">info</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> _info;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>옵저버 쪽으로 넘어가 보겠습니다. 인터페이스에는 dispatcher가 호출할 update() 메서드 이외엔 없군요.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
</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.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> interface IObserver
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> update<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;">&#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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
</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;">Shape</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;">TimerEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextField</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextFormat</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">Timer</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> gs.TweenMax
	<span style="color: #0033ff; font-weight: bold;">import</span> gs.easing.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Item extends <span style="color: #004993;">Sprite</span> implements IObserver
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> txtId<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextField</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _id<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">stageWidth</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">stageHeight</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">timer</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Timer</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Timer</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2000</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> isInfo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Item <span style="color: #000000;">&#40;</span> $size<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> circle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x000000, <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, $size <span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle <span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> hLine<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			hLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, 0xcc3300 <span style="color: #000000;">&#41;</span>;
			hLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">60</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
			hLine.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> hLine <span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> vLine<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			vLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, 0xcc3300 <span style="color: #000000;">&#41;</span>;
			vLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
			vLine.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> vLine <span style="color: #000000;">&#41;</span>
&nbsp;
			txtId = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextField</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> format<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextFormat</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextFormat</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			format.<span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Verdana&quot;</span>;
			format.<span style="color: #004993;">color</span> = 0xFFFFFF;
			format.<span style="color: #004993;">size</span> = <span style="color: #000000; font-weight:bold;">20</span>;
			txtId.<span style="color: #004993;">defaultTextFormat</span> = format
			txtId.<span style="color: #004993;">selectable</span> = <span style="color: #0033ff; font-weight: bold;">false</span>;
			txtId.<span style="color: #004993;">autoSize</span> = <span style="color: #990000;">&quot;left&quot;</span>;
			txtId.<span style="color: #004993;">backgroundColor</span> = 0x000000;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>txtId<span style="color: #000000;">&#41;</span>
&nbsp;
			<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_TO_STAGE</span>, initStage <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">timer</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">TimerEvent</span>.<span style="color: #004993;">TIMER</span>, onTimeHandler <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">timer</span>.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//옵저버가 되기위한 구현</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<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> event<span style="color: #000000; font-weight: bold;">:</span>ObserverEvent = $e <span style="color: #0033ff; font-weight: bold;">as</span> ObserverEvent;
			<span style="color: #0033ff; font-weight: bold;">this</span>._x = event.<span style="color: #004993;">info</span>.infoX;
			<span style="color: #0033ff; font-weight: bold;">this</span>._y = event.<span style="color: #004993;">info</span>.infoY;
			<span style="color: #009900;">// 정보로 들어온 값이 undefined 인지 아닌지 확인하여 isInfo에 Boolean 으로 저장</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.isInfo = <span style="color: #000000;">&#40;</span> _x <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> _y <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?</span> <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">false</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> onTimeHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TimerEvent</span> = <span style="color: #0033ff; font-weight: bold;">null</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;">if</span> <span style="color: #000000;">&#40;</span> isInfo <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				concentration<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span>
			<span style="color: #000000;">&#123;</span>
				deconcentration<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</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> initStage<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;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED_TO_STAGE</span>, initStage <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stageWidth</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span>;
			<span style="color: #004993;">stageHeight</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</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> <span style="color: #0033ff; font-weight: bold;">set</span> id <span style="color: #000000;">&#40;</span> $value<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</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>
			_id = $value;
			txtId.<span style="color: #004993;">text</span> = _id.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</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> <span style="color: #0033ff; font-weight: bold;">get</span> id<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _id; <span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//Event 의 정보 값으로 모이기</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> concentration<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>
			TweenMax.to<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000;">&#123;</span> <span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span>_x, <span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span>_y , ease<span style="color: #000000; font-weight: bold;">:</span>Quart.easeInOut <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//Event 정보 값이 undefined 일 경우 랜덤으로 움직이기</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deconcentration<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>
			TweenMax.to<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000;">&#123;</span> <span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageWidth</span><span style="color: #000000;">&#41;</span>, <span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageHeight</span><span style="color: #000000;">&#41;</span>, 
					bezier<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#91;</span> <span style="color: #000000;">&#123;</span> <span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageWidth</span><span style="color: #000000;">&#41;</span>, <span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageHeight</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#93;</span>, 
					ease<span style="color: #000000; font-weight: bold;">:</span>Quart.easeInOut <span style="color: #000000;">&#125;</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>구상 클래스의 길이가 약간 길지만 대부분 생성자에서 Shape로 그림을 그리는 부분과, 객체 본연의 할일을 부분<sup>[<a href="http://ufx.kr/blog/233#footnote_2_233" id="identifier_2_233" class="footnote-link footnote-identifier-link" title="랜덤으로 2초마다 움직이게 하는 일">03</a>]</sup> 들이고, 옵저버로서의 내용은 인터페이스 메서드인 update() 메서드를 구상하는 내용 뿐 입니다.</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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</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>;
	<span style="color: #0033ff; font-weight: bold;">import</span> gs.TweenLite;
&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> subject<span style="color: #000000; font-weight: bold;">:</span>Subject;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> isInfo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> clickedItem<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</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> bg<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</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; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			subject = <span style="color: #0033ff; font-weight: bold;">new</span> Subject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			createBG<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			createItem<span style="color: #000000;">&#40;</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> createBG<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>
			bg = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x555555, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>
			bg.<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;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span>, <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</span>, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> bg <span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, bgClickHandler<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> createItem<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: #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;">10</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> item<span style="color: #000000; font-weight: bold;">:</span>Item = <span style="color: #0033ff; font-weight: bold;">new</span> Item<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
				item.id = i;
				item.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
				item.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, itemClickHandler <span style="color: #000000;">&#41;</span>;
				item.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_OVER</span>, itemRollOverHandler <span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> item <span style="color: #000000;">&#41;</span>;
&nbsp;
				subject.addObserver<span style="color: #000000;">&#40;</span> item <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//옵저버 객체에 마우스 이벤트가 일어나면 handleItem() 으로 객체를 보냄</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> itemClickHandler<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: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">target</span><span style="color: #000000; font-weight: bold;">:</span>Item = e.<span style="color: #004993;">currentTarget</span> <span style="color: #0033ff; font-weight: bold;">as</span> Item
			handleItem<span style="color: #000000;">&#40;</span> <span style="color: #004993;">target</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//주제에 있는 옵저버 리스트(배열)을 조회하여 리스트에 있으면 옵저버 등록을 해지 (없으면 옵저버 등록)</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleItem<span style="color: #000000;">&#40;</span> $item<span style="color: #000000; font-weight: bold;">:</span>Item <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> isFind<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #0033ff; font-weight: bold;">in</span> subject.arrObserver <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> item === $item <span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					subject.removeObserver<span style="color: #000000;">&#40;</span> $item <span style="color: #000000;">&#41;</span>;
					$item.<span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">0.5</span>;
					isFind = <span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">!</span>isFind <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				subject.addObserver<span style="color: #000000;">&#40;</span> $item <span style="color: #000000;">&#41;</span>
				$item.<span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">1</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//롤오버된 객체는 가장 위로 올림</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> itemRollOverHandler<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;">setChildIndex</span><span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">currentTarget</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Sprite</span>, <span style="color: #004993;">numChildren</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//배경을 클릭한 지점을 주제 메서드로 보냄</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> bgClickHandler<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>
			isInfo = <span style="color: #000000; font-weight: bold;">!</span>isInfo;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> isInfo <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				subject.setClick<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">undefined</span>, <span style="color: #0033ff; font-weight: bold;">undefined</span> <span style="color: #000000;">&#41;</span>;
				TweenLite.to<span style="color: #000000;">&#40;</span> bg, <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000;">&#123;</span> tint<span style="color: #000000; font-weight: bold;">:</span>0x555555 <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span>
			<span style="color: #000000;">&#123;</span>
				subject.setClick<span style="color: #000000;">&#40;</span> <span style="color: #004993;">mouseX</span>, <span style="color: #004993;">mouseY</span> <span style="color: #000000;">&#41;</span>;
				TweenLite.to<span style="color: #000000;">&#40;</span> bg, <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000;">&#123;</span> tint<span style="color: #000000; font-weight: bold;">:</span>0x8D2545 <span style="color: #000000;">&#125;</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>각 메서드에 주석을 달아 놓았으니 참고하시면 되겠습니다.<br />
위의 도큐먼트 클래스를 컴파일한 결과입니다.<br />
<object width="600" height="400" data="http://ufx.kr/blog/wp-content/uploads/2009/06/observerdispatchereventexample.swf" type="application/x-shockwave-flash"><param name="wmode" value="transparent" /><param name="src" value="http://ufx.kr/blog/wp-content/uploads/2009/06/observerdispatchereventexample.swf" /></object></p>
<p>배경을 한번 클릭하면 이벤트가 발생하고, 다시한번 클릭하면 객체들은 다시 원래의 할일을 합니다.<br />
객체를 클릭하면 옵저버 리스트에서 빠지고, 다시한번 클릭하면 옵저버에 등록됩니다.</p>
<p>이벤트 모델을 이용한 옵저버 패턴은, 옵저버 객체를 임의로 빼거나 추가해도 디스패처로가 다른 옵저버들에게 전달하는 이벤트에는 아무런 문제가 발생하지 않게되죠.<sup>[<a href="http://ufx.kr/blog/233#footnote_3_233" id="identifier_3_233" class="footnote-link footnote-identifier-link" title="객체간 약한 결합 : Loose Coupling">04</a>]</sup> </p>
<p>이렇게 디스패처에 의해 발생된 이벤트에 정보를 실어 옵저버 객체에 전달하고 이벤트 리스너에 등록된 함수를 이용해 객체 내부를 업데이트 하여 정보를 사용하는 방법까지 표현하였습니다.</p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=9" title=" downloaded 91 times" >Observer Pattern Dispatcher Event Example 액션스크립트 코드 다운로드 (91)</a>
<ol class="footnotes"><li id="footnote_0_233" class="footnote">화면에서 각자 움직이다가</li><li id="footnote_1_233" class="footnote">즉, 배경에 MouseEvent.MOUSE_DOWN 이 일어나면</li><li id="footnote_2_233" class="footnote">랜덤으로 2초마다 움직이게 하는 일</li><li id="footnote_3_233" class="footnote">객체간 약한 결합 : Loose Coupling</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/233/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>이벤트에 뭔가 같이 보내보자 &#8211; 커스텀 이벤트 만들고 사용해보기</title>
		<link>http://ufx.kr/blog/228</link>
		<comments>http://ufx.kr/blog/228#comments</comments>
		<pubDate>Sun, 07 Jun 2009 04:14:31 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[AS3.0]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[CustomEvent]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[EventDispatcher]]></category>
		<category><![CDATA[Observer]]></category>
		<category><![CDATA[Observer Pattern]]></category>
		<category><![CDATA[onRelease]]></category>
		<category><![CDATA[removeEventListener]]></category>
		<category><![CDATA[디스패처]]></category>
		<category><![CDATA[리스너]]></category>
		<category><![CDATA[이벤트]]></category>
		<category><![CDATA[이벤트 디스패처]]></category>
		<category><![CDATA[이벤트 리스너]]></category>
		<category><![CDATA[커스텀 이벤트]]></category>
		<category><![CDATA[콜백]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=228</guid>
		<description><![CDATA[AS2.0 에서 AS3.0 으로 넘어와 보면 가장 처음 느끼는 것이 &#8220;뭐이리 복잡하게 해야 되는 거냐?&#8221; 하는 점이겠죠. 버튼에 액션을 넣어 뭔가를 하고 싶은 경우라면 button.onRelease = function() { //하고싶은일 } 이나 심지어 무비클립에 on(release) { //하고싶은일 } 같이 간단한 코드만으로 하고싶은 일을 하던것에 비해서 AS3.0 의 이벤트 리스너 방식은 귀찮기만 해 보입니다.
이 포스트에서는 이벤트 시스템이 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://ufx.kr/blog/wp-content/uploads/2009/06/sale_img.jpg" alt="sale_img" title="sale_img" width="300" height="225" class="alignleft size-full wp-image-229" />AS2.0 에서 AS3.0 으로 넘어와 보면 가장 처음 느끼는 것이 &#8220;뭐이리 복잡하게 해야 되는 거냐?&#8221; 하는 점이겠죠. 버튼에 액션을 넣어 뭔가를 하고 싶은 경우라면 button.onRelease = function() { //하고싶은일 } 이나 심지어 무비클립에 on(release) { //하고싶은일 } 같이 간단한 코드만으로 하고싶은 일을 하던것에 비해서 AS3.0 의 이벤트 리스너 방식은 귀찮기만 해 보입니다.</p>
<p>이 포스트에서는 이벤트 시스템이 2.0 이전의 콜백 방식에 비해 가지는 유리한 점 중의 하나를 살펴보겠습니다. 커스텀 이벤트 클래스를 이용하여 이벤트를 보내면서 특정한 정보를 보낼 수 있는 방법입니다. 콜백 방식으로는 부릴 수 없는 재주죠.</p>
<p><span id="more-228"></span>우선 커스텀 이벤트 클래스를 하나 만들어 봅니다.</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
</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.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> CustomEvent extends <span style="color: #004993;">Event</span>
	<span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> const CALL<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;call&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _data<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomEvent<span style="color: #000000;">&#40;</span> <span style="color: #004993;">type</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, <span style="color: #004993;">data</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, bubble<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #004993;">cancelable</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">type</span>, bubble, <span style="color: #004993;">cancelable</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>._data = <span style="color: #004993;">data</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> CustomEvent<span style="color: #000000;">&#40;</span> <span style="color: #004993;">type</span>, _data, <span style="color: #004993;">bubbles</span>, <span style="color: #004993;">cancelable</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> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">data</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> _data;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>커스텀 클래스의 모양새는 거의 이런 식입니다. bubble, cancelable 등 몇가지 파라미터들이 있습니다만, 이 포스트에서는 커스텀 클래스를 이용한 데이터 전송만을 다룰 것이므로, 생성자에서 object 를 인자로 받아 변수로 저장하고 있다는 것만 기억하면 되겠습니다.</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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</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: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> CustomEvent;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomEventTest 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> broadcast<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">EventDispatcher</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> <span style="color: #004993;">info</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #000000;">&#123;</span> info1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;의류관 세일중. &quot;</span>, info2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;무료시식코너 운영중. &quot;</span>, info3<span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;제휴카드 무이자 할부중. &quot;</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> CustomEventTest <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">stage</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>;
			broadcast.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> CustomEvent.CALL, broadcastListen <span style="color: #000000;">&#41;</span>;
			broadcast.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> CustomEvent.CALL, customer1Action <span style="color: #000000;">&#41;</span>;
			broadcast.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> CustomEvent.CALL, customer2Action <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>
			broadcast.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> CustomEvent<span style="color: #000000;">&#40;</span> CustomEvent.CALL, <span style="color: #004993;">info</span> <span style="color: #000000;">&#41;</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> broadcastListen<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span>CustomEvent <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>, e.<span style="color: #004993;">data</span>.info1, e.<span style="color: #004993;">data</span>.info2, e.<span style="color: #004993;">data</span>.info3 <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> customer1Action<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span>CustomEvent<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;">data</span>.info2, <span style="color: #990000;">&quot;: 먹으러 가볼까나&quot;</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> customer2Action<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span>CustomEvent<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;">data</span>.info1, <span style="color: #990000;">&quot;: 찜해놨던 옷을 사러가자&quot;</span> <span style="color: #000000;">&#41;</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>
			broadcast.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> CustomEvent.CALL, customer2Action <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>위의 내용은 백화점에서 세일 정보를 방송 ( broadcast ) 하고 있고, 그 정보에 반응하는 백화점 고객이 있다는 설정 입니다. </p>
<p><a href="http://ufx.kr/blog/170">옵저버 디자인 패턴의 등장인물 소개</a>에서 주제를 여러가지 단어로 설명을 했었는데요,<sup>[<a href="http://ufx.kr/blog/228#footnote_0_228" id="identifier_0_228" class="footnote-link footnote-identifier-link" title="dispatcher, supplier, notifier, publisher, server, service, provider">01</a>]</sup> broadcaster 도 주제를 표현하는 단어로 볼 수 있습니다. (그래서 이런 비유를 선택했습니다.) </p>
<p>실제로 이벤트 시스템은 옵저버 디자인 패턴에서 주제객체가 update 메서드를 이용해 옵저버들에게 정보를 전달하는 방식과 동일합니다. 디스패처는 이벤트 리스너 목록에 있는 리스너들에게 정보를 뿌려주죠. </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: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> broadcast<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">EventDispatcher</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> <span style="color: #004993;">info</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #000000;">&#123;</span> info1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;의류관 세일중. &quot;</span>, info2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;무료시식코너 운영중. &quot;</span>, info3<span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;제휴카드 무이자 할부중. &quot;</span> <span style="color: #000000;">&#125;</span>;</pre></td></tr></table></div>

<p>위와 같이 EventDispatcher 객체를 생성하면서 broadcast 라고 이름 지었고, 이벤트를 통해 전달할 정보인 info 를 Object 객체로 만들었습니다.</p>
<p>아래는 커스텀 이벤트를 발생시키는 디스패치 이벤트 입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;">broadcast.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> CustomEvent<span style="color: #000000;">&#40;</span> CustomEvent.CALL, <span style="color: #004993;">info</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>이렇게 이벤트를 디스패치 하게 되면 broadcast 객체에 addEventListener 시켜 CustomEvent.CALL 이벤트가 발생하는지 귀 기울이고 있던 리스너에 info 가 전달 됩니다.</p>
<p>이벤트 모델은 리스너들이 정보를 받아보는지 아닌지에 대한 것은 디스패처가 관심 없습니다. 다만 정보를 이벤트를 통해 보낼 뿐이죠. 이러한 구조는 객체지향 프로그래밍이 지향하는 약한 결합 (Loose Coupling)을 표현합니다.</p>
<ol class="footnotes"><li id="footnote_0_228" class="footnote">dispatcher, supplier, notifier, publisher, server, service, provider</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/228/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Timer Class를 이용한 원하는 시간에 원하는 일 하기 F1 대로 구현</title>
		<link>http://ufx.kr/blog/61</link>
		<comments>http://ufx.kr/blog/61#comments</comments>
		<pubDate>Fri, 12 Dec 2008 00:41:42 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[AS3.0]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[F1]]></category>
		<category><![CDATA[Sprite]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[TimerEvent]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=61</guid>
		<description><![CDATA[플생사모에 올라온 글에 대한 답변입니다.
http://cafe.naver.com/flashdev/34255
Timer Class 의 F1에 있는 예제 그대로 사용하였습니다.
http://livedocs.adobe.com/flash/9.0_kr/main/00000074.html

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
37
package &#123;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
&#160;
	public class ShortTimer extends Sprite &#123;
		public function ShortTimer&#40;&#41; &#123;
			// creates a new five-second Timer
			var minuteTimer:Timer = new Timer&#40;1000,5&#41;;
&#160;
			// designates listeners for the interval and completion events
			minuteTimer.addEventListener&#40;TimerEvent.TIMER, onTick&#41;;
			minuteTimer.addEventListener&#40;TimerEvent.TIMER_COMPLETE, onTimerComplete&#41;;
&#160;
			// starts the timer ticking
			minuteTimer.start&#40;&#41;;
		&#125;
&#160;
		public function onTick&#40;event:TimerEvent&#41;:void &#123;
			// displays the tick count so [...]]]></description>
			<content:encoded><![CDATA[<p>플생사모에 올라온 글에 대한 답변입니다.</p>
<p>http://cafe.naver.com/flashdev/34255</p>
<p>Timer Class 의 F1에 있는 예제 그대로 사용하였습니다.</p>
<p>http://livedocs.adobe.com/flash/9.0_kr/main/00000074.html</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
37
</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;">TimerEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">Timer</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ShortTimer 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> ShortTimer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// creates a new five-second Timer</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> minuteTimer<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Timer</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Timer</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1000</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// designates listeners for the interval and completion events</span>
			minuteTimer.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">TimerEvent</span>.<span style="color: #004993;">TIMER</span>, onTick<span style="color: #000000;">&#41;</span>;
			minuteTimer.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">TimerEvent</span>.<span style="color: #004993;">TIMER_COMPLETE</span>, onTimerComplete<span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// starts the timer ticking</span>
			minuteTimer.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</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> onTick<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TimerEvent</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;">// displays the tick count so far</span>
			<span style="color: #009900;">// The target of this event is the Timer instance itself.</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;tick &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> event.<span style="color: #004993;">target</span>.<span style="color: #004993;">currentCount</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">//원하는 시간에 원하는 일 간단하게 if 문으로 실행</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">target</span>.<span style="color: #004993;">currentCount</span> == <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;3초에 할일&quot;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #009900;">//A1.addChild (B);</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">target</span>.<span style="color: #004993;">currentCount</span> == <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;4초에 할일&quot;</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #009900;">//A1.addChild (C);</span>
			<span style="color: #000000;">&#125;</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> onTimerComplete<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TimerEvent</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;Time's Up!&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>네이버 카페, 트랙백좀 지원했으면&#8230;.</p>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/61/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
