ActionScript 3 在AS3中使用自定义事件传递数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 在AS3中使用自定义事件传递数据相关的知识,希望对你有一定的参考价值。

package utils  {
	import flash.events.Event;
	
	public class CustomEvent extends Event 
	{
		/*	Place as many events as you want to include in this class  */
		public static const EXAMPLE_EVENT:String = "exampleEvent";
		public static const ANOTHER_EVENT:String = "anotherEvent";
			
		/* 	Create properties to hold data that you want to pass with your event  
			Include as many as you like.  */
		public var dataObj:Object;
		
		public function CustomEvent(type:String, dataObj:Object = null) 
		{
			this.dataObj = dataObj;
			/*	Call the super function which fires off the event 
			set the event type, bubbling and cancelable properties  */
			super(type, false, false);
		}
		
		/*	Duplicates the instance of the event */
		override public function clone():Event
		{
			/*	This is the event that will be received by your handler function	*/
			return new CustomEvent(type, dataObj);
		}
	}
}

/********** To use this class would look something like this **************/

/*	Dispatch event with data to be passed
dataObj.name = "test data";
dispatchEvent(new CustomEvent(CustomEvent.EXAMPLE_EVENT, dataObj));

/*	 Listen for your custom event
addEventListener(CustomEvent.EXAMPLE_EVENT, handleExample);

/*	 Handle your custom event with custom data
function handleExample(event:CustomEvent):void {
var mydata:Object = event.dataObj;
}

/*************************************************************************/

以上是关于ActionScript 3 在AS3中使用自定义事件传递数据的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3中图像URL的自定义光标

语法错误 - rightparen - ActionScript 3 - 使用数组

ActionScript 3 AS3:在ActionScript中使用E4X生成动态XML

ActionScript 3 在AS3中使用麦克风

ActionScript 3 在AS3中使用RegExp去除空白

ActionScript 3 AS3在阵列中?