Original Post
Hi there. I have a class that has an array as a private property and I'm trying to load an XML file's contents into it. The code for the class is (cut-down example): The XML file I'm loading: The code to call the class (again, cut-down):
class CScreenCredits
{
private var m_credits_lines:Array;
function CScreenCredits()
{
// create credits array
this.m_credits_lines = new Array();
}
private function AddCreditLine(Text:String, Format:String):Number
{
return this.m_credits_lines.push([Text, Format]);
}
private function LoadCredits(Filename:String):Boolean
{
// clear previous credits
this.m_credits_lines = [];
// get copy of this as actionscript gets confused (again *sigh*)
var this_object = this;
// setup xml loader
var xml_loader:XML = new XML();
xml_loader.ignoreWhite = true;
// loader completion function
xml_loader.onLoad = function(HasLoaded:Boolean)
{
if (HasLoaded)
{
this_object.ProcessXMLFile(this);
}
}
// do load
return xml_loader.load(Filename);
}
private function ProcessXMLFile(XMLFile:XML):Void
{
for (var i=0;i<XMLFile.firstChild.childNodes.length;i++)
{
var line_node:XMLNode = XMLFile.firstChild.childNodes;
var new_line:Array = new Array();
for (var j in line_node.childNodes)
{
var node_type:String = line_node.childNodes[j].nodeName;
var node_value:String = line_node.childNodes[j].firstChild.nodeValue;
if (node_type == "text")
{
new_line[0] = node_value;
}
else
{
if (node_type == "format")
{
new_line[1] = node_value;
}
}
}
// TRACE ADD HERE
this.AddCreditLine(new_line[0], new_line[1]);
}
}
public function Build():Void
{
trace("before:" + this.m_credits_lines.length);
// load credits text
this.LoadCredits("./conf/credits.xml");
trace("after:" + this.m_credits_lines.length);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<credits>
<line>
<format>bold</format>
<text>Code</text>
</line>
<line>
<format>normal</format>
<text>My Name</text>
</line>
<line>
<format>bold</format>
<text>Thanks To</text>
</line>
<line>
<format>normal</format>
<text>Another Name</text>
</line>
</credits>
var my_screen:CScreenCredits = new CScreenCredits();
my_screen.Build(); Although tracing the XML loading (adding a trace at "TRACE ADD HERE") reveals the XML is loading fine. The two traces further down in the Build() method show no change in the array's size (length 0 and length 0 respectively). I've been at this for a while now and it's becoming most annoying. I'm on FlashDevelop 2.02 final if it's of any consequence. Any help would be great. Many thanks. [Edited by - eSCHEn on April 10, 2007 3:03:34 AM]