I have the following script which works jim-dandy when implimented as a frame
script in my .swf, but I don't want it there, I want it in a class I've created
called FigureItem. I've stripped the script down to just the part that loads
the XML. When the script is inside the .swf (without the class constructor,
obviously) 'trace(contentXML)' correctly returns the formatted XML. Inside the
.as 'trace(contentXML)' returns 'undefined'. SOMETHING is loading becase
'trace("yay")' returns 'yay'. (loadXML gets called by the FiguireItem movie
clip inside the .swf).
class FigureItem extends MovieClip {
var rollBox:MovieClip;
var contentXML:XML;
//
public function FigureItem() {
contentXML = new XML();
}
public function loadXML(myURL:String):Void {
contentXML.onLoad = function(success:Boolean):Void {
if (success) {
trace ("yay");
trace(contentXML);
} else {
trace ("yay");
}
};
contentXML.load(myURL);
}
}
Raymond Basque - 23 Aug 2006 16:26 GMT
contentXML is out of scope from the XML object;
Here are a few ways to deal with scope:
public function loadXML(myURL:String):Void {
var xmlOwner = this;
contentXML._OWNERREF = this;
contentXML.onLoad = function(success:Boolean):Void {
if (success) {
trace ("yay");
trace(this); // <===
trace(xmlOwner.contentXML); // <=====
trace(this._OWNERREF.contentXML); // <=========
} else { trace ("yay"); }
};
contentXML.load(myURL);
}
maija_g - 23 Aug 2006 16:55 GMT
Thanks, but that doesn't seem to work. I copied and pasted and immediately got
a pile of errors. I commented out all the stuff you added in until there were
no errors and then started uncommenting until I got an error. Below is the
first one.
class FigureItem extends MovieClip {
var contentXML:XML;
//
public function FigureItem() {
contentXML = new XML();
contentXML.onLoad = function(success:Boolean):Void {
if (success) {
trace ("yay");
//trace(this); // <===
//trace(xmlOwner.contentXML); // <=====
//trace(this._OWNERREF.contentXML); // <=========
} else {
trace ("booooo");
}
};
contentXML.load(myURL);
}
}
/**Error** blah/blah/blah/FigureItem.as: Line 11: There is no property with
the name '_OWNERREF'.
contentXML._OWNERREF = this;
*/
maija_g - 23 Aug 2006 17:00 GMT
Never mind, the 'trace (this)' and 'trace (xmlOwner.contentXML)' both worked so I'll use one of those. Thanks!!