1 Reply Latest reply on Oct 22, 2007 4:41 AM by Greg Dove

    passing a param to a function

    fourinchesofego
      Okay... So I am having another problem.

      I have created an array and have no problem passing it to a function as a param via set interval, as follows:

      // Create a text field and import text from outside txt file
      this.createTextField("aText", 1, 0, 0, 1, 1);
      var a_lv:LoadVars = new LoadVars();
      a_lv.onData = function(src:String):Void {
      if (src != undefined) {
      aText.text = src;
      makeA();
      } else {
      aText.text = "Unable to load external file.";
      }
      };
      a_lv.load("a.txt");
      var obNum = 0;
      function makeText(whichArray) {
      trace(whichArray);
      obNum++;
      var newObject = "myOb"+obNum;
      var newclip = createEmptyMovieClip(newObject, obNum);
      newclip.createTextField("newText", 0, 0, 0, 100, 100).text = whichArray[Math.round(Math.random()*whichArray.length-1)];
      trace(newclip.newText.text);
      newclip._x = Math.ceil(Math.random()*300);
      newclip._y = Math.ceil(Math.random()*300);
      }
      function makeA() {
      var a_str:String = aText.text;
      var a_array:Array = a_str.split(" ");
      trace(a_array);
      myInterval = setInterval(_root, "makeText", 1000, a_array);
      }

      The problem is when I try to pass the param via a button:

      on (keyPress "a") {
      makeText(a_array);
      }
      it comes out as undefined.

      What is wrong with my param pass???

      I know this must be very simple, but I seem rather addle-brained today...
        • 1. Re: passing a param to a function
          Greg Dove Level 4
          a_array is a temporary variable that exists ony inside makeA while its executing.

          try declaring it outside makeA

          var a_array:Array=[];

          and inside makeA just use
          a_array= a_str.split(" ");

          perhaps there's other things that need attention, but that was the first thing that jumped out at me. Also can I suggest you use the attach Code button to post your code. It makes it easier to read if its longer than a few lines and avoids the possibility of errors being introduced because of formatting codes used in the forum posts.