5 Replies Latest reply on Oct 17, 2007 3:59 PM by kglad

    Array in call back function

    YippySkippy-xx
      I define an array at the top of the file in FLASH
      var questions:Array = new Array();
      I read in a list of short XML files and push them onto the array in the COMPLETE callback function of the loader event listener using inside a for each loop
      questions.push( new XML(evt.target.data))
      Inside the callback function I can trace the contents of the array.
      trace (questions[length-1]);
      Outside the callback function the array elements are undefined.

      What is wrong with this appraoch to adding small XML files to each array element?
        • 1. Re: Array in call back function
          kglad Adobe Community Professional & MVP
          type your array outside your function body on the timeline. that way your array will be local to the timeline and accessible anywhere in your swf via timeline referencing, if necessary.

          when you type a variable (like your array questions) inside the function body, it's local that function and not accessible outside your function.
          • 2. Re: Array in call back function
            YippySkippy-xx Level 1
            The code is below.

            The output is
            undefined
            undefined
            1
            2

            I have been working on this code for a week.


            var listHands:XMLList;
            var loader:URLLoader;
            var questionFile:URLRequest = new URLRequest("questions.xml");
            var questionList:XML;
            var questions:Array = new Array();

            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE,accessXMLData);
            loader.load(questionFile);
            loadEachQuestion();
            trace (questions[0]);

            function loadEachQuestion(): void {
            for each (var file:String in listHands) {
            var questionLoader:URLLoader;
            questionLoader = new URLLoader();
            questionLoader.addEventListener(Event.COMPLETE,accessQuestion);
            var newQuestionFile:URLRequest = new URLRequest(file);
            questionLoader.load(newQuestionFile);
            }
            }

            function accessQuestion(evt:Event):void {
            // trace (evt.target.data);
            trace(questions.push( new XML(evt.target.data)));
            // trace (questions[length-1]);
            }

            function accessXMLData(evt:Event):void {
            questionList = new XML(loader.data);
            // trace (questionList.toXMLString());
            listHands = questionList.children();
            // trace (listHands.toXMLString());
            }
            • 3. Re: Array in call back function
              kglad Adobe Community Professional & MVP
              use the attach code option to display code in this forum.

              and you'll have a better chance of success with the code below:

              • 4. Re: Array in call back function
                YippySkippy-xx Level 1
                Thank you!!
                • 5. Re: Array in call back function
                  kglad Adobe Community Professional & MVP
                  you're welcome.