View Full Version : Passing multiple arguments to a custom function from within an Event Handler Method

Mr Dude
12-10-2008, 07:38 AM
Hi Everyone,

I am presently trying to pass a string value into a custom function, that is itself called from within an event method.

At the moment I have attached an event to a symbol instance (called product1) with the following code:

product1.addEventListener(MouseEvent.MOUSE_DOWN, onClick);

The above code succesfully calls the following custom function:

function onClick(evt:MouseEvent):void{
navigateToURL(requestURL, "_self");

This is only useful for calling 1, distinct, URL and I want to be able to pass a String value into the function onClick() which can represent one of several URLs depending upon which one of several symbols has been clicked.

As a result I will be attaching event listeners to several symbols instances, e.g.:

product1.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
product2.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
product3.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
product4.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
product5.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
product6.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
product7.addEventListener(MouseEvent.MOUSE_DOWN, onClick);

And I want my custom function to navigate to a URL based upon which symbol instance has been clicked.

I have tried the following:

product1.addEventListener(MouseEvent.MOUSE_DOWN, onClick(product1URL));

function onClick(requestURL:String):void{
navigateToURL(requestURL, "_self");

Can somebody please help me with passing both the mouse event, and a String value to my custom function? I have been unable to find examples of what I am attempting to do on the internet.

Any help will be greatly appreciated.

Kind Regards,


12-10-2008, 10:49 AM
FYI: This is AS3 which is why there are fewer resources out there at the moment. It is slowly being embraced, but many of us 'AS2' fans are still not wanting to pick it up since in some ways it makes simple things more difficult.

I looked at the documentation, and the function has no ability to pass arguments to the function. This is sad, I agree, but I have an idea. I don't know how to do it though, but perhaps you do?

So can you grab the instance name of the clicked button somehow? Then you can have an array with the 'instance name => link' inside of it to index the proper link for your function. Its an idea...

Mr Dude
12-11-2008, 12:40 AM
Hi jeremywilken,

It is hard to believe that AS3 does not have the ability to pass arguments to event methods. My greatest programming experience is with Java, and I could see many similarities between AS3 and Java / OOP languages. The idea that Adobe have left this out is just insanity.

I had dreamt up a work around similar to your suggestion on my way home in the car last night, but I kept thinking "surely AS3 has this ability".

I found information on the internet, stating exactly as you have informed me, however there was the suggestion that a custom class could be made from the particular event class, as a workaround.

One of the questions about ActionScript 3 that I am most often asked is how to send arguments to a listener function along with an event. Recently, a reader named Jim asked just such a question.

To provide some background for this question, you could send argument data to a standard function like this?

function showMsg(msg:String):void {

However, in an event listener, only one argument is allowed in the listener function: the argument responsible for receiving the event data. Using a standard mouse click listener as an example…

stage.addEventListener(MouseEvent.CLICK, showMsg, false, 0, true);
function showMsg(evt:MouseEvent):void {

…the kind of question asked is, can you do something like this:

stage.addEventListener(MouseEvent.CLICK, showMsg, "hello",
false, 0, true);
function showMsg(evt:MouseEvent, msg:String):void {

The answer is, not out of the box. The existing AS3 events do not provide for this capability. However, to answer Jim’s question, the best way to pass arguments with an event is to create your own event class by extending AS3’s Event class.

I found the above quote at this website: http://www.learningactionscript3.com/

Many thanks for your input, you have saved me some serious hair loss! :)



Mr Dude
12-11-2008, 12:43 AM
Further to the above post, the following link explains how to make a custom event class, so as to pass arguments into a function called from an event...