首页 > 代码库 > 定时器暂停以及完全删除

定时器暂停以及完全删除

 1 var SchedulerPauseResumeTest = cc.Layer.extend({ 2     ctor : function(){ 3         this._super(); 4 //添加监听 5      6         this.schedule(this.onTick1, 0.5); 7         this.schedule(this.onTick2, 0.5); 8         this.schedule(this.onPause, 3); 9     },10     onTick1:function (dt) {11         logTest("tick1");12     },13     onTick2:function (dt) {14         logTest("tick2");15     },16     onPause:function (dt) {17         cc.Director.getInstance().getScheduler().pauseTarget(this);18     }19 });20 21 SchedulerPauseResumeTest.create = function(args){22     var layer = new SchedulerPauseResumeTest();23     return layer.init() ? layer : null;24 };

展示如何暂停事件.

展示如何删除事件

var SchedulerUnscheduleAllTest = cc.Layer.extend({    ctor : function(){        this._super();        this.schedule(this.onTick1, 0.5);        this.schedule(this.onTick2, 1.0);        this.schedule(this.onTick3, 1.5);        this.schedule(this.onTick4, 1.5);        this.schedule(this.onUnscheduleAll, 4);    },    onTick1:function (dt) {        logTest("tick1");    },    onTick2:function (dt) {        logTest("tick2");    },    onTick3:function (dt) {        logTest("tick3");    },    onTick4:function (dt) {        logTest("tick4");    },    onUnscheduleAll:function (dt) {        this.unscheduleAllCallbacks();    }});SchedulerUnscheduleAllTest.create = function(args){    var layer = new SchedulerUnscheduleAllTest();    return layer.init() ? layer : null;};

 还有一种是硬删除:

var SchedulerUnscheduleAllHardTest = cc.Layer.extend({    ctor : function(){        this._super();        this.schedule(this.onTick1, 0.5);        this.schedule(this.onTick2, 1.0);        this.schedule(this.onTick3, 1.5);        this.schedule(this.onTick4, 1.5);        this.schedule(this.onUnscheduleAll, 4);    },    onTick1:function (dt) {        logTest("tick1");    },    onTick2:function (dt) {        logTest("tick2");    },    onTick3:function (dt) {        logTest("tick3");    },    onTick4:function (dt) {        logTest("tick4");    },    onUnscheduleAll:function (dt) {        cc.Director.getInstance().getScheduler().unscheduleAllCallbacks();    }});SchedulerUnscheduleAllHardTest.create = function(args){    var layer = new SchedulerUnscheduleAllHardTest();    return layer.init() ? layer : null;};