8 Replies Latest reply on Jun 13, 2006 4:57 AM by blemmo

    Dynamically generated functions

    pwiop Level 1
      Hi Folks

      I have asked a similar question before but it does not quite answer this problem.

      I wish to dynamically generate functions based on a number generated from xml - in the example below I use var totalCount to represent such a variable.

      var totalCount = 4;
      for (var i = 1; i<totalCount+1; i++) {
      _root["func"+i] = function () {
      trace(i);
      };
      trace(i) // this traces 1,2,3,4
      }

      if I then have a button "b1" with the following code . . .

      b1.onRelease=function(){
      _root.func2()
      }
      . .what I would like to see is '2' traced in the output panel. However, I get '4'
        • 1. Re: Dynamically generated functions
          neil manuell Level 1
          your code is fine, your logic in you tracing is wrong.

          after the loop has ended i will aways be 4
          trace a unique word in each instead
          • 2. Re: Dynamically generated functions
            pwiop Level 1
            Thank you for your response but actually 'no', it does not work. For example make four button instances on the stage name them b1, b2, b3, b4.

            Add the following code to the timeline;

            var totalCount = 4;
            for (var i = 1; i<totalCount+1; i++) {
            _root["func"+i] = function () {
            trace(i-1);
            };
            }
            b1.onRelease = function() {
            _root.func1();
            };
            b2.onRelease = function() {
            _root.func2();
            };
            b3.onRelease = function() {
            _root.func3();
            };
            b4.onRelease = function() {
            _root.func4();
            };

            What I want is for b1 to output "1", b2 to output "2" etc. As it is each button outputs "4"
            • 3. Re: Dynamically generated functions
              neil manuell Level 1
              yes, That is because i does equal four
              because when the user clicks the button, the loop has run 4 times, so its last value is 4.

              so basically you need a way to store the i number in the loop so that it can be accessed by the same function with i in its its name.

              with out thinking about it too much.. why not pass i in the function's arguments:
              var totalCount = 4;
              for (var i = 1; i < totalCount + 1; i ++)
              {
              _root ["func" + i] = function (id:Number)
              {
              trace (id - 1);
              };
              }

              • 4. Re: Dynamically generated functions
                neil manuell Level 1
                no no no that was stupid and wrong

                i'll probably have to think a little bit longer
                • 5. Re: Dynamically generated functions
                  pwiop Level 1
                  Thanks again for your response however, your code failed to do as you suggested. What it did do is return a NaN. HOWEVER, I did find the answer for anyone else that may be intereseted. The full code is as follows;
                  var totalCount = 4;
                  for (i=0; i<totalCount+1; i++) {
                  this["func"+i] = function () {
                  trace(arguments.callee.i);
                  };
                  this["func"+i].i = i;
                  }
                  //buttons
                  b1.onRelease = function() {
                  _root.func1();
                  };
                  b2.onRelease = function() {
                  _root.func2();
                  };
                  b3.onRelease = function() {
                  _root.func3();
                  };
                  b4.onRelease = function() {
                  _root.func4();
                  };
                  I take no credit in sussing it out, I owe the answer to the great god google and a forum user by the name of senocular at http://www.actionscript.org/forums/archive/index.php3/t-30179.html
                  • 6. Re: Dynamically generated functions
                    pwiop Level 1
                    Hey, no problems dude, it was kind of you to take time trying to help - thank you
                    • 7. Re: Dynamically generated functions
                      neil manuell Level 1
                      thought about mentioning function.callee, but since I've never used it successfully thought it better not to put my foot in it.
                      can't beat google though
                      • 8. Re: Dynamically generated functions
                        blemmo Level 1
                        If it's working with arguments.callee that's fine. I didn't know of this method before, just want to mention another way where you don't need to store 'i' as a property of the function: via an extra function that takes 'i' as parameter and gets the actual value by that.

                        function createFunction( id:Number ){
                        _root["func"+id] = function () {
                        trace( id );
                        };
                        }
                        // usage:
                        for (var i = 1; i<totalCount+1; i++) {
                        createFunction( i );
                        };

                        greets,
                        blemmo