[AS 3.0] 나만의 이벤트 만들기 – #2 (Event상속)

ActionScript 3.0 — Tags: , — Siaa @ 3:06 pm

이번엔 Event클래스를 상속받아 CustomEventExam 클래스를 만들어서 사용한다.

//CustomEventExam.as
package {

	import flash.events.Event;

	public class CustomEventExam extends Event {

		private var _customValue:Number;

		public function CustomEventExam(type:String, customValue:Number, bubbles:Boolean = false,
cancelable:Boolean = false) {
			super(type, bubbles, cancelable);
			_customValue = customValue;
		}

		public function get customValue():Number {
			return _customValue;
		}
	}
}

CustomEventExam 객체를 dispatchEvent 시킨다.

//EventDispatchExam.as
package {

	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class EventDispatchExam extends EventDispatcher{

		public static const CUSTOM_EVENT:String = "custom_event";

		public function EventDispatchExam() {

		}

		public function callCustomEvent(n:Number):void {
			dispatchEvent(new CustomEventExam(CUSTOM_EVENT, n));
		}
	}
}

EventDispatchExam인스턴스를 생성하고, callCustomEvent()를 호출하면 CUSTOM_EVENT 가 이벤트리스너로 등록된 인스턴스에 이벤트와 인수를 전달, 해당 함수를 실행한다.

//Main.as
package {

	import flash.display.Sprite;
	import flash.events.Event;

	public class Main extends Sprite {

		public function Main():void{

			var myEvent:EventDispatchExam = new EventDispatchExam();

			myEvent.addEventListener(EventDispatchExam.CUSTOM_EVENT, onCustomEventFunc);
			myEvent.callCustomEvent(20);
		}		

		private function onCustomEventFunc(e:CustomEventExam):void {
			trace( "onCustomEventFunc" );
			trace( "e : " + e );
			trace( "e.target : " + e.target );
			trace( "e.customValue : " + e.customValue );
		}
	}
}

결과값

//OutPut
//onCustomEventFunc
//e : [Event type="custom_event" bubbles=false cancelable=false eventPhase=2]
//e.target : [object EventDispatchExam]
//e.customValue : 20
Share

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 blog.flasia.com | powered by WordPress with Barecity