From 347a513eeb321b9571f157400595412c465063c6 Mon Sep 17 00:00:00 2001 From: dimon4eg Date: Fri, 14 Apr 2017 23:28:30 +0300 Subject: [PATCH 1/2] Fix schedule issue --- cocos2d/core/CCScheduler.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js index 0c75dc15af..943c593b8e 100644 --- a/cocos2d/core/CCScheduler.js +++ b/cocos2d/core/CCScheduler.js @@ -222,22 +222,20 @@ cc.inject({ } else {//advanced usage if (this._useDelay) { if (this._elapsed >= this._delay) { + this._timesExecuted += 1; // important to increment before call trigger this.trigger(); - this._elapsed -= this._delay; - this._timesExecuted += 1; this._useDelay = false; } } else { if (this._elapsed >= this._interval) { + this._timesExecuted += 1; // important to increment before call trigger this.trigger(); - this._elapsed = 0; - this._timesExecuted += 1; } } - if (this._callback && !this._runForever && this._timesExecuted > this._repeat) + if (this._callback && this.isExhausted()) this.cancel(); } } @@ -260,6 +258,10 @@ cc.inject({ cancel: function () { //override this._scheduler.unschedule(this._callback, this._target); + }, + + isExhausted: function () { + return !this._runForever && this._timesExecuted > this._repeat; } }, CallbackTimer.prototype); @@ -609,7 +611,7 @@ cc.Scheduler = cc.Class.extend(/** @lends cc.Scheduler# */{ } else { for (i = 0; i < element.timers.length; i++) { timer = element.timers[i]; - if (callback === timer._callback) { + if (!timer.isExhausted() && callback === timer._callback) { cc.log(cc._LogInfos.Scheduler_scheduleCallbackForTarget, timer.getInterval().toFixed(4), interval.toFixed(4)); timer._interval = interval; return; From 738c50c07fc6c95afc33733acf9dacf0ad3bed04 Mon Sep 17 00:00:00 2001 From: dimon4eg Date: Sun, 21 Jan 2018 13:58:56 +0200 Subject: [PATCH 2/2] sync with cocos2d-x (update interval correctly) --- cocos2d/core/CCScheduler.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js index 943c593b8e..d83805741b 100644 --- a/cocos2d/core/CCScheduler.js +++ b/cocos2d/core/CCScheduler.js @@ -193,16 +193,9 @@ cc.inject({ this._useDelay = (this._delay > 0); this._repeat = repeat; this._runForever = (this._repeat === cc.REPEAT_FOREVER); + this._timesExecuted = 0; return true; }, - /** - * @return {Number} returns interval of timer - */ - getInterval : function(){return this._interval;}, - /** - * @param {Number} interval set interval in seconds - */ - setInterval : function(interval){this._interval = interval;}, /** * triggers the timer @@ -613,7 +606,7 @@ cc.Scheduler = cc.Class.extend(/** @lends cc.Scheduler# */{ timer = element.timers[i]; if (!timer.isExhausted() && callback === timer._callback) { cc.log(cc._LogInfos.Scheduler_scheduleCallbackForTarget, timer.getInterval().toFixed(4), interval.toFixed(4)); - timer._interval = interval; + timer.initWithCallback(this, callback, target, interval, repeat, delay, key); return; } }