6 Replies Latest reply on Jan 29, 2007 1:23 AM by Peter Lorent

    Functions and Events

    svinec
      When I type object_mc.onPress = myFunction(param1, param2); on the 1st frame in the Actions layer and test it, the function is called instantaneously and never assigned to the onPress event.
      How can I assign a function with parameters to an event?
        • 1. Re: Functions and Events
          Peter Lorent Level 2
          Basically, you should use a class that extends the MovieClip class and link it to the mc that contains the button. Then use EventDispatcher and dispatch an event, sending an object as a parameter.
          But, a quick solution is using the Delegate Class. See the attached code.

          • 2. Re: Functions and Events
            Level 7
            > When I type object_mc.onPress = myFunction(param1, param2); on the
            > 1st
            > frame in the Actions layer and test it, the function is called
            > instantaneously
            > and never assigned to the onPress event.

            That is correct.

            Its because you are calling the function by having the ( ) after the
            function name.

            You need

            object_mc.onPress = myFunction;
            --
            Jeckyl



            • 3. Re: Functions and Events
              Level 7
              > Basically, you should use a class that extends the MovieClip class and
              > link it
              > to the mc that contains the button. Then use EventDispatcher and dispatch
              > an
              > event, sending an object as a parameter.
              > But, a quick solution is using the Delegate Class. See the attached code.

              That is overkill for just assigning an onPress
              --
              Jeckyl


              • 4. Re: Functions and Events
                Peter Lorent Level 2
                >>That is overkill for just assigning an onPress
                Sure, for just assigning an onPress it is. But the OP wants to pass parameters to his onPress. So, how are you going to do that then?
                • 5. Re: Functions and Events
                  Level 7
                  >>>That is overkill for just assigning an onPress
                  > Sure, for just assigning an onPress it is. But the OP wants to pass
                  > parameters to his onPress. So, how are you going to do that then?

                  onPress doesn't have parameters .. so I don't think the OP knows WHAT they
                  wants to do :)

                  maybe they want something along the lines of

                  onPress = function() { myOnPress(param1, param2); }
                  --
                  Jeckyl


                  • 6. Re: Functions and Events
                    Peter Lorent Level 2
                    >>onPress doesn't have parameters

                    Sure enough, because onPress is an event. But if you - for whatever dark reason - want to pass data to the callback handler you can use the arguments object as shown using the Delegate Class.
                    But if you want to keep it OO - I doubt that the OP wants that btw - broadcasting a custom event is the way to go.