From e258b0a0f99bd447dde5254d57bec7d1393b4698 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Tue, 5 Apr 2016 11:27:47 +0800 Subject: [PATCH] perfection scheduler API --- cocos2d/actions/CCActionInterval.js | 1 - cocos2d/core/CCScheduler.js | 216 ++++++++++++------ docs/cocos2d/core/CCScheduler/schedule.js | 1 - .../CCScheduler/scheduleCallbackForTarget.js | 4 +- .../CCScheduler/scheduleUpdateForTarget.js | 4 +- .../unscheduleCallbackForTarget.js | 4 +- .../CCScheduler/unscheduleUpdateForTarget.js | 4 +- 7 files changed, 156 insertions(+), 78 deletions(-) diff --git a/cocos2d/actions/CCActionInterval.js b/cocos2d/actions/CCActionInterval.js index 3fb7d14d35..13bb2fabcc 100644 --- a/cocos2d/actions/CCActionInterval.js +++ b/cocos2d/actions/CCActionInterval.js @@ -126,7 +126,6 @@ cc.ActionInterval = cc.FiniteTimeAction.extend({ * @param {Object} easeObj * @returns {ActionInterval} * @example - * //example * action.easeing(cc.easeIn(3.0)); */ easing: function (easeObj) { diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js index d4c4642c99..9adef99f14 100644 --- a/cocos2d/core/CCScheduler.js +++ b/cocos2d/core/CCScheduler.js @@ -93,7 +93,7 @@ var HashTimerEntry = function (timers, target, timerIndex, currentTimer, current }; /* - * Light weight timer + * Light weight timer. * @class Timer */ var Timer = cc._Class.extend({ @@ -203,19 +203,27 @@ var getTargetId = function (target) { }; /** - *

- * Scheduler is responsible of triggering the scheduled callbacks.
- * You should not use NSTimer. Instead use this class.
- *
- * There are 2 different types of callbacks (selectors):
- * - update callback: the 'update' callback will be called every frame. You can customize the priority.
- * - custom callback: A custom callback will be called every frame, or with a custom interval of time
- *
- * The 'custom selectors' should be avoided when possible. It is faster, and consumes less memory to use the 'update callback'. * - *

+ * !#en + * Scheduler is responsible of triggering the scheduled callbacks.
+ * You should not use NSTimer. Instead use this class.
+ *
+ * There are 2 different types of callbacks (selectors):
+ * - update callback: the 'update' callback will be called every frame. You can customize the priority.
+ * - custom callback: A custom callback will be called every frame, or with a custom interval of time
+ *
+ * The 'custom selectors' should be avoided when possible. It is faster, + * and consumes less memory to use the 'update callback'. * + * !#zh + * Scheduler 是负责触发回调函数的类。
+ * 通常情况下,建议使用 cc.director.getScheduler() 来获取系统定时器。
+ * 有两种不同类型的定时器:
+ * - update 定时器:每一帧都会触发。您可以自定义优先级。
+ * - 自定义定时器:自定义定时器可以每一帧或者自定义的时间间隔触发。
+ * 如果希望每帧都触发,应该使用 update 定时器,使用 update 定时器更快,而且消耗更少的内存。 + * * @class Scheduler */ -cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ +cc.Scheduler = cc._Class.extend({ _timeScale:1.0, //_updates : null, //_updates[0] list of priority < 0, _updates[1] list of priority == 0, _updates[2] list of priority > 0, @@ -342,14 +350,18 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ //-----------------------public method------------------------- /** - *

- * Modifies the time of all scheduled callbacks.
- * You can use this property to create a 'slow motion' or 'fast forward' effect.
- * Default is 1.0. To create a 'slow motion' effect, use values below 1.0.
- * To create a 'fast forward' effect, use values higher than 1.0.
- * @warning It will affect EVERY scheduled selector / action. - *

- * + * !#en + * Modifies the time of all scheduled callbacks.
+ * You can use this property to create a 'slow motion' or 'fast forward' effect.
+ * Default is 1.0. To create a 'slow motion' effect, use values below 1.0.
+ * To create a 'fast forward' effect, use values higher than 1.0.
+ * Note:It will affect EVERY scheduled selector / action. + * !#zh + * 设置时间间隔的缩放比例。
+ * 您可以使用这个方法来创建一个 “slow motion(慢动作)” 或 “fast forward(快进)” 的效果。
+ * 默认是 1.0。要创建一个 “slow motion(慢动作)” 效果,使用值低于 1.0。
+ * 要使用 “fast forward(快进)” 效果,使用值大于 1.0。
+ * 注意:它影响该 Scheduler 下管理的所有定时器。 * @method setTimeScale * @param {Number} timeScale */ @@ -358,7 +370,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Returns time scale of scheduler. + * !#en Returns time scale of scheduler. + * !#zh 获取时间间隔的缩放比例。 * @method getTimeScale * @return {Number} */ @@ -367,7 +380,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * 'update' the scheduler. (You should NEVER call this method, unless you know what you are doing.) + * !#en 'update' the scheduler. (You should NEVER call this method, unless you know what you are doing.) + * !#zh update 调度函数。(不应该直接调用这个方法,除非完全了解这么做的结果) * @method update * @param {Number} dt delta time */ @@ -454,6 +468,7 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** + * !#en *

* The scheduled method will be called every 'interval' seconds.
* If paused is YES, then it won't be called until it is resumed.
@@ -462,10 +477,20 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ * repeat let the action be repeated repeat + 1 times, use cc.macro.REPEAT_FOREVER to let the action run continuously
* delay is the amount of time the action will wait before it'll start
*

+ * !#zh + * 指定回调函数,调用对象等信息来添加一个新的定时器。
+ * 当时间间隔达到指定值时,设置的回调函数将会被调用。
+ * 如果 paused 值为 true,那么直到 resume 被调用才开始计时。
+ * 如果 interval 值为 0,那么回调函数每一帧都会被调用,但如果是这样, + * 建议使用 scheduleUpdateForTarget 代替。
+ * 如果回调函数已经被定时器使用,那么只会更新之前定时器的时间间隔参数,不会设置新的定时器。
+ * repeat 值可以让定时器触发 repeat + 1 次,使用 cc.macro.REPEAT_FOREVER + * 可以让定时器一直循环触发。
+ * delay 值指定延迟时间,定时器会在延迟指定的时间之后开始计时。 * @method scheduleCallbackForTarget * @deprecated since v3.4 please use .schedule * @param {Object} target - * @param {function} callback_fn + * @param {Function} callback_fn * @param {Number} interval * @param {Number} repeat * @param {Number} delay @@ -478,7 +503,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * The schedule + * !#en The schedule + * !#zh 定时器 * @method schedule * @param {Function} callback * @param {Object} target @@ -538,7 +564,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Schedules the update callback for a given target, the callback will be invoked every frame after schedule started + * !#en + * Schedules the update callback for a given target, + * the callback will be invoked every frame after schedule started. + * !#zh + * 使用指定的优先级为指定的对象设置 update 定时器。 + * update 定时器每一帧都会被触发。优先级的值越低,定时器被触发的越早。 * @method scheduleUpdate * @param {Object} target * @param {Number} priority @@ -561,9 +592,13 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ } }, - /** + /** + * !#en * Unschedules a callback for a callback and a given target. * If you want to unschedule the "update", use `unscheudleUpdate()` + * !#zh + * 根据指定的回调函数和调用对象。 + * 如果需要取消 update 定时器,请使用 unscheudleUpdate()。 * @method unschedule * @param {Function} callback The callback to be unscheduled * @param {Object} target The target bound to the callback. @@ -606,7 +641,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Unschedules the update callback for a given target + * !#en Unschedules the update callback for a given target. + * !#zh 取消指定对象的 update 定时器。 * @method unscheduleUpdate * @param {Object} target The target to be unscheduled. */ @@ -626,8 +662,10 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** + * !#en * Unschedules all scheduled callbacks for a given target. * This also includes the "update" callback. + * !#zh 取消指定对象的所有定时器,包括 update 定时器。 * @method unscheduleAllForTarget * @param {Object} target The target to be unscheduled. */ @@ -659,18 +697,26 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ this.unscheduleUpdate(target); }, - /** - * Unschedules all scheduled callbacks from all targets including the system callbacks. + /** + * !#en + * Unschedules all scheduled callbacks from all targets including the system callbacks.
* You should NEVER call this method, unless you know what you are doing. + * !#zh + * 取消所有对象的所有定时器,包括系统定时器。
+ * 不用调用此函数,除非你确定你在做什么。 * @method unscheduleAll */ unscheduleAll: function(){ this.unscheduleAllWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM); }, - /** - * Unschedules all callbacks from all targets with a minimum priority. + /** + * !#en + * Unschedules all callbacks from all targets with a minimum priority.
* You should only call this with `PRIORITY_NON_SYSTEM_MIN` or higher. + * !#zh + * 取消所有优先级的值大于指定优先级的定时器。
+ * 你应该只取消优先级的值大于 PRIORITY_NON_SYSTEM_MIN 的定时器。 * @method unscheduleAllWithMinPriority * @param {Number} minPriority The minimum priority of selector to be unscheduled. Which means, all selectors which * priority is higher than minPriority will be unscheduled. @@ -719,16 +765,17 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Checks whether a callback for a given target is scheduled. + * !#en Checks whether a callback for a given target is scheduled. + * !#zh 检查指定的回调函数和回调对象组合是否存在定时器。 * @method isScheduled - * @param {Function|String} key The callback to check. + * @param {Function} callback The callback to check. * @param {Object} target The target of the callback. * @return {Boolean} True if the specified callback is invoked, false if not. */ - isScheduled: function(key, target){ + isScheduled: function(callback, target){ //key, target //selector, target - cc.assert(key, "Argument key must not be empty"); + cc.assert(callback, "Argument callback must not be empty"); cc.assert(target, "Argument target must be non-nullptr"); var element = this._hashForUpdates[getTargetId(target)]; @@ -744,7 +791,7 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ for (var i = 0; i < timers.length; ++i){ var timer = timers[i]; - if (key === timer.getKey()){ + if (callback === timer.getKey()){ return true; } } @@ -753,11 +800,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - *

- * Pause all selectors from all targets.
- * You should NEVER call this method, unless you know what you are doing. - *

- * + * !#en + * Pause all selectors from all targets.
+ * You should NEVER call this method, unless you know what you are doing. + * !#zh + * 暂停所有对象的所有定时器。
+ * 不要调用这个方法,除非你知道你正在做什么。 * @method pauseAllTargets */ pauseAllTargets:function () { @@ -765,8 +813,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** + * !#en * Pause all selectors from all targets with a minimum priority.
* You should only call this with kCCPriorityNonSystemMin or higher. + * !#zh + * 暂停所有优先级的值大于指定优先级的定时器。
+ * 你应该只暂停优先级的值大于 PRIORITY_NON_SYSTEM_MIN 的定时器。 * @method pauseAllTargetsWithMinPriority * @param {Number} minPriority */ @@ -821,8 +873,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** + * !#en * Resume selectors on a set of targets.
* This can be useful for undoing a call to pauseAllCallbacks. + * !#zh + * 恢复指定数组中所有对象的定时器。
+ * 这个函数是 pauseAllCallbacks 的逆操作。 * @method resumeTargets * @param {Array} targetsToResume */ @@ -836,11 +892,14 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - *

- * Pauses the target.
- * All scheduled selectors/update for a given target won't be 'ticked' until the target is resumed.
- * If the target is not present, nothing happens. - *

+ * !#en + * Pauses the target.
+ * All scheduled selectors/update for a given target won't be 'ticked' until the target is resumed.
+ * If the target is not present, nothing happens. + * !#zh + * 暂停指定对象的定时器。
+ * 指定对象的所有定时器都会被暂停。
+ * 如果指定的对象没有定时器,什么也不会发生。 * @method pauseTarget * @param {Object} target */ @@ -864,9 +923,14 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** + * !#en * Resumes the target.
* The 'target' will be unpaused, so all schedule selectors/update will be 'ticked' again.
* If the target is not present, nothing happens. + * !#zh + * 恢复指定对象的所有定时器。
+ * 指定对象的所有定时器将继续工作。
+ * 如果指定的对象没有定时器,什么也不会发生。 * @method resumeTarget * @param {Object} target */ @@ -892,7 +956,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Returns whether or not the target is paused + * !#en Returns whether or not the target is paused. + * !#zh 返回指定对象的定时器是否暂停了。 * @method isTargetPaused * @param {Object} target * @return {Boolean} @@ -915,11 +980,14 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - *

- * Schedules the 'update' callback_fn for a given target with a given priority.
- * The 'update' callback_fn will be called every frame.
- * The lower the priority, the earlier it is called. - *

+ * !#en + * Schedules the 'update' callback_fn for a given target with a given priority.
+ * The 'update' callback_fn will be called every frame.
+ * The lower the priority, the earlier it is called. + * !#zh + * 为指定对象设置 update 定时器。
+ * update 定时器每一帧都会被调用。
+ * 优先级的值越低,越早被调用。 * @method scheduleUpdateForTarget * @deprecated since v3.4 please use .scheduleUpdate * @param {Object} target @@ -933,10 +1001,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - *

- * Unschedule a callback function for a given target.
- * If you want to unschedule the "update", use unscheudleUpdateForTarget. - *

+ * !#en + * Unschedule a callback function for a given target.
+ * If you want to unschedule the "update", use unscheudleUpdateForTarget. + * !#zh + * 根据指定的回调函数和调用对象对象取消相应的定时器。
+ * 如果需要取消 update 定时器,请使用 unscheudleUpdateForTarget()。 * @method unscheduleCallbackForTarget * @deprecated since v3.4 please use .unschedule * @param {Object} target @@ -949,7 +1019,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Unschedules the update callback function for a given target + * !#en Unschedules the update callback function for a given target. + * !#zh 取消指定对象的所有定时器。 * @method unscheduleUpdateForTarget * @param {Object} target * @deprecated since v3.4 please use .unschedule @@ -961,7 +1032,10 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - * Unschedules all function callbacks for a given target. This also includes the "update" callback function. + * !#en + * Unschedules all function callbacks for a given target.
+ * This also includes the "update" callback function. + * !#zh 取消指定对象的所有定时器,包括 update 定时器。 * @method unscheduleAllCallbacksForTarget * @deprecated since v3.4 please use unscheduleAllForTarget * @param {Object} target @@ -972,10 +1046,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - *

- * Unschedules all function callbacks from all targets.
- * You should NEVER call this method, unless you know what you are doing. - *

+ * !#en + * Unschedules all function callbacks from all targets.
+ * You should NEVER call this method, unless you know what you are doing. + * !#zh + * 取消所有对象的所有定时器。
+ * 不要调用这个方法,除非你知道你正在做什么。 * @method unscheduleAllCallbacks * @deprecated since v3.4 please use .unscheduleAllWithMinPriority */ @@ -985,10 +1061,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }, /** - *

- * Unschedules all function callbacks from all targets with a minimum priority.
- * You should only call this with kCCPriorityNonSystemMin or higher. - *

+ * !#en + * Unschedules all function callbacks from all targets with a minimum priority.
+ * You should only call this with kCCPriorityNonSystemMin or higher. + * !#zh + * 取消所有优先级的值大于指定优先级的所有对象的所有定时器。
+ * 你应该只暂停优先级的值大于 PRIORITY_NON_SYSTEM_MIN 的定时器。 * @method unscheduleAllCallbacksWithMinPriority * @deprecated since v3.4 please use .unscheduleAllWithMinPriority * @param {Number} minPriority @@ -1000,7 +1078,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ }); /** - * Priority level reserved for system services. + * !#en Priority level reserved for system services. + * !#zh 系统服务的优先级。 * @property PRIORITY_SYSTEM * @type {Number} * @static @@ -1008,7 +1087,8 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ cc.Scheduler.PRIORITY_SYSTEM = (-2147483647 - 1); /** - * Minimum priority level for user scheduling. + * !#en Minimum priority level for user scheduling. + * !#zh 用户调度最低优先级。 * @property PRIORITY_NON_SYSTEM * @type {Number} * @static diff --git a/docs/cocos2d/core/CCScheduler/schedule.js b/docs/cocos2d/core/CCScheduler/schedule.js index ccab42e5f1..7c07b16f4e 100644 --- a/docs/cocos2d/core/CCScheduler/schedule.js +++ b/docs/cocos2d/core/CCScheduler/schedule.js @@ -1,3 +1,2 @@ -------------------- //register a schedule to scheduler cc.director.getScheduler().schedule(callback, this, interval, !this._isRunning); diff --git a/docs/cocos2d/core/CCScheduler/scheduleCallbackForTarget.js b/docs/cocos2d/core/CCScheduler/scheduleCallbackForTarget.js index cb928db8ba..ac0761b904 100644 --- a/docs/cocos2d/core/CCScheduler/scheduleCallbackForTarget.js +++ b/docs/cocos2d/core/CCScheduler/scheduleCallbackForTarget.js @@ -1,3 +1,3 @@ -------------------- //register a schedule to scheduler -cc.director.getScheduler().scheduleCallbackForTarget(this, function, interval, repeat, delay, !this._isRunning ); +var scheduler = cc.director.getScheduler(); +scheduler.scheduleCallbackForTarget(this, function, interval, repeat, delay, !this._isRunning); diff --git a/docs/cocos2d/core/CCScheduler/scheduleUpdateForTarget.js b/docs/cocos2d/core/CCScheduler/scheduleUpdateForTarget.js index 3af3ee457c..6b3f469a53 100644 --- a/docs/cocos2d/core/CCScheduler/scheduleUpdateForTarget.js +++ b/docs/cocos2d/core/CCScheduler/scheduleUpdateForTarget.js @@ -1,3 +1,3 @@ -------------------- //register this object to scheduler -cc.director.getScheduler().scheduleUpdateForTarget(this, priority, !this._isRunning ); +var scheduler = cc.director.getScheduler(); +scheduler.scheduleUpdateForTarget(this, priority, !this._isRunning ); diff --git a/docs/cocos2d/core/CCScheduler/unscheduleCallbackForTarget.js b/docs/cocos2d/core/CCScheduler/unscheduleCallbackForTarget.js index af1c809c3a..3db2c55423 100644 --- a/docs/cocos2d/core/CCScheduler/unscheduleCallbackForTarget.js +++ b/docs/cocos2d/core/CCScheduler/unscheduleCallbackForTarget.js @@ -1,3 +1,3 @@ -------------------- //unschedule a callback of target -cc.director.getScheduler().unscheduleCallbackForTarget(function, this); +var scheduler = cc.director.getScheduler(); +scheduler.unscheduleCallbackForTarget(this, callback); diff --git a/docs/cocos2d/core/CCScheduler/unscheduleUpdateForTarget.js b/docs/cocos2d/core/CCScheduler/unscheduleUpdateForTarget.js index 25c769d006..cf84c1ce89 100644 --- a/docs/cocos2d/core/CCScheduler/unscheduleUpdateForTarget.js +++ b/docs/cocos2d/core/CCScheduler/unscheduleUpdateForTarget.js @@ -1,3 +1,3 @@ -------------------- //unschedules the "update" method. -cc.director.getScheduler().unscheduleUpdateForTarget(this); +var scheduler = cc.director.getScheduler(); +scheduler.unscheduleUpdateForTarget(this);