5 Replies Latest reply on Jan 18, 2009 10:01 AM by clbeech

    Dynamic Calls to Functions

      Hi can someone please tell me why this isn't working. im dynamically loading a list of mp3's and when i click on them i want to play the mp3 that i clicked on (this is the thisRow.onRelease function), but it says that the "this.filename" property is undefined, but it is defined when i trace it. all help much appreciated, thanks


        • 1. Re: Dynamic Calls to Functions
          clbeech Level 3
          in your conditionals you may need the ( ) around the condition. however i've also found that i get better resluts if i declare a var and pass that instead of writing the condition within the parameters, as in:

          function loadSong(song:Object):Void {
          var type = (sType == "progressive") ? true : false;
          var state = (sTyle == "progressive") ? "stop" : "start";
          sndTrack.loadSound(song.filename, type);
          setButtonState(state);
          nProgressInterval = setInterval(checkProgress, 100);
          }

          1 person found this helpful
          • 2. Re: Dynamic Calls to Functions
            hey thanks, that is a much better way of writing it, but the song.filename is undefined after i change it. any ideas?
            • 3. Re: Dynamic Calls to Functions
              hey thanks, that is a much better way of writing it, but the song.filename is undefined after i change it. any ideas?
              • 4. Re: Dynamic Calls to Functions
                I have an answer from another forum, thanks for you help.


                quote:

                At the time of the release, this doesn't know what song songArr [5] is because at the time of the release i doesn't have a value.

                Try moving: this.filename = songArr
                [0]; to just below: thisRow.price_txt.text = songArr [5]; - outside of your onRelease function.

                Where are you putting your trace? If you are putting it in the For loop, it makes sense that trace would know what filename is. If you are putting it in the onRelease function, it is very strange that trace knows what filename is but there are a few trace quirks like that.

                • 5. Re: Dynamic Calls to Functions
                  clbeech Level 3
                  yeah - here's the deal. when the loop propagates the buttons, it uses i to grab the data from the array index, however once the loop completes - only the 'final' value is available (if at all) so when in the onRelease i no longer represents the correct index. in order to 'keep' that information for later use you must 'store' the value of i as a property of the instance. so create a new property in the list like:

                  thisRow.id = i;

                  then refer to it in the handler as in:

                  thisRow.onRelease = function() {
                  filename = songArr[this.id][0];
                  loadSong(this);
                  rowActive(this);
                  }

                  you also may want to consider two other things:
                  1) within a handler the keyword 'this' is 'local' to the object so loadSong(this) may not function properly
                  2)you may have better results in keeping your buttons organized and 'separate' if you use a dynamic naming throughout the loop as is:

                  this['thisRow'+i] = ...
                  1 person found this helpful