I’ve been following Joey Lott’s Advanced AS 2.0 video tutorials from Lynda.com, and am just getting into the part where he starts creating custom classes. While I understand the following code, more or less, I am curious to know how I would go about making a slight modification, and have it so that each Book instance is preserved rather than overwritten whenever the for loop increments. I suppose the way to do this would involve naming the variable dynamically… something like var bkItem*:Book. But how is that done?
var myXML:XML = new XML();
initializeXML();
function initializeXML():Void {
myXML.ignoreWhite = true;
myXML.onLoad = function(bSuccess:Boolean):Void {
var bkItem:Book;
var xnBook:XMLNode;
for (i=0; i<this.firstChild.childNodes.length; i++) {
xnBook = this.firstChild.childNodes*;
bkItem = new Book(xnBook.attributes.title, xnBook.attributes.author, xnBook.firstChild.nodeValue);
trace(bkItem.toString());
}
};
myXML.load("library.xml");
}
class Book {
private var _sTitle:String;
private var _sAuthor:String;
private var _sDescription:String;
function Book(sTitle:String, sAuthor:String, sDescription:String) {
_sTitle = sTitle;
_sAuthor = sAuthor;
_sDescription = sDescription;
}
public function toString():String {
var sOutput:String = _sTitle+newline+_sAuthor+newline+_sDescription;
//trace(sOutput);
return sOutput;
}
}
Fingers