监听事件

onLoad: function () {
    this.time = 0 ;
    this.counting = false ;
    this.getComponent(cc.Label).string = this.counting;
    this.startgame.on('start',this.startcount,this.node);    //监听1
   // this.startcount();            //直接执行
},

startcount:function(){
    this.counting = true ;  
    this.getComponent(cc.Label).string = this.counting ;
},

update:function (dt) {
    if (this.counting) {
        this.time += dt;
        this.getComponent(cc.Label).string = this.time.toFixed(2) + ' s';
    }
}

备注中“直接执行”,startcount 可以将this.counting变成true.从而执行update,但是从监听1进入,则update不会执行,这是为什么。。。确认监听到事件了,label有显示

你的监听操作的target参数是this.node,所以startcount函数里的this是this.node这个对象,this.counting = true实际上是this.node.counting = true

你把target换成this应该就可以了

1赞

谢谢!可以了!