It will throw a TypeError: this.test is not a function when I dispatch this event, so I guess it is because of the difference of the “this pointer” when I step into the callback.
So generally, my solution is to pass a parameter to the callback to tell the callback function “this pointer”. I think there should be some better solutions.
Hi, in fact, the this object is the reference of the listener, and if you want the outer scope this, you can do something like this:
cc.eventManager.addListener(
{
.....
onTouchesBegan:function(touch,event)
{
var outerThis = event.getCurrentTarget();
outerThis.test();
}
}, this); // The this here will be passed with event
In general, the callback functions’ this doesn’t refer to the outer this in javascript, there is no rule, it depend on how the callback is invoked, please search JavaScript variable scope and context for more informations