新人求助 为什么我打的实例方法用不了?

我按照入门教程里的快速上手做个游戏里的方式在尝试制作,但是我自己想实现些别的东西,于是自己尝试着打了一串脚本,但是我打的方法出现错误。
var player = require (“player”);

cc.Class({
extends: cc.Component,

properties: {

},
runGame:function(){
    log("runGame function is comletely run.");
    player.node.runstart();
    },
onLoad: function () {

    log("startbtn onLoad this:"+this.name);
   this.node.on('mouseenter',function(event){
       log("callfunc this:"+this.name);
       this.runAction(cc.scaleTo(0.1,1.2));
   });
   this.node.on('mouseleave',function(event){
       this.runAction(cc.scaleTo(0.1,1));
   });
   this.node.on('mousedown',function(event){
       this.runGame();
   })

},

});

之后预览游戏时,出现错误:
Simulator : 145:TypeError: this.runGame is not a function
求高手指点,本人纯新手,卡在这里很长时间了。

你需要指定函数调用的target

或者你在事件回调外面用变量保存this

var self = this;

然后在你的事件回调里调用

self.runGame()

如果你不这样做,你函数里的this有可能并不是当前组件,所以当然找不到runGame这个方法了

1赞

谢谢,终于解决了。

有 啊 威哦抗:grin: