5

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
  • Sorry for the vague comment, but I once read that there was a bug with this. Can anybody verify that? Commented Jun 23, 2010 at 5:49
  • 2
    Depending on the specific case, you have to make sure your dynamic function doesn't get garbage collected before it's invoked. Usually, you're better off registering the function somehwere. Commented Jun 23, 2010 at 5:52
  • my probelm is i need to pass a parameter to the event handler while declaring it in action listner.If i am able to do that, i can define the handler somewhere else also. Commented Jun 23, 2010 at 5:55

1 Answer 1

4

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);
    }

}
Sign up to request clarification or add additional context in comments.

7 Comments

can i pass one extra parameter to the function while declaring in action listener
You'll need to do something like a Delegate class: actionscript.org/resources/articles/205/1/The-Delegate-Class/….
That's a great reference. thanks buddy. Also the code worked inline as well. Now i have two good options. Cool
Actually, that example is old and not exactly what I wanted.
I wonder if there is a way exist like "addEventlistener(handlerName,{parameters},..). If that is possible, thats the best.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.