Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Passing multiple arguments to a custom function from within an Event Handler Method

    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:
    Code:
    product1.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
    The above code succesfully calls the following custom function:
    Code:
    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.:
    Code:
    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:
    Code:
    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,

    David

  • #2
    Senior Coder gnomeontherun's Avatar
    Join Date
    Sep 2007
    Location
    Houston
    Posts
    2,846
    Thanks
    10
    Thanked 238 Times in 229 Posts
    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...
    jeremy - gnomeontherun
    Educated questions often get educated answers, and simple questions often get simple answers.

  • #3
    New Coder
    Join Date
    Sep 2007
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool Thanks for your response jeremywilken

    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 {
    trace(msg);
    }
    showMsg("Claire");

    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 {
    trace("hello");
    }

    …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 {
    trace(msg);
    }

    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!

    Cheers

    David

  • #4
    New Coder
    Join Date
    Sep 2007
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    The answer, in terms of a Custom Event Class

    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...

    http://www.learningactionscript3.com...vents/#more-56


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •