Here is an example of custom event that can pass unlimited parameters using … (rest) parameter:
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:
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.
24 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.
hey – thanks for this code. i’m having a problem trying to pass more than one parameter though. works fine with one, which i access with e.arg[0] … but when i add another in the dispatcher:
dispatchEvent(new GalleryEvent(“changingSection”, true, false, sectionID, “cat”));
then try and access e.arg[1] i get ‘undefined’…
what am i doing wrong? really appreciate any help, thanks
emma.
omg how embarrassing, ignore me – just realised what was wrong, nothing to do with the event at all
sorry! and thanks again for the code!
thank you. this code is a life saver.
i am also doing same thing, but no result
here is code
dispatchEvent(new CustomEventClass(CustomEventClass.MY_STRING,false,false,tempStr));
and listener
this.addEventListener(CustomEventClass.MY_STRING,addData);
and class
package assets
{
import flash.events.Event;
public class CustomEventClass extends Event
{
public static const MY_STRING:String = “myString”;
public var myString:String;
public function CustomEventClass(type:String, bubbles:Boolean=false, cancelable:Boolean=false,myString:String=null)
{
super(type, bubbles, cancelable);
this.myString = myString;
}
override public function clone():Event{
return new CustomEventClass(this.type,this.bubbles,this.cancelable,this.myString);
}
}
}
just let me know where i am wrong..
Can someone help, I’m having problems getting this to work with a timer. I want the event to be dispatched after 1 second which is the duration of the timer but the dispatch doesn’t seem to be happening. I have used the same class as above but have also tried changing it to extend a TimerEvent rather than just Event. Here is the function code:
private function setTimer():void {
var timer:Timer = new Timer(1000,1);
timer.dispatchEvent(new EventType(“TimerEvent”,false,false,mainContent.resizeEffect));
timer.addEventListener(“TimerEvent”, resetEffect);
timer.start();
}
private function resetEffect(event:EventType):void { mainContent.welcomePanel.setStyle(“resizeEffect”,event.arg[0]);
}
Any help greatly appreciated.
@Mo
couple of things with your sample
a) first you dispatch an event from timer before you set the listener. I don’t know that the timer actually dispatches this event??!! don’t think it’s allowed
b) the event you’re listening for (“TimerEvent”) isn’t dispatched by timer!
if you want to grab extra data with you’re event, I’d do this..:
private function setTimer():void
{
var timer:Timer = new Timer(1000,1); timer.addEventListener(TimerEvent.TIMER, resetEffect);
timer.start();
function resetEffect(event:*):void
{
addListener(“TimerEvent”,newFunction):
new EventType(“TimerEvent”,false,false,mainContent.resizeEffect));
}
}
private function newFunction(event:TimerEvent):void
{
//do something
}
Hi, could you upload the source .fla and .as files of this example??. It’s just i CAN’T manage this ‘custom event’, i know this tutorial was posted a year ago, but i really need it, and altough i’ve tried to implement it, as i said simply can’t manage it, many tanks.
I am trying to implement it with ‘ENTER_FRAME’ event, as that’s the only event i need, but i’m extremely confused with the dispatch event (and overall it in’st working), i simply don’t understand it.
So, i would be very thankful if you could post a simply but working example implementing the “ENTER_FRAME” event. i need it because what i’m using
stage.addEventListener(“ENTER_FRAME”, _onHandler);
which naturally doesn’t allow me to pass parameters; so i realy need it, thanks again.
You can’t pass parameters using Event.ENTER_FRAME but you can create variables dynamically from movie clips since they are dynamic class, e.g- mc.some_parameter = “Something”. So you can get them in event handler like event.currentTarget.some_parameter…
If you want to pass parameters with an ENTER_FRAME or TIMER event, you could dispatch a new ‘CustomEvent’ event from the ENTER_FRAME or TIMER event handler, which can contain extra parameters. For example:
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(“EVENT_NAME”, customEventHandler);
function enterFrameHandler(e:Event):void{
dispatchEvent( new CustomEvent(“EVENT_NAME”, false, false, this.currentFrame) );
}
function customEventHandler(e:CustomEvent):void {
trace(e.params[0]);
}
You’re basically listening for the ENTER_FRAME event, then dispatching another custom event, and listening again for that. You can use the new event to pass on values from the original ENTER_FRAME event as well as new custom parameters.
VERY useful bit of code! LOVE it!
Thx3000!
This method inspired me to create an alternate method defined here… http://www.actionscript.org/forums/showthread.php3?p=930835&posted=1#post930835
// Import class
import flash.events.Event;
// EventType
public class CustomEvent extends Event
{
// Properties
public var params:Array;
// Constructor
public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, … p:*)
{
super(type, bubbles, cancelable);
params = p;
}
// Override clone
override public function clone():Event
{
return new CustomEvent(type, bubbles, cancelable, params);
}
}
// CustomEvent
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(“EVENT_NAME”, customEventHandler);
function enterFrameHandler(e:Event):void
{
var params:Array = new Array(“hello”, “world”);
dispatchEvent(new CustomEvent(“EVENT_NAME”, false, false, params));
}
function customEventHandler(e:CustomEvent):void
{
trace(e.params[0] + ” ” + e.params[1]);
}
Why is it tracing:
hello,world undefined
insteand of:
hello world
You need to pass the variable one after another like:
dispatchEvent(new CustomEvent(”EVENT_NAME”, false, false, “hello”, “world”));
Leave A Reply