[AS3] EventDispatcher 간단한 예제

Flash - as2, as3 2010. 2. 3. 15:21
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class Main extends Sprite
	{
		private var dispatcher:CustomDispatcher;
		
		public function Main():void
		{
			var temp:Mov_1 = new Mov_1();
			addChild( temp );
			temp.buttonMode = true;
			temp.addEventListener( MouseEvent.CLICK, clickHandler );
			
			dispatcher = new CustomDispatcher();
			dispatcher.addEventListener( CustomDispatcher.ACTION, actionHandler );		// 디스패치된 이벤트를 감지
			//dispatcher.doAction();		// 이벤트를 디스패치 한다.
		}
		
		// 클릭했을 때 이벤트 디스패치
		private function clickHandler( e:MouseEvent ):void
		{
			dispatcher.doAction();
		}
		
		// 이벤트가 디스패치되면 메서드 실행
		private function actionHandler( e:Event ):void
		{
			txt.appendText( "actionHandler: " + e );
			//trace( "actionHandler: ",  e );
		}
	}
}

// CustomDispatcher.ACTION 이벤트를 전달하는 역할
import flash.events.EventDispatcher;
import flash.events.Event;
class CustomDispatcher extends EventDispatcher 
{
    public static var ACTION:String = "action";

    public function doAction():void 
	{
        dispatchEvent( new Event(CustomDispatcher.ACTION) );
    }
}


: