Bookmark and Share

Here is an example of custom event that can pass unlimited parameters using … (rest) parameter:

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package {
	// Import class
	import flash.events.Event;
	// EventType
	public class EventType extends Event {
		// Properties
  		public var arg:*;
  		// Constructor
  		public function EventType(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ... a:*) {
   			super(type, bubbles, cancelable);
   			arg = a;
   		}
		// Override clone
		override public function clone():Event{
			return new EventType(type, bubbles, cancelable, arg);
		};
	}
}

Usage:

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
dispatchEvent(new EventType("TYPE_NAME",false,false,"arg1","arg2"));
.
.
.
addEventListener("TYPE_NAME", onHandler);
function onHandler(e:EventType) {
	trace(e.arg[0]);
}

This is very useful when you just want to pass some variables via events.

Edit: You can define “bubbles” and “cancelable” parameters in super() so you don’t have to pass it via constructor, e.g- “super(type, false, false)” so you can call “dispatchEvent(new EventType(“TYPE_NAME”,”arg1″,”arg2″))” instead.