views:

22

answers:

1

I have an action listener :

myText.addEventListener(TextEvent.LINK,linkClickHandler);

As according to this, i need to write an event handler function seperate to handle the text link event.

Now my requirement is i need to write that function in the same line isntead of giving its name.ie, something like

mytext.addEventListenet(TextEvent.LINK, 
                 function(event:TextEvent) {....code comes here....});

can i do like this in AS3.What will the syntax for this if possible.

Also i want to know, wether i can pass one more extra param to the event handler other than event which is the default parameter.

+3  A: 

Your code will work. Just need a return type on the function.

EDIT: A simple delegate class:

public class Delegate
{

    public var cb:Function;
    public var args:Array;

    public function Delegate(cb:Function, ...args)
    {
        this.cb = cb;
        this.args = args;
    }

    public static function create(cb:Function, ...args):Function {
        var functionProxy:Delegate = new Delegate(cb, args);
        return functionProxy._create;
    }

    public function _create(e:Object=null):void {
        var params:Array = new Array();
        if(e) {
            params.push(e);
        }

        for each (var o:Object in args[0]) {
            params.push(o);
        }
        cb.apply(null, params);
    }

}
Glenn
can i pass one extra parameter to the function while declaring in action listener
Wind Chimez
You'll need to do something like a Delegate class: http://www.actionscript.org/resources/articles/205/1/The-Delegate-Class/Page1.html.
Glenn
That's a great reference. thanks buddy. Also the code worked inline as well. Now i have two good options. Cool
Wind Chimez
Actually, that example is old and not exactly what I wanted.
Glenn
I wonder if there is a way exist like "addEventlistener(handlerName,{parameters},..). If that is possible, thats the best.
Wind Chimez
Maybe if you created a custom event class that allows for extra parameters, like the one here: http://evolve.reintroducing.com/2007/10/23/as3/as3-custom-events/
letseatfood
@letseatfood: that's slightly different. That's attaching variables to the event itself, but you can't control most of the events (e.g., MouseEvent) so one way is to bind additional arguments onto the function.
Glenn