7 Replies Latest reply on Sep 3, 2010 9:59 AM by kglad

    Pass parameters along with a function that is a parameter to another function.  Is it possible?

    Lazlo Hollyfeld Level 1

      Hi There,

       

      I'm having some trouble passing parameters with a function that is itself being passed as a parameter.  In my application code I'm instancing that class five times:  they are buttons in a menu.  In that class, I've got an onRelease handler that does a number of things when a button is released, one of which is to invoke a function that is defined in the application level of the code.  My problem is that I don't know how to send the function parameters.

       

      In my StandardButton class I have:

       

      class StandardButton extends MovieClip
      {
           
           /* define properties */
           private var selected:Boolean = false;
           public  var btnMenu:MovieClip = null;
           public  var execFunc:Function;
           
           /* define constructor */
           public function StandardButton()
           {
                trace("Standard UI button created: " + this);
                this.onRelease = releaseHandler;
           }
           
           /* define methods */
           public function releaseHandler()
           {
                if (selected) {
                     
                     //this["bg"].gotoAndStop("normal");
                     //selected = false;
                     
                } else {
                     
                     //show the normal state of all other buttons in the menu set
                     for (var i:Number = 0; i < btnMenu.btnList.length; i++)
                     {
                          this._parent[btnMenu.btnList[i]].bg.gotoAndStop("normal");
                          this._parent[btnMenu.btnList[i]].selected = false;
                     }
                     
                     //show the select state of the button
                     this["bg"].gotoAndStop("select");
                     
                     //execute the function attached to this button
                     execFunc();
                     
                     //set the selected boolean for the current button
                     selected = true;
                     
                }
           }
           
           
           /* define accessors */
           public function set execFunction(instructions:Function)
           {
                execFunc = instructions;
           }
           
           public function set buttonMenu(menu:MovieClip)
           {
                btnMenu = menu;
           }
           
      }
      

       

      In my application .fla file I've got this

       

      //This is the function that will be executed when the user presses the button
      function autoModeHandler (param:String) {
           
                trace("You pressed the " + param + " button");
                
                                    
      }
      
      init();
      
      masterContainer.menuContainer.Auto.execFunction = Delegate.create(this, autoModeHandler);
      

       

      The function is successfully being "sent" to the StandardButton class, but without any parameters.  How can I send parameters to the class instance with the way I've got this architected.  Any help is greatly appreciated!

       

      Thanks for your time and help!