* 这个方法会给对象添加一个 `_id` 属性,如果这个属性不存在的话。
* @param {Object} target
*/
public enableForTarget (target) {
if (!target._id) {
if (target.__instanceId) {
cc.warnID(1513);
}
else {
target._id = idGenerator.getNewId();
}
}
}
/**
* @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 下管理的所有定时器。
* @param {Number} timeScale
*/
public setTimeScale (timeScale) {
this._timeScale = timeScale;
}
/**
* @en Returns time scale of scheduler.
* @zh 获取时间间隔的缩放比例。
* @return {Number}
*/
public getTimeScale (): number {
return this._timeScale;
}
/**
* @en 'update' the scheduler. (You should NEVER call this method, unless you know what you are doing.)
* @zh update 调度函数。(不应该直接调用这个方法,除非完全了解这么做的结果)
* @param {Number} dt delta time
*/
public update (dt) {
this._updateHashLocked = true;
if (this._timeScale !== 1) {
dt *= this._timeScale;
}
let i: number;
let list;
let len: number;
let entry;
for (i = 0, list = this._updatesNegList, len = list.length; i < len; i++){
entry = list[i];
if (!entry.paused && !entry.markedForDeletion) {
entry.target.update(dt);
}
}
for ( i = 0, list = this._updates0List, len = list.length; i < len; i++){
entry = list[i];
if (!entry.paused && !entry.markedForDeletion) {
entry.target.update(dt);
}
}
for ( i = 0, list = this._updatesPosList, len = list.length; i < len; i++){
entry = list[i];
if (!entry.paused && !entry.markedForDeletion) {
entry.target.update(dt);
}
}
// Iterate over all the custom selectors
let elt;
const arr = this._arrayForTimers;
for ( i = 0; i < arr.length; i++){
elt = arr[i];
this._currentTarget = elt;
this._currentTargetSalvaged = false;
if (!elt.paused){
// The 'timers' array may change while inside this loop
for (elt.timerIndex = 0; elt.timerIndex < elt.timers.length; ++(elt.timerIndex)){
elt.currentTimer = elt.timers[elt.timerIndex];
elt.currentTimerSalvaged = false;
elt.currentTimer.update(dt);
elt.currentTimer = null;
}
}
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
if (this._currentTargetSalvaged && this._currentTarget.timers.length === 0) {
this._removeHashElement(this._currentTarget);
--i;
}
}
// delete all updates that are marked for deletion
// updates with priority < 0
for ( i = 0, list = this._updatesNegList; i < list.length; ){
entry = list[i];
if (entry.markedForDeletion) {
this._removeUpdateFromHash(entry);
}
else {
i++;
}
}
for ( i = 0, list = this._updates0List; i < list.length; ){
entry = list[i];
if (entry.markedForDeletion) {
this._removeUpdateFromHash(entry);
}
else {
i++;
}
}
for ( i = 0, list = this._updatesPosList; i < list.length; ){
entry = list[i];
if (entry.markedForDeletion) {
this._removeUpdateFromHash(entry);
}
else {
i++;
}
}
this._updateHashLocked = false;
this._currentTarget = null;
}
/**
* @en
*
* The scheduled method will be called every 'interval' seconds.
* If paused is YES, then it won't be called until it is resumed.
* If 'interval' is 0, it will be called every frame, but if so, it recommended to use 'scheduleUpdateForTarget:' instead.
* If the callback function is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
* 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 值指定延迟时间,定时器会在延迟指定的时间之后开始计时。
* @param {Function} callback
* @param {Object} target
* @param {Number} interval
* @param {Number} [repeat=cc.macro.REPEAT_FOREVER]
* @param {Number} [delay=0]
* @param {Boolean} [paused=fasle]
*/
public schedule (callback: Function, target: any, interval: number, repeat: number, delay: number, paused?: boolean) {
'use strict';
if (typeof callback !== 'function') {
const tmp = callback;
callback = target;
target = tmp;
}
// selector, target, interval, repeat, delay, paused
// selector, target, interval, paused
if (arguments.length === 4 || arguments.length === 5) {
paused = !!repeat;
repeat = cc.macro.REPEAT_FOREVER;
delay = 0;
}
cc.assertID(target, 1502);
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
let element = this._hashForTimers[targetId];
if (!element) {
// Is this the 1st element ? Then set the pause level to all the callback_fns of this target
element = HashTimerEntry.get(null, target, 0, null, null, paused);
this._arrayForTimers.push(element);
this._hashForTimers[targetId] = element;
} else if (element.paused !== paused) {
cc.warnID(1511);
}
let timer;
let i;
if (element.timers == null) {
element.timers = [];
}
else {
for (i = 0; i < element.timers.length; ++i) {
timer = element.timers[i];
if (timer && callback === timer._callback) {
cc.logID(1507, timer.getInterval(), interval);
timer._interval = interval;
return;
}
}
}
timer = CallbackTimer.get();
timer.initWithCallback(this, callback, target, interval, repeat, delay);
element.timers.push(timer);
if (this._currentTarget === element && this._currentTargetSalvaged) {
this._currentTargetSalvaged = false;
}
}
/**
* @en
* Schedules the update callback for a given target,
* During every frame after schedule started, the "update" function of target will be invoked.
* @zh
* 使用指定的优先级为指定的对象设置 update 定时器。
* update 定时器每一帧都会被触发,触发时自动调用指定对象的 "update" 函数。
* 优先级的值越低,定时器被触发的越早。
* @param {Object} target
* @param {Number} priority
* @param {Boolean} paused
*/
public scheduleUpdate (target: any, priority: Number, paused: Boolean) {
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
const hashElement = this._hashForUpdates[targetId];
if (hashElement && hashElement.entry){
// check if priority has changed
if (hashElement.entry.priority !== priority){
if (this._updateHashLocked){
cc.logID(1506);
hashElement.entry.markedForDeletion = false;
hashElement.entry.paused = paused;
return;
}else{
// will be added again outside if (hashElement).
this.unscheduleUpdate(target);
}
}else{
hashElement.entry.markedForDeletion = false;
hashElement.entry.paused = paused;
return;
}
}
const listElement = ListEntry.get(target, priority, paused, false);
let ppList;
// most of the updates are going to be 0, that's way there
// is an special list for updates with priority 0
if (priority === 0) {
ppList = this._updates0List;
this._appendIn(ppList, listElement);
}
else {
ppList = priority < 0 ? this._updatesNegList : this._updatesPosList;
this._priorityIn(ppList, listElement, priority);
}
// update hash entry for quick access
this._hashForUpdates[targetId] = HashUpdateEntry.get(ppList, listElement, target, null);
}
/**
* @en
* Unschedules a callback for a callback and a given target.
* If you want to unschedule the "update", use `unscheduleUpdate()`
* @zh
* 根据指定的回调函数和调用对象。
* 如果需要取消 update 定时器,请使用 unscheduleUpdate()。
* @param {Function} callback The callback to be unscheduled
* @param {Object} target The target bound to the callback.
*/
public unschedule (callback, target) {
// callback, target
// explicity handle nil arguments when removing an object
if (!target || !callback) {
return;
}
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
const self = this;
const element = self._hashForTimers[targetId];
if (element) {
const timers = element.timers;
for (let i = 0, li = timers.length; i < li; i++) {
const timer = timers[i];
if (callback === timer._callback) {
if ((timer === element.currentTimer) && (!element.currentTimerSalvaged)) {
element.currentTimerSalvaged = true;
}
timers.splice(i, 1);
CallbackTimer.put(timer);
// update timerIndex in case we are in tick;, looping over the actions
if (element.timerIndex >= i) {
element.timerIndex--;
}
if (timers.length === 0) {
if (self._currentTarget === element) {
self._currentTargetSalvaged = true;
} else {
self._removeHashElement(element);
}
}
return;
}
}
}
}
/**
* @en Unschedules the update callback for a given target.
* @zh 取消指定对象的 update 定时器。
* @param {Object} target The target to be unscheduled.
*/
public unscheduleUpdate (target) {
if (!target) {
return;
}
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
const element = this._hashForUpdates[targetId];
if (element) {
if (this._updateHashLocked) {
element.entry.markedForDeletion = true;
} else {
this._removeUpdateFromHash(element.entry);
}
}
}
/**
* @en
* Unschedules all scheduled callbacks for a given target.
* This also includes the "update" callback.
* @zh 取消指定对象的所有定时器,包括 update 定时器。
* @param {Object} target The target to be unscheduled.
*/
public unscheduleAllForTarget (target) {
// explicit nullptr handling
if (!target){
return;
}
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
// Custom Selectors
const element = this._hashForTimers[targetId];
if (element) {
const timers = element.timers;
if (timers.indexOf(element.currentTimer) > -1 &&
(!element.currentTimerSalvaged)) {
element.currentTimerSalvaged = true;
}
for (let i = 0, l = timers.length; i < l; i++) {
CallbackTimer.put(timers[i]);
}
timers.length = 0;
if (this._currentTarget === element){
this._currentTargetSalvaged = true;
}else{
this._removeHashElement(element);
}
}
// update selector
this.unscheduleUpdate(target);
}
/**
* @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
* 取消所有对象的所有定时器,包括系统定时器。
* 不用调用此函数,除非你确定你在做什么。
*/
public unscheduleAll (){
this.unscheduleAllWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM);
}
/**
* @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 的定时器。
* @param {Number} minPriority The minimum priority of selector to be unscheduled. Which means, all selectors which
* priority is higher than minPriority will be unscheduled.
*/
public unscheduleAllWithMinPriority (minPriority: number){
// Custom Selectors
let i;
let element;
const arr = this._arrayForTimers;
for ( i = arr.length - 1; i >= 0; i--) {
element = arr[i];
this.unscheduleAllForTarget(element.target);
}
// Updates selectors
let entry;
let temp_length = 0;
if (minPriority < 0){
for ( i = 0; i < this._updatesNegList.length; ) {
temp_length = this._updatesNegList.length;
entry = this._updatesNegList[i];
if (entry && entry.priority >= minPriority) {
this.unscheduleUpdate(entry.target);
}
if (temp_length === this._updatesNegList.length) {
i++;
}
}
}
if (minPriority <= 0 ) {
for ( i = 0; i < this._updates0List.length; ) {
temp_length = this._updates0List.length;
entry = this._updates0List[i];
if (entry) {
this.unscheduleUpdate(entry.target);
}
if (temp_length === this._updates0List.length) {
i++;
}
}
}
for ( i = 0; i < this._updatesPosList.length; ) {
temp_length = this._updatesPosList.length;
entry = this._updatesPosList[i];
if ( entry && entry.priority >= minPriority ) {
this.unscheduleUpdate(entry.target);
}
if (temp_length === this._updatesPosList.length) {
i++;
}
}
}
/**
* @en Checks whether a callback for a given target is scheduled.
* @zh 检查指定的回调函数和回调对象组合是否存在定时器。
* @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.
*/
public isScheduled (callback, target){
// key, target
// selector, target
cc.assertID(callback, 1508);
cc.assertID(target, 1509);
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
const element = this._hashForTimers[targetId];
if (!element) {
return false;
}
if (element.timers == null){
return false;
}
else {
const timers = element.timers;
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < timers.length; ++i) {
const timer = timers[i];
if (callback === timer._callback){
return true;
}
}
return false;
}
}
/**
* @en
* Pause all selectors from all targets.
* You should NEVER call this method, unless you know what you are doing.
* @zh
* 暂停所有对象的所有定时器。
* 不要调用这个方法,除非你知道你正在做什么。
*/
public pauseAllTargets () {
return this.pauseAllTargetsWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM);
}
/**
* @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 的定时器。
* @param {Number} minPriority
*/
public pauseAllTargetsWithMinPriority (minPriority: number) {
const idsWithSelectors: any = [];
const self = this;
let element;
const locArrayForTimers = self._arrayForTimers;
let i;
let li;
// Custom Selectors
for (i = 0, li = locArrayForTimers.length; i < li; i++) {
element = locArrayForTimers[i];
if (element) {
element.paused = true;
idsWithSelectors.push(element.target);
}
}
let entry;
if (minPriority < 0) {
for (i = 0; i < this._updatesNegList.length; i++) {
entry = this._updatesNegList[i];
if (entry) {
if (entry.priority >= minPriority) {
entry.paused = true;
idsWithSelectors.push(entry.target);
}
}
}
}
if (minPriority <= 0) {
for (i = 0; i < this._updates0List.length; i++) {
entry = this._updates0List[i];
if (entry) {
entry.paused = true;
idsWithSelectors.push(entry.target);
}
}
}
for (i = 0; i < this._updatesPosList.length; i++) {
entry = this._updatesPosList[i];
if (entry) {
if (entry.priority >= minPriority) {
entry.paused = true;
idsWithSelectors.push(entry.target);
}
}
}
return idsWithSelectors;
}
/**
* @en
* Resume selectors on a set of targets.
* This can be useful for undoing a call to pauseAllCallbacks.
* @zh
* 恢复指定数组中所有对象的定时器。
* 这个函数是 pauseAllCallbacks 的逆操作。
* @param {Array} targetsToResume
*/
public resumeTargets (targetsToResume) {
if (!targetsToResume) {
return;
}
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < targetsToResume.length; i++) {
this.resumeTarget(targetsToResume[i]);
}
}
/**
* @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
* 暂停指定对象的定时器。
* 指定对象的所有定时器都会被暂停。
* 如果指定的对象没有定时器,什么也不会发生。
* @param {Object} target
*/
public pauseTarget (target) {
cc.assertID(target, 1503);
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
// customer selectors
const self = this;
const element = self._hashForTimers[targetId];
if (element) {
element.paused = true;
}
// update callback
const elementUpdate = self._hashForUpdates[targetId];
if (elementUpdate) {
elementUpdate.entry.paused = true;
}
}
/**
* @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
* 恢复指定对象的所有定时器。
* 指定对象的所有定时器将继续工作。
* 如果指定的对象没有定时器,什么也不会发生。
* @param {Object} target
*/
public resumeTarget (target) {
cc.assertID(target, 1504);
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
// custom selectors
const self = this;
const element = self._hashForTimers[targetId];
if (element) {
element.paused = false;
}
// update callback
const elementUpdate = self._hashForUpdates[targetId];
if (elementUpdate) {
elementUpdate.entry.paused = false;
}
}
/**
* @en Returns whether or not the target is paused.
* @zh 返回指定对象的定时器是否处于暂停状态。
* @param {Object} target
* @return {Boolean}
*/
public isTargetPaused (target) {
cc.assertID(target, 1505);
let targetId = target._id;
if (!targetId) {
if (target.__instanceId) {
cc.warnID(1513);
targetId = target._id = target.__instanceId;
}
else {
cc.errorID(1510);
}
}
// Custom selectors
const element = this._hashForTimers[targetId];
if (element) {
return element.paused;
}
const elementUpdate = this._hashForUpdates[targetId];
if (elementUpdate) {
return elementUpdate.entry.paused;
}
return false;
}
// -----------------------private method----------------------
private _removeHashElement (element) {
delete this._hashForTimers[element.target._id];
const arr = this._arrayForTimers;
for (let i = 0, l = arr.length; i < l; i++) {
if (arr[i] === element) {
arr.splice(i, 1);
break;
}
}
HashTimerEntry.put(element);
}
private _removeUpdateFromHash (entry) {
const targetId = entry.target._id;
const self = this;
const element = self._hashForUpdates[targetId];
if (element) {
// Remove list entry from list
const list = element.list;
const listEntry = element.entry;
for (let i = 0, l = list.length; i < l; i++) {
if (list[i] === listEntry) {
list.splice(i, 1);
break;
}
}
delete self._hashForUpdates[targetId];
ListEntry.put(listEntry);
HashUpdateEntry.put(element);
}
}
private _priorityIn (ppList, listElement, priority) {
for (let i = 0; i < ppList.length; i++){
if (priority < ppList[i].priority) {
ppList.splice(i, 0, listElement);
return;
}
}
ppList.push(listElement);
}
private _appendIn (ppList, listElement) {
ppList.push(listElement);
}
}
cc.Scheduler = Scheduler;
export default Scheduler;