3 Replies Latest reply on Oct 7, 2009 7:38 AM by Ned Murphy

    parameters for addEventListener




      I am new to actionscript 3 and I have some questions about addEventListener.


      from various books, I know that to add eventlisenter for mouse click to a movieclip:


      movieClipName.addEventListener (MouseEvent.CLICK, clickFunction);


      then I define the function


      function clickFunction(event:MouseEvent)

      { bla bla bla }



      My questions are (as silly as they might sound!),

      a) why do we need a parameter (i.e. event:MouseEvent) for clickFunction??

      b) since we have a parameter for clickFunction, when we feed this function into addEventListener, why didn't we write

          movieClipName.addEventListener (MouseEvent.CLICK, clickFunction() );



      Thanks in advance for your help!


        • 1. Re: parameters for addEventListener
          Ned Murphy Adobe Community Professional & MVP

          a. The event throws an argument to the function, the argument carries information about the event.  This information can be very useful at times.  One common use is that the same function can be used for several items, and you cab use the event.currentTarget to isolate which object was involved.


          b.  I do not have the definitive answer, but it probably has to do with the event listener method using the function name in its processing, packing in the argument data before it commands the function to execute.  Using the function with the parentheses might actually call the function, and it isn't ready to go anywhere without a fully defined argument added in.

          1 person found this helpful
          • 2. Re: parameters for addEventListener
            jimmybbq Level 1

            a) yea it can be useful. but without this parameter, the addEventListener function wont even work!

                but that's fine, I guess I just have to do what has to be done.

            • 3. Re: parameters for addEventListener
              Ned Murphy Adobe Community Professional & MVP

              That's correct--if you don't define the function properly it will not work... if an argument is going to be sent to a function and the function is not set up to receive it, the compiler will throw an error.