passing parameters to Events, custom events
AS3 February 29th, 2008In AS 3.0, if you do want to pass params in your Events you can create your own custom events:
update 12.08.08:
I have updated the class to be able to carry any kind of parameters, in an array, making it more flexible.
Actionscript:
-
package com.events {
-
-
/**
-
* ...CustomEvent class
-
* to attach params to dispatch event in arg:Array
-
* @author Sim 15/03/2008 22:56
-
* @usage import com.events.CustomEvent;
-
* var myCustomEvnt = new CustomEvent("EventName",parameterArray);
-
dispatchEvent(myCustomEvnt);
-
// OR simpler:
-
dispatchEvent(new CustomEvent("EventName",parameterArray));
-
-
*/
-
import flash.events.Event;
-
-
public class CustomEvent extends Event {
-
-
public var arg:*;
-
-
public function CustomEvent( type:String, ... a:*) {
-
super( type );
-
arg = a;
-
}
-
override public function clone():Event{
-
return new CustomEvent( type, arg );
-
}
-
}
-
}
-
-
/*_______you then dispatch this like this______*/
-
-
//you would have to import your custom event
-
import com.events.CustomEvent;
-
var myCustomEvnt = new CustomEvent("EventName","1","2","3");
-
dispatchEvent(myCustomEvnt);
-
-
//again you would need to import your custom event
-
import com.events.CustomEvent;
-
hboxvar.addEventListener( "EventName", dosomething );
-
-
private function dosomething( event:CustomEvent ):Void{
-
trace( event.arg) // output: 1,2,3
-
}
another interesting approach can be found here, where flep assigns multiple event types to one custom event, to have more variety in eventHandling.