Hey FlashCoders,
Talkin' AS 2 here.
I am trying to do something that seems obvious. This was easy in AS1 adding
methods and properties via '.prototype'. But obviously I am doing something
wrong! Kudos and eternal gratitude to the Coder who can tell me what it is:
I am parsing XML with this structure:
<root>
<module>
<title>L1 title</title>
<section>
<title>L2 title</title>
<screen url="screen1.swf"/>
<screen url="screen2.swf"/>
</section>
</module>
</root>
I want to create an object in the root of an AS 2 app, such as:
var course:Course = new Course("theXML.xml");// file at same level as root swf
After creating this class, I want to access the arrays that are part of the
instantiated 'course' Object:
trace(course.modules[0].length);// returns 0. I cannot access the data. Wha
Zup?????????
Source available at http://www.cyburth.com/Flash_Source.zip
file: module.as looks like this:
Class Course {
private var modules:Array = new Array();
private var moduleTitles:Array = new Array();
// constructor
public function module(xml:String0{
this.loadDataFile(url);
}
public function loadDataFile(url:String):Void {
var crsData:XML = new XML();
crsData.ignoreWhite = true;
crsData.onLoad = function(success) {
this.tmpModules = new Array();
this.tmpModuleTitles = new Array();
var modNumb:Number=0;
for(var i:Number = 0;
i<crsData.firstChild.childNodes.length;i++){
var node:XMLNode = crsData.firstChild.childNodes[i];
if (node.nodeName == "module"){
var mod:Object = new module(node);
trace("So this mod title is -> "+mod.title);
this.tmpModules.push(mod.modules);
this.tmpModuleTitles.push(mod.title);
}// End if name == module
}
modules = this.tmpModules;
moduleTitles = this.tmpModuleTitles;
};// end onLoad
// Load the XML
crsData.load(url);
}
}
// file module.as looks like this:
class module {
public var sections:Array = new Array();
public var sectionTitles:Array = new Array();
public var title:String = "";
public function module(mod:XMLNode){
// This is the module class's constructor
trace(mod);// shows XML node correctly
for(var j:Number = 0; j<mod.childNodes.length;j++){
if(mod.childNodes[j].nodeName == "title"){
title =
mod.childNodes[j].childNodes[0].nodeValue;
trace("The title of this module is: "+title);//
returns title correctly
}
else if(mod.childNodes[j].nodeName == "section"){
var sec:section = new
section(mod.childNodes[j]); // points to class defined in file 'section.as'.
Similar structure as this class.
sections.push(sec.screens);
sectionTitles.push(sec.section_title);
trace(" section "+j+": "+sec.section_title+"
urls = "+sec.screens) //returns correct values
}
}
}
}
// file 'section.as' looks similar. it parses an xml node, puts child nodes
into an array.
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com