Skip to main content
Participant
August 10, 2011
Question

Using XML data in main fla's array

  • August 10, 2011
  • 2 replies
  • 463 views

Hi people,
I'm having some trouble trying to use the xml's array to store into the main fla file's array so that i can randomize the array for use. Any idea how I could extract the data out from the xmlloader?

Here is the code for the main fla file:

Code:

               var xmlLoader: URLLoader = new URLLoader();
          var xmlData: XML = new XML();
          var factsArrays: Array = new Array();

          function xmlTutorial()
          {
               xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
               xmlLoader.load(new URLRequest("FactText.xml"));
          }

          function LoadXML(e:Event):void
          {
               trace("XML File Loaded");
               xmlData = new XML(e.target.data);
               for (var i in xmlData.item) {
                    factsArrays.push({text:xmlData.item.text, url:xmlData.item.url});
               }
               trace(factsArrays[0].text);
               trace(factsArrays[1].text);
          }


                var newFactArray:Array = new Array();

                function startGame():void{//we'll run this function every time a new level begins
          
             for(var i:int=0;i<enemyArray[currentLvl-1].length;i++){
              if(enemyArray[currentLvl-1] != 0){
               enemiesLeft ++;
          }
                      }
                      this.xmlTutorial();
                      factRandomArray();
                      trace(newFactArray[0].toString());
                      trace(newFactArray[1].toString());
                      trace(newFactArray[2].toString());
                      trace(newFactArray[3].toString());
                      trace(newFactArray[4].toString());
                         }

               
                //randomize the facts
                function factRandomArray():Array {
      
                 while(factArray.length > 0)
                 {
                     var obj:Array = factArray.splice(Math.floor(Math.random()*factArray.length), 1);
                    newFactArray.push(obj[0]);
                    }
                        return newFactArray;
                }

Thanks in advance

This topic has been closed for replies.

2 replies

Inspiring
August 10, 2011

Try this (read comment in factRandomArray):

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var factsArrays:Array = new Array();
var newFactArray:Array = new Array();

xmlTutorial();

function xmlTutorial()
{
     xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
     xmlLoader.load(new URLRequest("FactText.xml"));
}

function LoadXML(e:Event):void
{
     trace("XML File Loaded");
     xmlData = new XML(e.target.data);
     for (var i in xmlData.item)
     {
          factsArrays.push({text: xmlData.item.text, url: xmlData.item.url});
     }
     trace(factsArrays[0].text);
     trace(factsArrays[1].text);
     startGame();
}

function startGame():void
{ //we'll run this function every time a new level begins
     
     for (var i:int = 0; i < enemyArray[currentLvl - 1].length; i++)
     {
          if (enemyArray[currentLvl - 1] != 0)
          {
               enemiesLeft++;
          }
     }
     factRandomArray();
     trace(newFactArray[0].toString());
     trace(newFactArray[1].toString());
     trace(newFactArray[2].toString());
     trace(newFactArray[3].toString());
     trace(newFactArray[4].toString());
}

//randomize the facts
function factRandomArray():Array
{
     newFactArray = [];
     // preserve original factArray and make/manipulate it's copy
     // so that you can use for other game levels
     var arrayCopy:Array = factArray.slice();
     while (arrayCopy.length > 0)
     {
          var obj:Array = arrayCopy.splice(Math.floor(Math.random() * arrayCopy.length), 1);
          newFactArray.push(obj[0]);
     }
     return newFactArray;
}

Kenneth Kawamoto
Community Expert
Community Expert
August 10, 2011

At first glance, this caught my eyes:

...

this.xmlTutorial();
factRandomArray();
trace(newFactArray[0].toString());

...

At least for the first time you run the function you will get nothing for your trace. Loading is asynchronous - when you trace your Array is empty because XML is not yet loaded.

Participant
August 10, 2011

Yup, I found out that the tracing loads after the error was formed. So I put it into an earlier frame so that the data loads 1st.