관리 메뉴

nkdk의 세상

AS3.0의 EventDispatcher 본문

My Programing/ActionScript

AS3.0의 EventDispatcher

nkdk 2008. 3. 9. 22:26
이벤트는 자바의 이벤트랑 동일하지만 쓰는 법을 아셔야 겠죠? 그런데 갑자기 팍 어려워 지네요.

CS3에서 3개의 인자로 혁신적으로 이벤트모델을 변경하였습니다.

- 이벤트 대상 객체
- EventDispatcher 객체
- Event 객체

EventDispatcher 클래스에 대해서

DisplayObject 를 포함한 많은 클래스가 이 클래스를 상속 받아 구현한다.

- component관련 EventDispathcer: RadioButtonGroup, DataProvider
- 비디오 관련 EventDispatcher: Camera, Microphone
- 통신 관련
FileReference, FileReferenceList, LocalConnection, NetConnection,NetStream, URLLoader, URLStream, XMLSocket, Socket, LoaderInfo
- 그 외 관련
Animator, ContextMenu, ContextMenuItem, DisplayObject, IME, Locale, PrintJob, SharedObject, Sound, SoundChannel, StyleSheet, Timer, Transition, TransitionManager, Tween

굉장히 많은 클래스가 상속 받고 있다.

자 예를 들어 보죠

/*
// 예제1)
import flash.events.MouseEvent;

stage.addEventListener(MouseEvent.CLICK,fnMouseClick);
trace(MouseEvent.CLICK);//click

function fnMouseClick(e:MouseEvent):void
{
trace(e);
}

/*
[MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=437 localY=173 stageX=437 stageY=173 relatedObject=null ctrlKey=false altKey=false shiftKey=false delta=0]
*/

// 예제2)
import flash.events.MouseEvent;

stage.addEventListener(MouseEvent.CLICK,fnMouseClick1);
stage.addEventListener(MouseEvent.MOUSE_WHEEL,fnMouseClick2);

function fnMouseClick1(e:MouseEvent):void
{
//trace( e.formatToString( "type", "bubbles", "cancelable", "eventPhase"));

trace("fnMouseClick1"+e);

e.stopImmediatePropagation();
fnMouseClick2(e);
}

function fnMouseClick2(e:MouseEvent):void
{
trace("fnMouseClick2"+e);
}

다음과 같이 만들었습니다. 아마 자바를 하셨더라면
이해가 잘 되시리라 생각 되네요.

이벤트에서 나타낼 수 있는게 많으니까 출력된 걸 보시면 아마 아실 것입니다.
e.typename 이런식으로 하면 값을 얻을 수가 있습니다. 그럼 다음회에서 ^^