HI
I'm trying to access RootNode in the code from anywhere in the timeline. I
assigned a value in the loadFile function but when I try to access it from
outside this function I get "undefined". How can I have this easily accessible
from anywhere in the code? I know _global.RootNode will not work for XMLnode
types.
Thanks!
// Initialize variables
var selectedUnitSize:String = "studio";
var selectedFloor:Number;
var matchingFloors:Array;// for Select a floor component
var matchingUnits:Array;// for keyplan component
var RootNode:XMLNode;
// Creates XML Object
var floorplansXML:XML = new XML();
floorplansXML.ignoreWhite = true;
floorplansXML.onLoad = loadFile;
floorplansXML.load("xml/floorplans.xml");
function init(){
loadAllFloorplans();
}
// Accesses XML data from the XML object
function loadFile(success:Boolean):Void {
if (success) {
RootNode = this.firstChild;
trace(RootNode);
} else {
trace("Error in loading XML file");
}
};
// Loads selected floorplan data from XML file
function loadAllFloorplans(selectedUnitSize) {
//trace(RootNode);
for (var i:Number = 0; i<RootNode.childNodes.length; i++) {
nodeUnitSize = RootNode.childNodes[i].attributes.unitsize;
if (nodeUnitSize == selectedUnitSize) {
matchingUnits.push(RootNode.childNodes[i]);
break;
}
}
};
init();
Noelbaland - 11 Jul 2008 04:10 GMT
Hello there,
Don't use the init function as you lose the scope to your RootNode variable.
You should instead call the loadAllFloorplans function from inside the loadFile
function.
if(success){
RootNode = this.firstChild;
// Pass in the selectedUnitSize variable as a parameter here
loadAllFloorplans(selectedUnitSize);
}
and then in the loadAllFloorPlans function set a different name for the
parameter and strict type it to Number.
function loadAllFloorPlans(unitSize:Number) {
// Use unitSize throughout the rest of your script
for (var i:Number = 0; i<RootNode.childNodes.length; i++) {
nodeUnitSize = RootNode.childNodes[i].attributes.unitsize;
if (nodeUnitSize == unitSize) {
matchingUnits.push(RootNode.childNodes[i]);
break;
}
}
};