Here is an example of custom event that can pass unlimited parameters using … (rest) parameter:
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:
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.
10 users commented in " AS3 Custom Event for Passing Unlimited Parameters "
Follow-up comment rss or Leave a TrackbackGreat.
This is a super useful class. Thanks for sharing!
Hi,
I am new to flex. Can you provide a working example using AS3 Custom Event for Passing Unlimited Parameters.
thanks! this just saved me a good amount of time.
thanks for the good tip, I changed your script a bit though. Instead of the args array I used an Object because that way I don’t have to remember the numerical order of the parameters
arg[0]=size
arg[1]=type …
but
arg.size=size
arg.type=type
its just nicer to handle that way I think
Feel free to change whatever u want but doesn’t it mean that u will need to define the object variable every time u want a new parameter to pass on since the rest parameter only returns array? and u won’t be able to pass unlimited parameters?
e.g-
public function EventType(type:String, bubbles:Boolean = false, cancelable:Boolean = false, … a:*) {
super(type, bubbles, cancelable);
arg.size = a[0];
arg.type= a[1];
}
Unless u want to instantiate the EventType class first at client, e.g-
var e:EventType = new EventType(”TYPE_NAME”);
e.arg.size = “size”;
e.arg.type = “type”;
dispatchEvent(e);
But it will make the code much longer…
I received that: TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@30ab601 to EventType.
I understand what means, but i don’t understand why? What am i doing wrong?
The function that handle the event will need to be typed to “EventType” or post your code here.
kub.dispatchEvent(new EventType(”mouseDown”, true, true, “slav”));
kub.addEventListener(”mouseDown”, down);
function down(e:EventType){
trace(e.arg);
}
Is possible use that way?
p.s. “kub” is a MC.
You can’t use EventType on MC since they have built-in dispatchEvent… but if the MC is dynamic, you can add variables to MC in run-time, e.g-
kub.data = “Test”
kub.addEventListener(”mouseDown”, down);
function down(e:Event){
trace(e.target.data);
}
Note: MC needs to have customized class that extends MovieClip and has dynamic attribute, you create dynamic classes by using the dynamic attribute when you declare a class, e.g- “dynamic class kub”
Cool, :))))))
Thx a lot, this is very usefully.
Leave A Reply