Passing Additional Parameters with Events
What's the best way to pass additional parameters with.. let's say.. a mouseclick?
So, for example... the user clicks on a button and I want to pass the value 10 along with it.. so, whenever the buttonClicked function is called it has that value to use.
I know that you can create custom events by making a subclass of the Event class. And I know that you can dispatch the custom events to where you can access additional parameters by doing something like:
PHP Code:
dispatchEvent(new CustomEvent("action", 10));
CustomEvent's constructor would look something like:
PHP Code:
public function CustomEvent(_type:String, _val:Number);
super(type);
val = _val;
}
And later on you can access the val by doing something like:
PHP Code:
function buttonClicked(evt:CustomEvent) {
trace(evt.val);
}
But in order to do that, you'd have to do something like:
PHP Code:
button.addEventListener("buttonClick", buttonClicked);
dipatchEvent(new CustomEvent("buttonClick", 10));
You have to dispatch the event yourself in order to pass that extra parameter.
So, how could I pass parameters when the user actually does click on the button?
I've thought about just adding a regular MouseEvent.MOUSE_DOWN and dispatching the event from that function... but I lose the values that I need to pass through.
I've also thought about creating a local listener function.. something like
PHP Code:
var val:uint = 10;
button.addEventListener(MouseEvent.MOUSE_DOWN, function buttonClicked(e:Event):void { dispatchEvent("buttonClick", val); });
Would that work, though? I can't test it out right now. Only brainstorming.
Any help is appreciated.