CCScheduler.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * Minimum priority level for user scheduling.
  24. * @constant
  25. * @type Number
  26. */
  27. cc.PRIORITY_NON_SYSTEM = cc.PRIORITY_SYSTEM + 1;
  28. //data structures
  29. /**
  30. * A list double-linked list used for "updates with priority"
  31. * @Class
  32. * @name cc.ListEntry
  33. * @param {cc.ListEntry} prev
  34. * @param {cc.ListEntry} next
  35. * @param {cc.Class} target not retained (retained by hashUpdateEntry)
  36. * @param {Number} priority
  37. * @param {Boolean} paused
  38. * @param {Boolean} markedForDeletion selector will no longer be called and entry will be removed at end of the next tick
  39. */
  40. cc.ListEntry = function (prev, next, target, priority, paused, markedForDeletion) {
  41. this.prev = prev;
  42. this.next = next;
  43. this.target = target;
  44. this.priority = priority;
  45. this.paused = paused;
  46. this.markedForDeletion = markedForDeletion;
  47. };
  48. /**
  49. * A update entry list
  50. * @Class
  51. * @name cc.HashUpdateEntry
  52. * @param {cc.ListEntry} list Which list does it belong to ?
  53. * @param {cc.ListEntry} entry entry in the list
  54. * @param {cc.Class} target hash key (retained)
  55. * @param {Array} hh
  56. */
  57. cc.HashUpdateEntry = function (list, entry, target, hh) {
  58. this.list = list;
  59. this.entry = entry;
  60. this.target = target;
  61. this.hh = hh;
  62. };
  63. //
  64. /**
  65. * Hash Element used for "selectors with interval"
  66. * @Class
  67. * @param {Array} timers
  68. * @param {cc.Class} target hash key (retained)
  69. * @param {Number} timerIndex
  70. * @param {cc.Timer} currentTimer
  71. * @param {Boolean} currentTimerSalvaged
  72. * @param {Boolean} paused
  73. * @param {Array} hh
  74. */
  75. cc.HashTimerEntry = function (timers, target, timerIndex, currentTimer, currentTimerSalvaged, paused, hh) {
  76. var _t = this;
  77. _t.timers = timers;
  78. _t.target = target;
  79. _t.timerIndex = timerIndex;
  80. _t.currentTimer = currentTimer;
  81. _t.currentTimerSalvaged = currentTimerSalvaged;
  82. _t.paused = paused;
  83. _t.hh = hh;
  84. };
  85. /**
  86. * Light weight timer
  87. * @class
  88. * @extends cc.Class
  89. */
  90. cc.Timer = cc.Class.extend(/** @lends cc.Timer# */{
  91. _interval:0.0,
  92. _callback:null,//is called _callback before
  93. _target:null,//target of _callback
  94. _elapsed:0.0,
  95. _runForever:false,
  96. _useDelay:false,
  97. _timesExecuted:0,
  98. _repeat:0, //0 = once, 1 is 2 x executed
  99. _delay:0,
  100. /**
  101. * @return {Number} returns interval of timer
  102. */
  103. getInterval : function(){return this._interval;},
  104. /**
  105. * @param {Number} interval set interval in seconds
  106. */
  107. setInterval : function(interval){this._interval = interval;},
  108. /**
  109. * @return {String|function} returns callback
  110. */
  111. getCallback : function(){return this._callback},
  112. /**
  113. * cc.Timer's Constructor
  114. * Constructor of cc.Timer
  115. * @param {cc.Class} target target
  116. * @param {String|function} callback Selector
  117. * @param {Number} [interval=0] second
  118. * @param {Number} [repeat=cc.REPEAT_FOREVER] repeat times
  119. * @param {Number} [delay=0] delay
  120. */
  121. ctor:function (target, callback, interval, repeat, delay) {
  122. var self = this;
  123. self._target = target;
  124. self._callback = callback;
  125. self._elapsed = -1;
  126. self._interval = interval || 0;
  127. self._delay = delay || 0;
  128. self._useDelay = self._delay > 0;
  129. self._repeat = (repeat == null) ? cc.REPEAT_FOREVER : repeat;
  130. self._runForever = (self._repeat == cc.REPEAT_FOREVER);
  131. },
  132. _doCallback:function(){
  133. var self = this;
  134. if (typeof(self._callback) == "string")
  135. self._target[self._callback](self._elapsed);
  136. else // if (typeof(this._callback) == "function") {
  137. self._callback.call(self._target, self._elapsed);
  138. },
  139. /**
  140. * triggers the timer
  141. * @param {Number} dt delta time
  142. */
  143. update:function (dt) {
  144. var self = this;
  145. if (self._elapsed == -1) {
  146. self._elapsed = 0;
  147. self._timesExecuted = 0;
  148. } else {
  149. var locTarget = self._target, locCallback = self._callback;
  150. self._elapsed += dt;//standard timer usage
  151. if (self._runForever && !self._useDelay) {
  152. if (self._elapsed >= self._interval) {
  153. if (locTarget && locCallback)
  154. self._doCallback();
  155. self._elapsed = 0;
  156. }
  157. } else {
  158. //advanced usage
  159. if (self._useDelay) {
  160. if (self._elapsed >= self._delay) {
  161. if (locTarget && locCallback)
  162. self._doCallback();
  163. self._elapsed = self._elapsed - self._delay;
  164. self._timesExecuted += 1;
  165. self._useDelay = false;
  166. }
  167. } else {
  168. if (self._elapsed >= self._interval) {
  169. if (locTarget && locCallback)
  170. self._doCallback();
  171. self._elapsed = 0;
  172. self._timesExecuted += 1;
  173. }
  174. }
  175. if (self._timesExecuted > self._repeat)
  176. cc.director.getScheduler().unscheduleCallbackForTarget(locTarget, locCallback);
  177. }
  178. }
  179. }
  180. });
  181. /**
  182. * <p>
  183. * Scheduler is responsible of triggering the scheduled callbacks.<br/>
  184. * You should not use NSTimer. Instead use this class.<br/>
  185. * <br/>
  186. * There are 2 different types of callbacks (selectors):<br/>
  187. * - update callback: the 'update' callback will be called every frame. You can customize the priority.<br/>
  188. * - custom callback: A custom callback will be called every frame, or with a custom interval of time<br/>
  189. * <br/>
  190. * The 'custom selectors' should be avoided when possible. It is faster, and consumes less memory to use the 'update callback'. *
  191. * </p>
  192. * @class
  193. * @extends cc.Class
  194. *
  195. * @example
  196. * //register a schedule to scheduler
  197. * cc.director.getScheduler().scheduleSelector(callback, this, interval, !this._isRunning);
  198. */
  199. cc.Scheduler = cc.Class.extend(/** @lends cc.Scheduler# */{
  200. _timeScale:1.0,
  201. _updates : null, //_updates[0] list of priority < 0, _updates[1] list of priority == 0, _updates[2] list of priority > 0,
  202. _hashForUpdates:null, // hash used to fetch quickly the list entries for pause,delete,etc
  203. _arrayForUpdates:null,
  204. _hashForTimers:null, //Used for "selectors with interval"
  205. _arrayForTimes:null,
  206. _currentTarget:null,
  207. _currentTargetSalvaged:false,
  208. _updateHashLocked:false, //If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
  209. ctor:function () {
  210. var self = this;
  211. self._timeScale = 1.0;
  212. self._updates = [[], [], []];
  213. self._hashForUpdates = {};
  214. self._arrayForUpdates = [];
  215. self._hashForTimers = {};
  216. self._arrayForTimers = [];
  217. self._currentTarget = null;
  218. self._currentTargetSalvaged = false;
  219. self._updateHashLocked = false;
  220. },
  221. //-----------------------private method----------------------
  222. _removeHashElement:function (element) {
  223. delete this._hashForTimers[element.target.__instanceId];
  224. cc.arrayRemoveObject(this._arrayForTimers, element);
  225. element.Timer = null;
  226. element.target = null;
  227. element = null;
  228. },
  229. _removeUpdateFromHash:function (entry) {
  230. var self = this, element = self._hashForUpdates[entry.target.__instanceId];
  231. if (element) {
  232. //list entry
  233. cc.arrayRemoveObject(element.list, element.entry);
  234. delete self._hashForUpdates[element.target.__instanceId];
  235. cc.arrayRemoveObject(self._arrayForUpdates, element);
  236. element.entry = null;
  237. //hash entry
  238. element.target = null;
  239. }
  240. },
  241. _priorityIn:function (ppList, target, priority, paused) {
  242. var self = this, listElement = new cc.ListEntry(null, null, target, priority, paused, false);
  243. // empey list ?
  244. if (!ppList) {
  245. ppList = [];
  246. ppList.push(listElement);
  247. } else {
  248. var index2Insert = ppList.length - 1;
  249. for(var i = 0; i <= index2Insert; i++){
  250. if (priority < ppList[i].priority) {
  251. index2Insert = i;
  252. break;
  253. }
  254. }
  255. ppList.splice(i, 0, listElement);
  256. }
  257. //update hash entry for quick access
  258. var hashElement = new cc.HashUpdateEntry(ppList, listElement, target, null);
  259. self._arrayForUpdates.push(hashElement);
  260. self._hashForUpdates[target.__instanceId] = hashElement;
  261. return ppList;
  262. },
  263. _appendIn:function (ppList, target, paused) {
  264. var self = this, listElement = new cc.ListEntry(null, null, target, 0, paused, false);
  265. ppList.push(listElement);
  266. //update hash entry for quicker access
  267. var hashElement = new cc.HashUpdateEntry(ppList, listElement, target, null);
  268. self._arrayForUpdates.push(hashElement);
  269. self._hashForUpdates[target.__instanceId] = hashElement;
  270. },
  271. //-----------------------public method-------------------------
  272. /**
  273. * <p>
  274. * Modifies the time of all scheduled callbacks.<br/>
  275. * You can use this property to create a 'slow motion' or 'fast forward' effect.<br/>
  276. * Default is 1.0. To create a 'slow motion' effect, use values below 1.0.<br/>
  277. * To create a 'fast forward' effect, use values higher than 1.0.<br/>
  278. * @warning It will affect EVERY scheduled selector / action.
  279. * </p>
  280. * @param {Number} timeScale
  281. */
  282. setTimeScale:function (timeScale) {
  283. this._timeScale = timeScale;
  284. },
  285. /**
  286. * Returns time scale of scheduler
  287. * @return {Number}
  288. */
  289. getTimeScale:function () {
  290. return this._timeScale;
  291. },
  292. /**
  293. * 'update' the scheduler. (You should NEVER call this method, unless you know what you are doing.)
  294. * @param {Number} dt delta time
  295. */
  296. update:function (dt) {
  297. var self = this;
  298. var locUpdates = self._updates, locArrayForTimers = self._arrayForTimers;
  299. var tmpEntry, elt, i, li;
  300. self._updateHashLocked = true;
  301. if (this._timeScale != 1.0) {
  302. dt *= this._timeScale;
  303. }
  304. for(i = 0, li = locUpdates.length; i < li && i >= 0; i++){
  305. var update = self._updates[i];
  306. for(var j = 0, lj = update.length; j < lj; j++){
  307. tmpEntry = update[j];
  308. if ((!tmpEntry.paused) && (!tmpEntry.markedForDeletion)) tmpEntry.target.update(dt);
  309. }
  310. }
  311. //Interate all over the custom callbacks
  312. for(i = 0, li = locArrayForTimers.length; i < li; i++){
  313. elt = locArrayForTimers[i];
  314. if(!elt) break;
  315. self._currentTarget = elt;
  316. self._currentTargetSalvaged = false;
  317. if (!elt.paused) {
  318. // The 'timers' array may change while inside this loop
  319. for (elt.timerIndex = 0; elt.timerIndex < elt.timers.length; elt.timerIndex++) {
  320. elt.currentTimer = elt.timers[elt.timerIndex];
  321. elt.currentTimerSalvaged = false;
  322. elt.currentTimer.update(dt);
  323. elt.currentTimer = null;
  324. }
  325. }
  326. if ((self._currentTargetSalvaged) && (elt.timers.length == 0)){
  327. self._removeHashElement(elt);
  328. i--;
  329. }
  330. }
  331. for(i = 0, li = locUpdates.length; i < li; i++){
  332. var update = self._updates[i];
  333. for(var j = 0, lj = update.length; j < lj; ){
  334. tmpEntry = update[j];
  335. if(!tmpEntry) break;
  336. if (tmpEntry.markedForDeletion) self._removeUpdateFromHash(tmpEntry);
  337. else j++;
  338. }
  339. }
  340. self._updateHashLocked = false;
  341. self._currentTarget = null;
  342. },
  343. /**
  344. * <p>
  345. * The scheduled method will be called every 'interval' seconds.</br>
  346. * If paused is YES, then it won't be called until it is resumed.<br/>
  347. * If 'interval' is 0, it will be called every frame, but if so, it recommended to use 'scheduleUpdateForTarget:' instead.<br/>
  348. * If the callback function is already scheduled, then only the interval parameter will be updated without re-scheduling it again.<br/>
  349. * repeat let the action be repeated repeat + 1 times, use cc.REPEAT_FOREVER to let the action run continuously<br/>
  350. * delay is the amount of time the action will wait before it'll start<br/>
  351. * </p>
  352. * @param {cc.Class} target
  353. * @param {function} callback_fn
  354. * @param {Number} interval
  355. * @param {Number} repeat
  356. * @param {Number} delay
  357. * @param {Boolean} paused
  358. * @example
  359. * //register a schedule to scheduler
  360. * cc.director.getScheduler().scheduleCallbackForTarget(this, function, interval, repeat, delay, !this._isRunning );
  361. */
  362. scheduleCallbackForTarget:function (target, callback_fn, interval, repeat, delay, paused) {
  363. cc.assert(callback_fn, cc._LogInfos.Scheduler_scheduleCallbackForTarget_2);
  364. cc.assert(target, cc._LogInfos.Scheduler_scheduleCallbackForTarget_3);
  365. // default arguments
  366. interval = interval || 0;
  367. repeat = (repeat == null) ? cc.REPEAT_FOREVER : repeat;
  368. delay = delay || 0;
  369. paused = paused || false;
  370. var self = this, timer;
  371. var element = self._hashForTimers[target.__instanceId];
  372. if (!element) {
  373. // Is this the 1st element ? Then set the pause level to all the callback_fns of this target
  374. element = new cc.HashTimerEntry(null, target, 0, null, null, paused, null);
  375. self._arrayForTimers.push(element);
  376. self._hashForTimers[target.__instanceId] = element;
  377. }
  378. if (element.timers == null) {
  379. element.timers = [];
  380. } else {
  381. for (var i = 0; i < element.timers.length; i++) {
  382. timer = element.timers[i];
  383. if (callback_fn == timer._callback) {
  384. cc.log(cc._LogInfos.Scheduler_scheduleCallbackForTarget, timer.getInterval().toFixed(4), interval.toFixed(4));
  385. timer._interval = interval;
  386. return;
  387. }
  388. }
  389. }
  390. timer = new cc.Timer(target, callback_fn, interval, repeat, delay);
  391. element.timers.push(timer);
  392. },
  393. /**
  394. * <p>
  395. * Schedules the 'update' callback_fn for a given target with a given priority.<br/>
  396. * The 'update' callback_fn will be called every frame.<br/>
  397. * The lower the priority, the earlier it is called.
  398. * </p>
  399. * @param {cc.Class} target
  400. * @param {Number} priority
  401. * @param {Boolean} paused
  402. * @example
  403. * //register this object to scheduler
  404. * cc.director.getScheduler().scheduleUpdateForTarget(this, priority, !this._isRunning );
  405. */
  406. scheduleUpdateForTarget:function (target, priority, paused) {
  407. if(target === null)
  408. return;
  409. var self = this, locUpdates = self._updates;
  410. var hashElement = self._hashForUpdates[target.__instanceId];
  411. if (hashElement) {
  412. // TODO: check if priority has changed!
  413. hashElement.entry.markedForDeletion = false;
  414. return;
  415. }
  416. // most of the updates are going to be 0, that's way there
  417. // is an special list for updates with priority 0
  418. if (priority == 0) {
  419. self._appendIn(locUpdates[1], target, paused);
  420. } else if (priority < 0) {
  421. locUpdates[0] = self._priorityIn(locUpdates[0], target, priority, paused);
  422. } else {
  423. // priority > 0
  424. locUpdates[2] = self._priorityIn(locUpdates[2], target, priority, paused);
  425. }
  426. },
  427. /**
  428. * <p>
  429. * Unschedule a callback function for a given target.<br/>
  430. * If you want to unschedule the "update", use unscheudleUpdateForTarget.
  431. * </p>
  432. * @param {cc.Class} target
  433. * @param {function} callback_fn
  434. * @example
  435. * //unschedule a callback of target
  436. * cc.director.getScheduler().unscheduleCallbackForTarget(function, this);
  437. */
  438. unscheduleCallbackForTarget:function (target, callback_fn) {
  439. // explicity handle nil arguments when removing an object
  440. if ((target == null) || (callback_fn == null)) {
  441. return;
  442. }
  443. var self = this, element = self._hashForTimers[target.__instanceId];
  444. if (element) {
  445. var timers = element.timers;
  446. for(var i = 0, li = timers.length; i < li; i++){
  447. var timer = timers[i];
  448. if (callback_fn == timer._callback) {
  449. if ((timer == element.currentTimer) && (!element.currentTimerSalvaged)) {
  450. element.currentTimerSalvaged = true;
  451. }
  452. timers.splice(i, 1)
  453. //update timerIndex in case we are in tick;, looping over the actions
  454. if (element.timerIndex >= i) {
  455. element.timerIndex--;
  456. }
  457. if (timers.length == 0) {
  458. if (self._currentTarget == element) {
  459. self._currentTargetSalvaged = true;
  460. } else {
  461. self._removeHashElement(element);
  462. }
  463. }
  464. return;
  465. }
  466. }
  467. }
  468. },
  469. /**
  470. * Unschedules the update callback function for a given target
  471. * @param {cc.Class} target
  472. * @example
  473. * //unschedules the "update" method.
  474. * cc.director.getScheduler().unscheduleUpdateForTarget(this);
  475. */
  476. unscheduleUpdateForTarget:function (target) {
  477. if (target == null) {
  478. return;
  479. }
  480. var self = this, element = self._hashForUpdates[target.__instanceId];
  481. if (element != null) {
  482. if (self._updateHashLocked) {
  483. element.entry.markedForDeletion = true;
  484. } else {
  485. self._removeUpdateFromHash(element.entry);
  486. }
  487. }
  488. },
  489. /**
  490. * Unschedules all function callbacks for a given target. This also includes the "update" callback function.
  491. * @param {cc.Class} target
  492. */
  493. unscheduleAllCallbacksForTarget:function (target) {
  494. //explicit NULL handling
  495. if (target == null) {
  496. return;
  497. }
  498. var self = this, element = self._hashForTimers[target.__instanceId];
  499. if (element) {
  500. var timers = element.timers;
  501. if ((!element.currentTimerSalvaged) && (timers.indexOf(element.currentTimer) >= 0)) {
  502. element.currentTimerSalvaged = true;
  503. }
  504. timers.length = 0;
  505. if (self._currentTarget == element) {
  506. self._currentTargetSalvaged = true;
  507. } else {
  508. self._removeHashElement(element);
  509. }
  510. }
  511. // update callback
  512. self.unscheduleUpdateForTarget(target);
  513. },
  514. /**
  515. * <p>
  516. * Unschedules all function callbacks from all targets. <br/>
  517. * You should NEVER call this method, unless you know what you are doing.
  518. * </p>
  519. */
  520. unscheduleAllCallbacks:function () {
  521. this.unscheduleAllCallbacksWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM);
  522. },
  523. /**
  524. * <p>
  525. * Unschedules all function callbacks from all targets with a minimum priority.<br/>
  526. * You should only call this with kCCPriorityNonSystemMin or higher.
  527. * </p>
  528. * @param {Number} minPriority
  529. */
  530. unscheduleAllCallbacksWithMinPriority:function (minPriority) {
  531. // Custom Selectors
  532. var self = this, locArrayForTimers = self._arrayForTimers, locUpdates = self._updates;
  533. for(var i = 0, li = locArrayForTimers.length; i < li; i++){
  534. // element may be removed in unscheduleAllCallbacksForTarget
  535. self.unscheduleAllCallbacksForTarget(locArrayForTimers[i].target);
  536. }
  537. for(var i = 2; i >= 0; i--){
  538. if((i == 1 && minPriority > 0) || (i == 0 && minPriority >= 0)) continue;
  539. var updates = locUpdates[i];
  540. for(var j = 0, lj = updates.length; j < lj; j++){
  541. self.unscheduleUpdateForTarget(updates[j].target);
  542. }
  543. }
  544. },
  545. /**
  546. * <p>
  547. * Pause all selectors from all targets.<br/>
  548. * You should NEVER call this method, unless you know what you are doing.
  549. * </p>
  550. */
  551. pauseAllTargets:function () {
  552. return this.pauseAllTargetsWithMinPriority(cc.Scheduler.PRIORITY_SYSTEM);
  553. },
  554. /**
  555. * Pause all selectors from all targets with a minimum priority. <br/>
  556. * You should only call this with kCCPriorityNonSystemMin or higher.
  557. * @param {Number} minPriority
  558. */
  559. pauseAllTargetsWithMinPriority:function (minPriority) {
  560. var idsWithSelectors = [];
  561. var self = this, element, locArrayForTimers = self._arrayForTimers, locUpdates = self._updates;
  562. // Custom Selectors
  563. for(var i = 0, li = locArrayForTimers.length; i < li; i++){
  564. element = locArrayForTimers[i];
  565. if (element) {
  566. element.paused = true;
  567. idsWithSelectors.push(element.target);
  568. }
  569. }
  570. for(var i = 0, li = locUpdates.length; i < li; i++){
  571. var updates = locUpdates[i];
  572. for(var j = 0, lj = updates.length; j < lj; j++){
  573. element = updates[j];
  574. if (element) {
  575. element.paused = true;
  576. idsWithSelectors.push(element.target);
  577. }
  578. }
  579. }
  580. return idsWithSelectors;
  581. },
  582. /**
  583. * Resume selectors on a set of targets.<br/>
  584. * This can be useful for undoing a call to pauseAllCallbacks.
  585. * @param {Array} targetsToResume
  586. */
  587. resumeTargets:function (targetsToResume) {
  588. if (!targetsToResume)
  589. return;
  590. for (var i = 0; i < targetsToResume.length; i++) {
  591. this.resumeTarget(targetsToResume[i]);
  592. }
  593. },
  594. /**
  595. * <p>
  596. * Pauses the target.<br/>
  597. * All scheduled selectors/update for a given target won't be 'ticked' until the target is resumed.<br/>
  598. * If the target is not present, nothing happens.
  599. * </p>
  600. * @param {cc.Class} target
  601. */
  602. pauseTarget:function (target) {
  603. cc.assert(target, cc._LogInfos.Scheduler_pauseTarget);
  604. //customer selectors
  605. var self = this, element = self._hashForTimers[target.__instanceId];
  606. if (element) {
  607. element.paused = true;
  608. }
  609. //update callback
  610. var elementUpdate = self._hashForUpdates[target.__instanceId];
  611. if (elementUpdate) {
  612. elementUpdate.entry.paused = true;
  613. }
  614. },
  615. /**
  616. * Resumes the target.<br/>
  617. * The 'target' will be unpaused, so all schedule selectors/update will be 'ticked' again.<br/>
  618. * If the target is not present, nothing happens.
  619. * @param {cc.Class} target
  620. */
  621. resumeTarget:function (target) {
  622. cc.assert(target, cc._LogInfos.Scheduler_resumeTarget);
  623. // custom selectors
  624. var self = this, element = self._hashForTimers[target.__instanceId];
  625. if (element) {
  626. element.paused = false;
  627. }
  628. //update callback
  629. var elementUpdate = self._hashForUpdates[target.__instanceId];
  630. if (elementUpdate) {
  631. elementUpdate.entry.paused = false;
  632. }
  633. },
  634. /**
  635. * Returns whether or not the target is paused
  636. * @param {cc.Class} target
  637. * @return {Boolean}
  638. */
  639. isTargetPaused:function (target) {
  640. cc.assert(target, cc._LogInfos.Scheduler_isTargetPaused);
  641. // Custom selectors
  642. var element = this._hashForTimers[target.__instanceId];
  643. if (element) {
  644. return element.paused;
  645. }
  646. return false;
  647. }
  648. });
  649. /**
  650. * Priority level reserved for system services.
  651. * @constant
  652. * @type Number
  653. */
  654. cc.Scheduler.PRIORITY_SYSTEM = (-2147483647 - 1);