Home » ActionScript 3.0

Passing Arguments to Event Handler AS3:

25 November 2008 4 Comments

In AS2 we use Delegate class to pass arguments to event handlers and scoping event handlers. But there is no Delegate class in AS3.

Still sending extra argument to event handlers is not a big deal.

We are going to use one way of sending extra parameters to event handlers in actionscript 3.0,

code:

button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){handleClickEvent(e,"Home")});
function handleClickEvent(e:MouseEvent,str:String) {
	trace("Argument :"+str,"- Event target :"+e.target.name);
}

output:
Argument :Home – Event target :button

Here note,

We are adding event handler for button symbol. On click we call an inline function where inturn we are passing the MouseEvent and a parameter.

Simple is’nt it.

Cheers :)

VN:F [1.7.7_1013]
Rating: 10.0/10 (4 votes cast)
VN:F [1.7.7_1013]
Rating: +1 (from 3 votes)
Passing Arguments to Event Handler AS3:10.0104
Translate this post
        
        
        
        
  

4 Comments »

  • Palash said:

    Hi
    This really helped me.I can pass arguments on panel onclick in flex 3 actionscript .
    Thanks
    Palash

    UN:F [1.7.7_1013]
    Rating: 5.0/5 (1 vote cast)
    UN:F [1.7.7_1013]
    Rating: 0 (from 0 votes)
  • David said:

    Hmm. but how do you remove the eventListener again?

    UN:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.7.7_1013]
    Rating: 0 (from 0 votes)
  • Ricardo said:

    Now, this is genius!!

    UN:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.7.7_1013]
    Rating: +1 (from 1 vote)
  • DreadSquirrel said:

    Thanks mate! I actually knew that… i just forgot how to make inline functions in AS3 (and forgot they were called “inline”)… LOL!

    Passing arguments to event handlers this way is so much better for small apps, than making a custom Event class or dynamic class or any other similar overkill (which googling shows first as a way of dealing with the problem, sadly).

    UN:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.7.7_1013]
    Rating: 0 (from 0 votes)

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.