PDA

View Full Version : Variable Scope Question...


ellils
05-14-2008, 06:01 AM
Hi guys,

I have a variable scope problem with this code:

var XML4:XML = new XML();
var pathtologo = new String();
XML4.ignoreWhite = true;
XML4.onLoad = function(success) {
if(success){
pathtologo = XML4.firstChild.childNodes[3];
trace("(1) Path to logo is: "+pathtologo);
}
}
conXML4.load("common.xml");

trace("(2) Path to logo is: "+pathtologo);


trace on (2) gives no value and I would like to somehow get the value there instead of being forced to use only method (1) to generate a value.

How should I do that?

Noct
05-14-2008, 07:22 AM
Welcome aboard,

Scope isn't the issue here. The problem is that your second trace actually fires before the xml is loaded, at which point your variable still has no value.

You need to wait until the xml is fully loaded. For example, this would probably do the trick:

var XML4:XML = new XML();
var pathtologo:String;
XML4.ignoreWhite = true;
XML4.onLoad = function(success) {
if (success) {
pathtologo = this.firstChild.childNodes[3];
trace("(1) Path to logo is: "+pathtologo);
}
};
XML4.load("common.xml");
this.onEnterFrame = function() {
if (pathtologo) {
trace("(2) Path to logo is: "+pathtologo);
delete this.onEnterFrame;
}
};