flash里自定义事件

 
/* 
* 文件名:  自定义事件
* 创建人:   陈泽丹/Clark
* 创建时间: 20120220
* 文件描述: 网上查资料时,发现不少讨论对这个小问题解决得垃圾信息一堆,故写个简单版的。
*/


package 
{
	import flash.display.Sprite;
	import flash.events.EventDispatcher;

	public class Test extends Sprite
	{
		public function Test()
		{
			setEvent2();
		}
		public function setEvent1()
		{
			var pDispatcher:EventDispatcher = new EventDispatcher();
			pDispatcher.addEventListener(UserEvent.EVENT_SIGN1, this.onEvent1);
			pDispatcher.dispatchEvent(new UserEvent(UserEvent.EVENT_SIGN1, 1));
			pDispatcher.removeEventListener(UserEvent.EVENT_SIGN1, this.onEvent1);
			pDispatcher = null;
		}
		public function setEvent2()
		{
			var pDispatcher:EventDispatcher = new EventDispatcher();
			pDispatcher.addEventListener(UserEvent.EVENT_SIGN2, this.onEvent2);
			pDispatcher.dispatchEvent(new UserEvent(UserEvent.EVENT_SIGN2, 2));
			pDispatcher.removeEventListener(UserEvent.EVENT_SIGN2, this.onEvent2);   
			pDispatcher = null;
		}
		public function onEvent1(_evt:UserEvent)
		{
			trace("onEvent1 " + _evt.m_iEventData);
		}
		public function onEvent2(_evt:UserEvent)
		{
			trace("onEvent2 "+ _evt.m_iEventData);
		}
	}
}

import flash.events.Event;
class UserEvent extends Event
{
	public static const EVENT_SIGN1:String = "EVENT_SIGN1";
	public static const EVENT_SIGN2:String = "EVENT_SIGN2";
	public var m_iEventData:int = 0;
	public function UserEvent(_szEventSign:String, _iEventData:int):void
	{
		super(_szEventSign);
		m_iEventData = _iEventData;
	}
}

你可能感兴趣的:(function,String,Flash,null,Class,import)