A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: addEventListner on a loop loading XML files

  1. #1
    Junior Member
    Join Date
    Dec 2007
    Posts
    15

    Unhappy addEventListner on a loop loading XML files

    Hello,
    I noticed this forum as having some of the best minds and tips so far. I am new to as3 so I hope this makes sense to you gents.

    I have a bunch of XML files which I want to parse into an array.
    I have created a loop which goes one XML file at the time and fills in the array.




    //""""""""""""""""""""""""""""""""""""""""
    public function mainFunction(dataPathInput:String)
    {
    ...
    // collect all the files into an array
    XML_files_array = [("data/artists.xml"),("data/books.xml")];
    ...
    LoopThroughXMLFiles(XML_files_array);
    ...
    // query the array
    trace(final_itemEntries_array);
    }

    //""""""""""""""""""""""""""""""""""""""""
    public function LoopThroughXMLFiles(XMLarray):void
    {
    // loop through each XML file to collec all the data Im interested in
    for each (XML_file in XML_files_array) {
    initFrontPageXML(XML_file);
    }
    }

    //""""""""""""""""""""""""""""""""""""""""
    public function initFrontPageXML(menu_xml_input:String):void
    {
    ...
    var xmlLdr:URLLoader = new URLLoader();
    ...
    xmlLdr.addEventListener(Event.COMPLETE, xmlCompleteHandler);
    ...
    var request:URLRequest = new URLRequest(menu_xml_input);
    xmlLdr.load(request);
    ...
    }

    //""""""""""""""""""""""""""""""""""""""""
    private function xmlCompleteHandler(event:Event):void
    {
    ...
    for each (nodeXML in menuXMLList) {
    itemEntries_array.push({titling:nodeXML.@name});
    }
    ...
    // fill the array
    final_itemEntries_array = new Array(itemEntries_array);

    // this works so far as the array grows properly
    trace( final_itemEntries_array );
    }



    All of this works, but when I query the array from the mainFunction it is not filled in yet because, at least I think that is the case, the even handler has not started or finished yet.

    So I thought to put the loop into an eventListener of its own:



    //""""""""""""""""""""""""""""""""""""""""
    public function mainFunction(dataPathInput:String)
    {
    ...
    // collect all the files into an array
    XML_files_array = [("data/artists.xml"),("data/books.xml")];
    ...
    var myListener = new Object();
    myListener.complete = function(eventObject) {trace("loop is Finished"); };
    LoopThroughXMLFiles(XML_files_array).addEventListe ner("complete", myListener);
    ...
    // query the array
    trace(final_itemEntries_array);
    }



    But this way I get an error:


    "Call to a possibly undefined method addEventListener through a reference with static type void: LoopThroughXMLFiles(XML_files_array).addEventListe ner("complete", myListener);


    I dont have enough knowledge to know if Im on the right path or not, but in short, all I want is to loop through these XML files, collect the data into an array and ONLY ONCE that is done, use the array as I wish.

    Any suggestion or consideration would be welcome. Thank you everyone.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You are mixing up AS2 and AS3. This is AS2:

    var myListener = new Object();
    myListener.complete = function(eventObject) {trace("loop is Finished"); };
    LoopThroughXMLFiles(XML_files_array).addEventListe ner("complete", myListener);

    And I guess you are using AS3 as the main AS.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Dec 2007
    Posts
    15
    Ah. That would explain it, wouldnt it?
    Any trick you'd suggest for as3? Considering I make mistakes like this?

  4. #4
    Junior Member
    Join Date
    Dec 2007
    Posts
    15

    Unhappy

    hmmm.. trying this next:

    .....
    for (i=0; i<source.length; i++){
    XML_URL = "http://localhost/temp/" + source[i];
    myXMLURL = new URLRequest (XML_URL);
    myLoader = new URLLoader ();
    myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    myLoader.load(myXMLURL);
    }
    ....
    function xmlLoaded(evt:Event):void{
    content.push(evt.target.data);
    }


    ... nm. This would give the same issue where if I were to access content, it would still be empty...
    Last edited by Litow; 12-14-2007 at 02:27 PM.

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Before you add another eventlistener and assume something, first test if what you think is correct. Add a trace in the for .. in loop.
    - The right of the People to create Flash movies shall not be infringed. -

  6. #6
    Junior Member
    Join Date
    Dec 2007
    Posts
    15
    Good question, thank you for the heads up.

    The reason I replied to myself is beacuse I noticed Im doing the same already just with a different process.

    In any case, I tried your suggestion to trace both my new array and the one that is "pushed" after parsing the XML files with
    LoopThroughXMLFiles(XML_files_array);
    and they are both empty:

    LoopThroughXMLFiles(XML_files_array);
    trace("final_itemEntries_array " + final_itemEntries_array); // returns null
    trace("itemEntries_array " + itemEntries_array.length); // returns 0

    also, interesting thing is that they resolve in the output window before the trace in my xmlCompleteHandler loop... which makes me think Im not coding the eventListener in the loop correctly.
    Last edited by Litow; 12-14-2007 at 02:56 PM.

  7. #7
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    It sounds like your code is continuing before the XML file has been parsed. I understand the need for the event listener. When my XML method was done with what it had to do....parsing and all....I simply had it call another function which continued the code......

    I didn't think about adding an listener, but that a better idea......The listener should appear at the bottom of the loop, but you must make sure your code cannot continue regardless. The listener should call another function.

    Does that make sense?
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  8. #8
    Junior Member
    Join Date
    Dec 2007
    Posts
    15
    I'll take anything now! I'll try that next. hehehe
    Ive been stuck here all day.
    Thank you Samac.

  9. #9
    Junior Member
    Join Date
    Dec 2007
    Posts
    15

    [RESOLVED] addEventListner on a loop loading XML files

    Just added a counter to stop the loop when the size of the array was met:

    XML_files_array_size = XML_files_array.length;
    XML_files_array_counter = 0;

    LoopThroughXMLFiles(XML_files_array);//load XML

    ...

    XML_files_array_counter++;
    if(XML_files_array_counter == XML_files_array_size){
    ...
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center