In a Flash game I am developing, there are some settings that are set by an external XML file. When I run the SWF file through the Flash IDE, it loads fine. If I run the same file as a projector (.exe) or the independent SWF file, it does not load the XML file.
My (unexpected) fix was to assign an error event listener to the loader object. When I published the file again, the XML loaded properly in the projector and standalone SWF files. (I have since verified that commenting out the error event handler restores the bug).
Here's the block of code involved (with extraneous code and function calls removed):
public function getSettings():void {
outputBox = getChildByName("output_box") as TextField;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, loadXML, false, 0, true);
xmlLoader.addEventListener(ErrorEvent.ERROR, function (e:Error)
{ outputBox.appendText(e.message) });
try {
xmlLoader.load(xmlPath);
}
catch(err:Error) {
trace(err.message);
outputBox.appendText(err.message);
checkChances("0");
}
function loadXML(e:Event):void {
try {
xmlData = new XML(e.target.data);
var chances:String = xmlData.chances.text();
var dbURL:String = xmlData.database.text();
trace("Chances are set to: " + chances);
trace("Database URL is set to: " + dbURL);
outputBox.appendText("Chances are set to: " + chances);
}
catch(err:Error) {
outputBox.appendText(err.message);
}
checkChances(chances);
dbPath = new URLRequest(dbURL);
}
}
Let me know if you have run into this, or if you can shed some light on what may be happening. Thanks!
EDIT:
Here is the code which does not work. (I also edited the code that does work to show all the other bits that I took out, just in case they might be effecting it):
public function getSettings():void {
outputBox = getChildByName("output_box") as TextField;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, loadXML, false, 0, true);
/*xmlLoader.addEventListener(ErrorEvent.ERROR, function (e:Error)
{ outputBox.appendText(e.message) });*/
try {
xmlLoader.load(xmlPath);
}
catch(err:Error) {
trace(err.message);
outputBox.appendText(err.message);
checkChances("0");
}
function loadXML(e:Event):void {
try {
xmlData = new XML(e.target.data);
var chances:String = xmlData.chances.text();
var dbURL:String = xmlData.database.text();
trace("Chances are set to: " + chances);
trace("Database URL is set to: " + dbURL);
outputBox.appendText("Chances are set to: " + chances);
}
catch(err:Error) {
outputBox.appendText(err.message);
}
checkChances(chances);
dbPath = new URLRequest(dbURL);
}
}