addEventListener | () | method |
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Parameters
type:String — The type of event. | |
listener:Function — The listener function that processes the event. This function must accept an Event object as its only parameter and must return nothing, as this example shows: function(evt:Event):void The function can have any name. | |
useCapture:Boolean (default = false ) — Determines whether the listener works in the capture phase or the target and bubbling phases. If useCapture is set to true , the listener processes the event only during the capture phase and not in the target or bubbling phase. If useCapture is false , the listener processes the event only during the target or bubbling phase. To listen for the event in all three phases, call addEventListener twice, once with useCapture set to true , then again with useCapture set to false . | |
priority:int (default = 0 ) — The priority level of the event listener. The priority is designated by a signed 32-bit integer. The higher the number, the higher the priority. All listeners with priority n are processed before listeners of priority n-1. If two or more listeners share the same priority, they are processed in the order in which they were added. The default priority is 0. | |
useWeakReference:Boolean (default = false ) — Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not. Class-level member functions are not subject to garbage collection, so you can set |
Throws
ArgumentError — The listener specified is not a function. |
See also
========================================================
There is no way to pass some addtional parameters to an event handling function!
Deva Raj's solution is fine, but without some explaination it is hard to understand.
So the quick and dirty way is to use an inline function:
btn1.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){myEventHandler(e, btn1.id)});
public function myEventHandler(e:MouseEvent, btnId:String) {
Alert.show("Clicked button"+btnId, "Great Title",Alert.OK);
}
Here btn1.id is just an example parameter, you can replace it with what you want.
Another better(!) solution is to create custom Event, which you can find it here:
ActionScript Code:
package com
{
import flash.events.Event;
public class EventWithData extends Event
{
public var data:*;
public function EventWithData(type:String, data:*,
bubbles:Boolean = false, cancelable:Boolean = false)
{
this.data=data;
super(type, bubbles, cancelable);
}
}
}
3 comments:
thanks..
nice explanation..
Awesome!
Thanks!
Nice post
Post a Comment