Thursday, February 21, 2008

Referencing a class function from inside of an XML object in AS2

Ok-- so this one tripped me up tonight. I'm converting an AS1 program to AS2 and thus moving functions into classes. So far so good, until I try to get a function to trigger after an XML.onLoad event:

myClass{
var myXML:XML = new XML();
myXML.onLoad = function(){
myFunction();
}
myXML.load("myXML.xml");

public myFunction ():void{
// function contents
}
}



-- but when tried to run this, I had no luck. You also can't use "this.myFunction()" either since "this" actually refers to the results that have been loaded. So, after a bit of Googling I found this Blog post from 2004:

http://www.bit-101.com/blog/?p=525

It turns out that the easiest way to fix it is to create a var inside the class that refers to "this" like so:

var myClass:Object = this;

Then you can simply run the example above and everything is hunky dory again ;-)

No comments: