怎样在touch事件里,调用其他方法

cc.Class({
extends: cc.Component,
properties: {

},
changePlace_me:function(){
  // do someting  
},
onLoad: function () {
    this.node.on('touchend', function ( event ) {
        this.changePlace_me();})

})
代码入上。在his.node.on(‘touchend’, function ( event ) 中,this已经变了,所以调用this.changePlace_me就会报错,请问正确的方式是什么呢?

cc.Class({   
    extends: cc.Component,   
    properties: {

    },
changePlace_me:function(){
  // do someting  
},
onLoad: function () {
    var self = this;
    this.node.on('touchend', function ( event ) {
        self.changePlace_me();
   })
}
})

或者

cc.Class({   
    extends: cc.Component,   
    properties: {

},
changePlace_me:function(){
  // do someting  
},
onLoad: function () {
    this.node.on('touchend', function ( event ) {
        this.changePlace_me();
   }, this)
}
})
1赞

使用es6语法,箭头函数就ok了:

this.node.on('touchend', (event) => {
        this.changePlace_me();
})

还可以这样 万能的bind
this.node.on(‘touchend’, function ( event ) {
this.changePlace_me();
}.bind(this))

你function后面加一个参数 this,就对了