I am having an issue with scope in a class created in flash. I am creating an application that utilizes XML to create an array of information that I will manipulate and re-save to XML.
I have a document class that is calling calling another class that I am using to load the XML, convert it into an array and return the array to the document constructor using a method. I have successfully parsed the XML and created an array of the data but can't seem to add data to the array via the processXML function inside the loadXMLData class.
I stripped out all the stuff from the code that doesn't matter. Here is the basic representation of what i am trying to do.
My Document Class
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.*;
public class dashBoard extends MovieClip {
public var newData
//initialize the dashBoard
public function dashBoard(){
//construct the Dashboard Object
trace("Dashboard Initialized");
trace("|--------------XML Data--------------|");
newData = new loadXMLData();
trace(newData.getSections());
}
}
}
My loadXMLData Class
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.*;
public class loadXMLData extends MovieClip {
//initialize an array that will be used in the document class
public var sectionList:Array = new Array();
public function loadXMLData() {
//load the xml file containing the data
var myLoader = new URLLoader();
myLoader.load(new URLRequest("dashboard.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
//process the xml file after it loads
//and create an object
var newXML:XML = new XML(e.target.data);
//then I use the XML to populate my
//array i declared at the top
//this is a simple test
sectionList[0] = "test";
}
}
//and this is the method i use in the document class
//to get the sectionList array
public function getSections():Array{
return sectionList;
}
}
}
It seems pretty straightforward but I can't seem to edit the array. The document class always returns a blank array. Any Ideas?
You're trying to access information that doesn't yet exist.Dispatch an event in your
loadXMLData.processXML()
function when the XML has finished loading, like so:Then listen for it before you do anything like this:
There is a very real possibility that the line:
trace(newData.getSections());
occurs before the function processXML, which is what populates the array. This is because the processXML is responding to the Event.COMPLETE, which may take, say a few seconds to complete.Your document should also 'listen' for an event (which your loader class should dispatch) before trying to output it.
It's not a scope issue, you're constructing the loadXMLData and then immediately trying to get the sectionList, without waiting for it to load. The constructor call and call to getSections are synchronous, but the actual XML file load is asynchronous.
Ideally, your loadXMLData object will dispatch a COMPLETE event from the processXML handler function, which your document class listens for (just as your loadXMLData listens for the Loader to complete)