When you call a method of a class inside a callback function, you can not use this object.
To call the method, in javascript, I declare that variable, assign this to that, and use that inside the callback to call the method of this.
In actionscript, do I have to do the same way as I do in javascript?
The following code is the example to use that to call a method inside callback.
Are there more simple way in actionscript?
class C {
private var that:C;
function C() {
that = this
}
public function f1():void {
var sp:Sprite = new Sprite;
sp.addEventListener(MouseEvent.CLICK, function():void {
this.f2(); // this doesn't work
that.f2(); // that works
});
}
public function f2():void {
trace('hello');
}
}
Here’s another way to do that: