CCActionInterval.js 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. * <p> An interval action is an action that takes place within a certain period of time. <br/>
  24. * It has an start time, and a finish time. The finish time is the parameter<br/>
  25. * duration plus the start time.</p>
  26. *
  27. * <p>These CCActionInterval actions have some interesting properties, like:<br/>
  28. * - They can run normally (default) <br/>
  29. * - They can run reversed with the reverse method <br/>
  30. * - They can run with the time altered with the Accelerate, AccelDeccel and Speed actions. </p>
  31. *
  32. * <p>For example, you can simulate a Ping Pong effect running the action normally and<br/>
  33. * then running it again in Reverse mode. </p>
  34. *
  35. * @class
  36. * @extends cc.FiniteTimeAction
  37. * @Example
  38. * // example
  39. * var pingPongAction = cc.Sequence.create(action, action.reverse());
  40. */
  41. cc.ActionInterval = cc.FiniteTimeAction.extend(/** @lends cc.ActionInterval# */{
  42. _elapsed:0,
  43. _firstTick:false,
  44. ctor:function () {
  45. cc.FiniteTimeAction.prototype.ctor.call(this);
  46. this._elapsed = 0;
  47. this._firstTick = false;
  48. },
  49. /** how many seconds had elapsed since the actions started to run.
  50. * @return {Number}
  51. */
  52. getElapsed:function () {
  53. return this._elapsed;
  54. },
  55. /** initializes the action
  56. * @param {Number} d duration in seconds
  57. * @return {Boolean}
  58. */
  59. initWithDuration:function (d) {
  60. this._duration = (d === 0) ? cc.FLT_EPSILON : d;
  61. // prevent division by 0
  62. // This comparison could be in step:, but it might decrease the performance
  63. // by 3% in heavy based action games.
  64. this._elapsed = 0;
  65. this._firstTick = true;
  66. return true;
  67. },
  68. /** returns true if the action has finished
  69. * @return {Boolean}
  70. */
  71. isDone:function () {
  72. return (this._elapsed >= this._duration);
  73. },
  74. /**
  75. * returns a new clone of the action
  76. * @returns {cc.ActionInterval}
  77. */
  78. clone:function () {
  79. var action = new cc.ActionInterval();
  80. action.initWithDuration(this._duration);
  81. return action;
  82. },
  83. /**
  84. * @param {Number} dt delta time in seconds
  85. */
  86. step:function (dt) {
  87. if (this._firstTick) {
  88. this._firstTick = false;
  89. this._elapsed = 0;
  90. } else
  91. this._elapsed += dt;
  92. //this.update((1 > (this._elapsed / this._duration)) ? this._elapsed / this._duration : 1);
  93. //this.update(Math.max(0, Math.min(1, this._elapsed / Math.max(this._duration, cc.FLT_EPSILON))));
  94. var t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
  95. t = (1 > t ? t : 1);
  96. this.update(t > 0 ? t : 0);
  97. },
  98. /**
  99. * @param {cc.Node} target
  100. */
  101. startWithTarget:function (target) {
  102. cc.Action.prototype.startWithTarget.call(this, target);
  103. this._elapsed = 0;
  104. this._firstTick = true;
  105. },
  106. /**
  107. * @return {Null}
  108. */
  109. reverse:function () {
  110. cc.log("cc.IntervalAction: reverse not implemented.");
  111. return null;
  112. },
  113. /**
  114. * @param {Number} amp
  115. */
  116. setAmplitudeRate:function (amp) {
  117. // Abstract class needs implementation
  118. cc.log("cc.ActionInterval.setAmplitudeRate(): it should be overridden in subclass.");
  119. },
  120. /**
  121. * @return {Number}
  122. */
  123. getAmplitudeRate:function () {
  124. // Abstract class needs implementation
  125. cc.log("cc.ActionInterval.getAmplitudeRate(): it should be overridden in subclass.");
  126. }
  127. });
  128. /**
  129. * @param {Number} d duration in seconds
  130. * @return {cc.ActionInterval}
  131. * @example
  132. * // example
  133. * var actionInterval = cc.ActionInterval.create(3);
  134. */
  135. cc.ActionInterval.create = function (d) {
  136. var action = new cc.ActionInterval();
  137. action.initWithDuration(d);
  138. return action;
  139. };
  140. /** Runs actions sequentially, one after another
  141. * @class
  142. * @extends cc.ActionInterval
  143. */
  144. cc.Sequence = cc.ActionInterval.extend(/** @lends cc.Sequence# */{
  145. _actions:null,
  146. _split:null,
  147. _last:0,
  148. /**
  149. * Constructor
  150. */
  151. ctor:function () {
  152. cc.ActionInterval.prototype.ctor.call(this);
  153. this._actions = [];
  154. this._split = null;
  155. this._last = 0;
  156. },
  157. /** initializes the action <br/>
  158. * @param {cc.FiniteTimeAction} actionOne
  159. * @param {cc.FiniteTimeAction} actionTwo
  160. * @return {Boolean}
  161. */
  162. initWithTwoActions:function (actionOne, actionTwo) {
  163. if(!actionOne || !actionTwo)
  164. throw "cc.Sequence.initWithTwoActions(): arguments must all be non nil";
  165. var d = actionOne.getDuration() + actionTwo.getDuration();
  166. this.initWithDuration(d);
  167. this._actions[0] = actionOne;
  168. this._actions[1] = actionTwo;
  169. return true;
  170. },
  171. /**
  172. * returns a new clone of the action
  173. * @returns {cc.Sequence}
  174. */
  175. clone:function () {
  176. var action = new cc.Sequence();
  177. action.initWithTwoActions(this._actions[0].clone(), this._actions[1].clone());
  178. return action;
  179. },
  180. /**
  181. * @param {cc.Node} target
  182. */
  183. startWithTarget:function (target) {
  184. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  185. this._split = this._actions[0].getDuration() / this._duration;
  186. this._last = -1;
  187. },
  188. /**
  189. * stop the action
  190. */
  191. stop:function () {
  192. // Issue #1305
  193. if (this._last !== -1)
  194. this._actions[this._last].stop();
  195. cc.Action.prototype.stop.call(this);
  196. },
  197. /**
  198. * @param {Number} time time in seconds
  199. */
  200. update:function (time) {
  201. var new_t, found = 0;
  202. var locSplit = this._split, locActions = this._actions, locLast = this._last;
  203. if (time < locSplit) {
  204. // action[0]
  205. new_t = (locSplit !== 0) ? time / locSplit : 1;
  206. if (found === 0 && locLast === 1) {
  207. // Reverse mode ?
  208. // XXX: Bug. this case doesn't contemplate when _last==-1, found=0 and in "reverse mode"
  209. // since it will require a hack to know if an action is on reverse mode or not.
  210. // "step" should be overriden, and the "reverseMode" value propagated to inner Sequences.
  211. locActions[1].update(0);
  212. locActions[1].stop();
  213. }
  214. } else {
  215. // action[1]
  216. found = 1;
  217. new_t = (locSplit === 1) ? 1 : (time - locSplit) / (1 - locSplit);
  218. if (locLast === -1) {
  219. // action[0] was skipped, execute it.
  220. locActions[0].startWithTarget(this._target);
  221. locActions[0].update(1);
  222. locActions[0].stop();
  223. }
  224. if (!locLast) {
  225. // switching to action 1. stop action 0.
  226. locActions[0].update(1);
  227. locActions[0].stop();
  228. }
  229. }
  230. // Last action found and it is done.
  231. if (locLast === found && locActions[found].isDone())
  232. return;
  233. // Last action found and it is done
  234. if (locLast !== found)
  235. locActions[found].startWithTarget(this._target);
  236. locActions[found].update(new_t);
  237. this._last = found;
  238. },
  239. /**
  240. * @return {cc.ActionInterval}
  241. */
  242. reverse:function () {
  243. return cc.Sequence._actionOneTwo(this._actions[1].reverse(), this._actions[0].reverse());
  244. },
  245. /**
  246. * to copy object with deep copy.
  247. * @return {object}
  248. */
  249. copy:function () {
  250. return this.clone();
  251. }
  252. });
  253. /** helper constructor to create an array of sequenceable actions
  254. * @param {Array|cc.FiniteTimeAction} tempArray
  255. * @return {cc.Sequence}
  256. * @example
  257. * // example
  258. * // create sequence with actions
  259. * var seq = cc.Sequence.create(act1, act2);
  260. *
  261. * // create sequence with array
  262. * var seq = cc.Sequence.create(actArray);
  263. */
  264. cc.Sequence.create = function (/*Multiple Arguments*/tempArray) {
  265. var paraArray = (tempArray instanceof Array) ? tempArray : arguments;
  266. if ((paraArray.length > 0) && (paraArray[paraArray.length - 1] == null))
  267. cc.log("parameters should not be ending with null in Javascript");
  268. var prev = paraArray[0];
  269. for (var i = 1; i < paraArray.length; i++) {
  270. if (paraArray[i])
  271. prev = cc.Sequence._actionOneTwo(prev, paraArray[i]);
  272. }
  273. return prev;
  274. };
  275. /** creates the action
  276. * @param {cc.FiniteTimeAction} actionOne
  277. * @param {cc.FiniteTimeAction} actionTwo
  278. * @return {cc.Sequence}
  279. * @private
  280. */
  281. cc.Sequence._actionOneTwo = function (actionOne, actionTwo) {
  282. var sequence = new cc.Sequence();
  283. sequence.initWithTwoActions(actionOne, actionTwo);
  284. return sequence;
  285. };
  286. /** Repeats an action a number of times.
  287. * To repeat an action forever use the CCRepeatForever action.
  288. * @class
  289. * @extends cc.ActionInterval
  290. */
  291. cc.Repeat = cc.ActionInterval.extend(/** @lends cc.Repeat# */{
  292. _times:0,
  293. _total:0,
  294. _nextDt:0,
  295. _actionInstant:false,
  296. _innerAction:null, //CCFiniteTimeAction
  297. ctor:function () {
  298. cc.ActionInterval.prototype.ctor.call(this);
  299. this._times = 0;
  300. this._total = 0;
  301. this._nextDt = 0;
  302. this._actionInstant = false;
  303. this._innerAction = null;
  304. },
  305. /**
  306. * @param {cc.FiniteTimeAction} action
  307. * @param {Number} times
  308. * @return {Boolean}
  309. */
  310. initWithAction:function (action, times) {
  311. var duration = action.getDuration() * times;
  312. if (this.initWithDuration(duration)) {
  313. this._times = times;
  314. this._innerAction = action;
  315. if (action instanceof cc.ActionInstant)
  316. this._times -= 1;
  317. this._total = 0;
  318. return true;
  319. }
  320. return false;
  321. },
  322. /**
  323. * returns a new clone of the action
  324. * @returns {cc.Repeat}
  325. */
  326. clone:function () {
  327. var action = new cc.Repeat();
  328. action.initWithAction(this._innerAction.clone(), this._times);
  329. return action;
  330. },
  331. /**
  332. * @param {cc.Node} target
  333. */
  334. startWithTarget:function (target) {
  335. this._total = 0;
  336. this._nextDt = this._innerAction.getDuration() / this._duration;
  337. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  338. this._innerAction.startWithTarget(target);
  339. },
  340. /**
  341. * stop the action
  342. */
  343. stop:function () {
  344. this._innerAction.stop();
  345. cc.Action.prototype.stop.call(this);
  346. },
  347. /**
  348. * @param {Number} time time in seconds
  349. */
  350. update:function (time) {
  351. var locInnerAction = this._innerAction;
  352. var locDuration = this._duration;
  353. var locTimes = this._times;
  354. var locNextDt = this._nextDt;
  355. if (time >= locNextDt) {
  356. while (time > locNextDt && this._total < locTimes) {
  357. locInnerAction.update(1);
  358. this._total++;
  359. locInnerAction.stop();
  360. locInnerAction.startWithTarget(this._target);
  361. locNextDt += locInnerAction.getDuration() / locDuration;
  362. this._nextDt = locNextDt;
  363. }
  364. // fix for issue #1288, incorrect end value of repeat
  365. if (time >= 1.0 && this._total < locTimes)
  366. this._total++;
  367. // don't set a instantaction back or update it, it has no use because it has no duration
  368. if (this._actionInstant) {
  369. if (this._total == locTimes) {
  370. locInnerAction.update(1);
  371. locInnerAction.stop();
  372. } else {
  373. // issue #390 prevent jerk, use right update
  374. locInnerAction.update(time - (locNextDt - locInnerAction.getDuration() / locDuration));
  375. }
  376. }
  377. } else {
  378. locInnerAction.update((time * locTimes) % 1.0);
  379. }
  380. },
  381. /**
  382. * @return {Boolean}
  383. */
  384. isDone:function () {
  385. return this._total == this._times;
  386. },
  387. /**
  388. * @return {cc.ActionInterval}
  389. */
  390. reverse:function () {
  391. return cc.Repeat.create(this._innerAction.reverse(), this._times);
  392. },
  393. /**
  394. * @param {cc.FiniteTimeAction} action
  395. */
  396. setInnerAction:function (action) {
  397. if (this._innerAction != action) {
  398. this._innerAction = action;
  399. }
  400. },
  401. /**
  402. * @return {cc.FiniteTimeAction}
  403. */
  404. getInnerAction:function () {
  405. return this._innerAction;
  406. }
  407. });
  408. /** creates a CCRepeat action. Times is an unsigned integer between 1 and pow(2,30)
  409. * @param {cc.FiniteTimeAction} action
  410. * @param {Number} times
  411. * @return {cc.Repeat}
  412. * @example
  413. * // example
  414. * var rep = cc.Repeat.create(cc.Sequence.create(jump2, jump1), 5);
  415. */
  416. cc.Repeat.create = function (action, times) {
  417. var repeat = new cc.Repeat();
  418. repeat.initWithAction(action, times);
  419. return repeat;
  420. };
  421. /** Repeats an action for ever. <br/>
  422. * To repeat the an action for a limited number of times use the Repeat action. <br/>
  423. * @warning This action can't be Sequencable because it is not an IntervalAction
  424. * @class
  425. * @extends cc.ActionInterval
  426. */
  427. cc.RepeatForever = cc.ActionInterval.extend(/** @lends cc.RepeatForever# */{
  428. _innerAction:null, //CCActionInterval
  429. ctor:function () {
  430. cc.ActionInterval.prototype.ctor.call(this);
  431. this._innerAction = null;
  432. },
  433. /**
  434. * @param {cc.ActionInterval} action
  435. * @return {Boolean}
  436. */
  437. initWithAction:function (action) {
  438. if(!action)
  439. throw "cc.RepeatForever.initWithAction(): action must be non null";
  440. this._innerAction = action;
  441. return true;
  442. },
  443. /**
  444. * returns a new clone of the action
  445. * @returns {cc.RepeatForever}
  446. */
  447. clone:function () {
  448. var action = new cc.RepeatForever();
  449. action.initWithAction(this._innerAction.clone());
  450. return action;
  451. },
  452. /**
  453. * @param {cc.Node} target
  454. */
  455. startWithTarget:function (target) {
  456. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  457. this._innerAction.startWithTarget(target);
  458. },
  459. /**
  460. * @param dt delta time in seconds
  461. */
  462. step:function (dt) {
  463. var locInnerAction = this._innerAction;
  464. locInnerAction.step(dt);
  465. if (locInnerAction.isDone()) {
  466. //var diff = locInnerAction.getElapsed() - locInnerAction.getDuration();
  467. locInnerAction.startWithTarget(this._target);
  468. // to prevent jerk. issue #390 ,1247
  469. //this._innerAction.step(0);
  470. //this._innerAction.step(diff);
  471. locInnerAction.step(locInnerAction.getElapsed() - locInnerAction.getDuration());
  472. }
  473. },
  474. /**
  475. * @return {Boolean}
  476. */
  477. isDone:function () {
  478. return false;
  479. },
  480. /**
  481. * @return {cc.ActionInterval}
  482. */
  483. reverse:function () {
  484. return (cc.RepeatForever.create(this._innerAction.reverse()));
  485. },
  486. /**
  487. *
  488. * @param {cc.ActionInterval} action
  489. */
  490. setInnerAction:function (action) {
  491. if (this._innerAction != action) {
  492. this._innerAction = action;
  493. }
  494. },
  495. /**
  496. * @return {cc.ActionInterval}
  497. */
  498. getInnerAction:function () {
  499. return this._innerAction;
  500. }
  501. });
  502. /**
  503. * Repeat the acton forever
  504. * @param action
  505. * @return {cc.RepeatForever}
  506. * @example
  507. * // example
  508. * var repeat = cc.RepeatForever.create(cc.RotateBy.create(1.0, 360));
  509. */
  510. cc.RepeatForever.create = function (action) {
  511. var ret = new cc.RepeatForever();
  512. if (ret && ret.initWithAction(action))
  513. return ret;
  514. return null;
  515. };
  516. /** Spawn a new action immediately
  517. * @class
  518. * @extends cc.ActionInterval
  519. */
  520. cc.Spawn = cc.ActionInterval.extend(/** @lends cc.Spawn# */{
  521. _one:null,
  522. _two:null,
  523. ctor:function () {
  524. cc.ActionInterval.prototype.ctor.call(this);
  525. this._one = null;
  526. this._two = null;
  527. },
  528. /** initializes the Spawn action with the 2 actions to spawn
  529. * @param {cc.FiniteTimeAction} action1
  530. * @param {cc.FiniteTimeAction} action2
  531. * @return {Boolean}
  532. */
  533. initWithTwoActions:function (action1, action2) {
  534. if(!action1 || !action2)
  535. throw "cc.Spawn.initWithTwoActions(): arguments must all be non null" ;
  536. var ret = false;
  537. var d1 = action1.getDuration();
  538. var d2 = action2.getDuration();
  539. if (this.initWithDuration(Math.max(d1, d2))) {
  540. this._one = action1;
  541. this._two = action2;
  542. if (d1 > d2) {
  543. this._two = cc.Sequence._actionOneTwo(action2, cc.DelayTime.create(d1 - d2));
  544. } else if (d1 < d2) {
  545. this._one = cc.Sequence._actionOneTwo(action1, cc.DelayTime.create(d2 - d1));
  546. }
  547. ret = true;
  548. }
  549. return ret;
  550. },
  551. /**
  552. * returns a new clone of the action
  553. * @returns {cc.Spawn}
  554. */
  555. clone:function () {
  556. var action = new cc.Spawn();
  557. action.initWithTwoActions(this._one.clone(), this._two.clone());
  558. return action;
  559. },
  560. /**
  561. * @param {cc.Node} target
  562. */
  563. startWithTarget:function (target) {
  564. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  565. this._one.startWithTarget(target);
  566. this._two.startWithTarget(target);
  567. },
  568. /**
  569. * Stop the action
  570. */
  571. stop:function () {
  572. this._one.stop();
  573. this._two.stop();
  574. cc.Action.prototype.stop.call(this);
  575. },
  576. /**
  577. * @param {Number} time time in seconds
  578. */
  579. update:function (time) {
  580. if (this._one)
  581. this._one.update(time);
  582. if (this._two)
  583. this._two.update(time);
  584. },
  585. /**
  586. * @return {cc.FiniteTimeAction}
  587. */
  588. reverse:function () {
  589. return cc.Spawn._actionOneTwo(this._one.reverse(), this._two.reverse());
  590. }
  591. });
  592. /**
  593. * @param {Array|cc.FiniteTimeAction}tempArray
  594. * @return {cc.FiniteTimeAction}
  595. * @example
  596. * // example
  597. * var action = cc.Spawn.create(cc.JumpBy.create(2, cc.p(300, 0), 50, 4), cc.RotateBy.create(2, 720));
  598. */
  599. cc.Spawn.create = function (/*Multiple Arguments*/tempArray) {
  600. var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
  601. if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null))
  602. cc.log("parameters should not be ending with null in Javascript");
  603. var prev = paramArray[0];
  604. for (var i = 1; i < paramArray.length; i++) {
  605. if (paramArray[i] != null)
  606. prev = this._actionOneTwo(prev, paramArray[i]);
  607. }
  608. return prev;
  609. };
  610. /**
  611. * @param {cc.FiniteTimeAction} action1
  612. * @param {cc.FiniteTimeAction} action2
  613. * @return {cc.Spawn}
  614. * @private
  615. */
  616. cc.Spawn._actionOneTwo = function (action1, action2) {
  617. var pSpawn = new cc.Spawn();
  618. pSpawn.initWithTwoActions(action1, action2);
  619. return pSpawn;
  620. };
  621. /** Rotates a cc.Node object to a certain angle by modifying it's
  622. * rotation attribute. <br/>
  623. * The direction will be decided by the shortest angle.
  624. * @class
  625. * @extends cc.ActionInterval
  626. */
  627. cc.RotateTo = cc.ActionInterval.extend(/** @lends cc.RotateTo# */{
  628. _dstAngleX:0,
  629. _startAngleX:0,
  630. _diffAngleX:0,
  631. _dstAngleY:0,
  632. _startAngleY:0,
  633. _diffAngleY:0,
  634. ctor:function () {
  635. cc.ActionInterval.prototype.ctor.call(this);
  636. this._dstAngleX = 0;
  637. this._startAngleX = 0;
  638. this._diffAngleX = 0;
  639. this._dstAngleY = 0;
  640. this._startAngleY = 0;
  641. this._diffAngleY = 0;
  642. },
  643. /**
  644. * @param {Number} duration
  645. * @param {Number} deltaAngleX
  646. * @param {Number} deltaAngleY
  647. * @return {Boolean}
  648. */
  649. initWithDuration:function (duration, deltaAngleX, deltaAngleY) {
  650. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  651. this._dstAngleX = deltaAngleX || 0;
  652. this._dstAngleY = deltaAngleY || this._dstAngleX;
  653. return true;
  654. }
  655. return false;
  656. },
  657. /**
  658. * returns a new clone of the action
  659. * @returns {cc.RotateTo}
  660. */
  661. clone:function () {
  662. var action = new cc.RotateTo();
  663. action.initWithDuration(this._duration, this._dstAngleX, this._dstAngleY);
  664. return action;
  665. },
  666. /**
  667. * @param {cc.Node} target
  668. */
  669. startWithTarget:function (target) {
  670. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  671. // Calculate X
  672. var locStartAngleX = target.getRotationX() % 360.0;
  673. var locDiffAngleX = this._dstAngleX - locStartAngleX;
  674. if (locDiffAngleX > 180)
  675. locDiffAngleX -= 360;
  676. if (locDiffAngleX < -180)
  677. locDiffAngleX += 360;
  678. this._startAngleX = locStartAngleX;
  679. this._diffAngleX = locDiffAngleX;
  680. // Calculate Y It's duplicated from calculating X since the rotation wrap should be the same
  681. this._startAngleY = target.getRotationY() % 360.0;
  682. var locDiffAngleY = this._dstAngleY - this._startAngleY;
  683. if (locDiffAngleY > 180)
  684. locDiffAngleY -= 360;
  685. if (locDiffAngleY < -180)
  686. locDiffAngleY += 360;
  687. this._diffAngleY = locDiffAngleY
  688. },
  689. /**
  690. * RotateTo reverse not implemented
  691. */
  692. reverse:function () {
  693. cc.log("cc.RotateTo.reverse(): it should be overridden in subclass.");
  694. },
  695. /**
  696. * @param {Number} time time in seconds
  697. */
  698. update:function (time) {
  699. if (this._target) {
  700. this._target.setRotationX(this._startAngleX + this._diffAngleX * time);
  701. this._target.setRotationY(this._startAngleY + this._diffAngleY * time);
  702. }
  703. }
  704. });
  705. /**
  706. * creates the action with separate rotation angles
  707. * @param {Number} duration duration in seconds
  708. * @param {Number} deltaAngleX deltaAngleX in degrees.
  709. * @param {Number} [deltaAngleY=] deltaAngleY in degrees.
  710. * @return {cc.RotateTo}
  711. * @example
  712. * // example
  713. * var rotateTo = cc.RotateTo.create(2, 61.0);
  714. */
  715. cc.RotateTo.create = function (duration, deltaAngleX, deltaAngleY) {
  716. var rotateTo = new cc.RotateTo();
  717. rotateTo.initWithDuration(duration, deltaAngleX, deltaAngleY);
  718. return rotateTo;
  719. };
  720. /** Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
  721. * @class
  722. * @extends cc.ActionInterval
  723. */
  724. cc.RotateBy = cc.ActionInterval.extend(/** @lends cc.RotateBy# */{
  725. _angleX:0,
  726. _startAngleX:0,
  727. _angleY:0,
  728. _startAngleY:0,
  729. ctor:function () {
  730. cc.ActionInterval.prototype.ctor.call(this);
  731. this._angleX = 0;
  732. this._startAngleX = 0;
  733. this._angleY = 0;
  734. this._startAngleY = 0;
  735. },
  736. /**
  737. * @param {Number} duration duration in seconds
  738. * @param {Number} deltaAngleX deltaAngleX in degrees
  739. * @param {Number} [deltaAngleY=] deltaAngleY in degrees
  740. * @return {Boolean}
  741. */
  742. initWithDuration:function (duration, deltaAngleX, deltaAngleY) {
  743. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  744. this._angleX = deltaAngleX || 0;
  745. this._angleY = deltaAngleY || this._angleX;
  746. return true;
  747. }
  748. return false;
  749. },
  750. /**
  751. * returns a new clone of the action
  752. * @returns {cc.RotateBy}
  753. */
  754. clone:function () {
  755. var action = new cc.RotateBy();
  756. action.initWithDuration(this._duration, this._angleX, this._angleY);
  757. return action;
  758. },
  759. /**
  760. * @param {cc.Node} target
  761. */
  762. startWithTarget:function (target) {
  763. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  764. this._startAngleX = target.getRotationX();
  765. this._startAngleY = target.getRotationY();
  766. },
  767. /**
  768. * @param {Number} time
  769. */
  770. update:function (time) {
  771. if (this._target) {
  772. this._target.setRotationX(this._startAngleX + this._angleX * time);
  773. this._target.setRotationY(this._startAngleY + this._angleY * time);
  774. }
  775. },
  776. /**
  777. * @return {cc.ActionInterval}
  778. */
  779. reverse:function () {
  780. return cc.RotateBy.create(this._duration, -this._angleX, -this._angleY);
  781. }
  782. });
  783. /**
  784. * @param {Number} duration druation in seconds
  785. * @param {Number} deltaAngleX deltaAngleX in degrees
  786. * @param {Number} [deltaAngleY=] deltaAngleY in degrees
  787. * @return {cc.RotateBy}
  788. * @example
  789. * // example
  790. * var actionBy = cc.RotateBy.create(2, 360);
  791. */
  792. cc.RotateBy.create = function (duration, deltaAngleX, deltaAngleY) {
  793. var rotateBy = new cc.RotateBy();
  794. rotateBy.initWithDuration(duration, deltaAngleX, deltaAngleY);
  795. return rotateBy;
  796. };
  797. /**
  798. * <p>
  799. * Moves a CCNode object x,y pixels by modifying it's position attribute. <br/>
  800. * x and y are relative to the position of the object. <br/>
  801. * Several CCMoveBy actions can be concurrently called, and the resulting <br/>
  802. * movement will be the sum of individual movements.
  803. * </p>
  804. * @class
  805. * @extends cc.ActionInterval
  806. */
  807. cc.MoveBy = cc.ActionInterval.extend(/** @lends cc.MoveBy# */{
  808. _positionDelta:null,
  809. _startPosition:null,
  810. _previousPosition:null,
  811. ctor:function () {
  812. cc.ActionInterval.prototype.ctor.call(this);
  813. this._positionDelta = cc.p(0, 0);
  814. this._startPosition = cc.p(0, 0);
  815. this._previousPosition = cc.p(0, 0);
  816. },
  817. /**
  818. * @param {Number} duration duration in seconds
  819. * @param {cc.Point} position
  820. * @return {Boolean}
  821. */
  822. initWithDuration:function (duration, position) {
  823. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  824. this._positionDelta.x = position.x;
  825. this._positionDelta.y = position.y;
  826. return true;
  827. }
  828. return false;
  829. },
  830. /**
  831. * returns a new clone of the action
  832. * @returns {cc.MoveBy}
  833. */
  834. clone:function () {
  835. var action = new cc.MoveBy();
  836. action.initWithDuration(this._duration, this._positionDelta)
  837. return action;
  838. },
  839. /**
  840. * @param {Number} target
  841. */
  842. startWithTarget:function (target) {
  843. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  844. var locPosX = target.getPositionX();
  845. var locPosY = target.getPositionY();
  846. this._previousPosition.x = locPosX;
  847. this._previousPosition.y = locPosY;
  848. this._startPosition.x = locPosX;
  849. this._startPosition.y = locPosY;
  850. },
  851. /**
  852. * @param {Number} time time in seconds
  853. */
  854. update:function (time) {
  855. if (this._target) {
  856. var x = this._positionDelta.x * time;
  857. var y = this._positionDelta.y * time;
  858. var locStartPosition = this._startPosition;
  859. if (cc.ENABLE_STACKABLE_ACTIONS) {
  860. var targetX = this._target.getPositionX();
  861. var targetY = this._target.getPositionY();
  862. var locPreviousPosition = this._previousPosition;
  863. locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
  864. locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
  865. x = x + locStartPosition.x;
  866. y = y + locStartPosition.y;
  867. this._target.setPosition(x, y);
  868. locPreviousPosition.x = x;
  869. locPreviousPosition.y = y;
  870. } else {
  871. this._target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
  872. }
  873. }
  874. },
  875. /**
  876. * MoveTo reverse is not implemented
  877. */
  878. reverse:function () {
  879. return cc.MoveBy.create(this._duration, cc.p(-this._positionDelta.x, -this._positionDelta.y));
  880. }
  881. });
  882. /**
  883. * @param {Number} duration duration in seconds
  884. * @param {cc.Point} deltaPosition
  885. * @return {cc.MoveBy}
  886. * @example
  887. * // example
  888. * var actionTo = cc.MoveBy.create(2, cc.p(windowSize.width - 40, windowSize.height - 40));
  889. */
  890. cc.MoveBy.create = function (duration, deltaPosition) {
  891. var moveBy = new cc.MoveBy();
  892. moveBy.initWithDuration(duration, deltaPosition);
  893. return moveBy;
  894. };
  895. /**
  896. * Moves a CCNode object to the position x,y. x and y are absolute coordinates by modifying it's position attribute. <br/>
  897. * Several CCMoveTo actions can be concurrently called, and the resulting <br/>
  898. * movement will be the sum of individual movements.
  899. * @class
  900. * @extends cc.MoveBy
  901. */
  902. cc.MoveTo = cc.MoveBy.extend(/** @lends cc.MoveTo# */{
  903. _endPosition:null,
  904. ctor:function () {
  905. cc.MoveBy.prototype.ctor.call(this);
  906. this._endPosition = cc.p(0, 0);
  907. },
  908. /**
  909. * @param {Number} duration duration in seconds
  910. * @param {cc.Point} position
  911. * @return {Boolean}
  912. */
  913. initWithDuration:function (duration, position) {
  914. if (cc.MoveBy.prototype.initWithDuration.call(this, duration, position)) {
  915. this._endPosition.x = position.x;
  916. this._endPosition.y = position.y;
  917. return true;
  918. }
  919. return false;
  920. },
  921. /**
  922. * returns a new clone of the action
  923. * @returns {cc.MoveTo}
  924. */
  925. clone:function () {
  926. var action = new cc.MoveTo();
  927. action.initWithDuration(this._duration, this._endPosition);
  928. return action;
  929. },
  930. /**
  931. * @param {cc.Node} target
  932. */
  933. startWithTarget:function (target) {
  934. cc.MoveBy.prototype.startWithTarget.call(this, target);
  935. this._positionDelta.x = this._endPosition.x - target.getPositionX();
  936. this._positionDelta.y = this._endPosition.y - target.getPositionY();
  937. }
  938. });
  939. /**
  940. * @param {Number} duration duration in seconds
  941. * @param {cc.Point} position
  942. * @return {cc.MoveBy}
  943. * @example
  944. * // example
  945. * var actionBy = cc.MoveTo.create(2, cc.p(80, 80));
  946. */
  947. cc.MoveTo.create = function (duration, position) {
  948. var moveTo = new cc.MoveTo();
  949. moveTo.initWithDuration(duration, position);
  950. return moveTo;
  951. };
  952. /**
  953. * Skews a cc.Node object to given angles by modifying it's skewX and skewY attributes
  954. * @class
  955. * @extends cc.ActionInterval
  956. */
  957. cc.SkewTo = cc.ActionInterval.extend(/** @lends cc.SkewTo# */{
  958. _skewX:0,
  959. _skewY:0,
  960. _startSkewX:0,
  961. _startSkewY:0,
  962. _endSkewX:0,
  963. _endSkewY:0,
  964. _deltaX:0,
  965. _deltaY:0,
  966. ctor:function () {
  967. cc.ActionInterval.prototype.ctor.call(this);
  968. this._skewX = 0;
  969. this._skewY = 0;
  970. this._startSkewX = 0;
  971. this._startSkewY = 0;
  972. this._endSkewX = 0;
  973. this._endSkewY = 0;
  974. this._deltaX = 0;
  975. this._deltaY = 0;
  976. },
  977. /**
  978. * @param {Number} t time in seconds
  979. * @param {Number} sx
  980. * @param {Number} sy
  981. * @return {Boolean}
  982. */
  983. initWithDuration:function (t, sx, sy) {
  984. var ret = false;
  985. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  986. this._endSkewX = sx;
  987. this._endSkewY = sy;
  988. ret = true;
  989. }
  990. return ret;
  991. },
  992. /**
  993. * returns a new clone of the action
  994. * @returns {cc.SkewTo}
  995. */
  996. clone:function () {
  997. var action = new cc.SkewTo();
  998. action.initWithDuration(this._duration, this._endSkewX, this._endSkewY);
  999. return action;
  1000. },
  1001. /**
  1002. * @param {cc.Node} target
  1003. */
  1004. startWithTarget:function (target) {
  1005. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1006. this._startSkewX = target.getSkewX() % 180;
  1007. this._deltaX = this._endSkewX - this._startSkewX;
  1008. if (this._deltaX > 180)
  1009. this._deltaX -= 360;
  1010. if (this._deltaX < -180)
  1011. this._deltaX += 360;
  1012. this._startSkewY = target.getSkewY() % 360;
  1013. this._deltaY = this._endSkewY - this._startSkewY;
  1014. if (this._deltaY > 180)
  1015. this._deltaY -= 360;
  1016. if (this._deltaY < -180)
  1017. this._deltaY += 360;
  1018. },
  1019. /**
  1020. * @param {Number} t time in seconds
  1021. */
  1022. update:function (t) {
  1023. this._target.setSkewX(this._startSkewX + this._deltaX * t);
  1024. this._target.setSkewY(this._startSkewY + this._deltaY * t);
  1025. }
  1026. });
  1027. /**
  1028. * @param {Number} t time in seconds
  1029. * @param {Number} sx
  1030. * @param {Number} sy
  1031. * @return {cc.SkewTo}
  1032. * @example
  1033. * // example
  1034. * var actionTo = cc.SkewTo.create(2, 37.2, -37.2);
  1035. */
  1036. cc.SkewTo.create = function (t, sx, sy) {
  1037. var skewTo = new cc.SkewTo();
  1038. if (skewTo)
  1039. skewTo.initWithDuration(t, sx, sy);
  1040. return skewTo;
  1041. };
  1042. /** Skews a cc.Node object by skewX and skewY degrees
  1043. * @class
  1044. * @extends cc.SkewTo
  1045. */
  1046. cc.SkewBy = cc.SkewTo.extend(/** @lends cc.SkewBy# */{
  1047. /**
  1048. * @param {Number} t time in seconds
  1049. * @param {Number} deltaSkewX skew in degrees for X axis
  1050. * @param {Number} deltaSkewY skew in degrees for Y axis
  1051. * @return {Boolean}
  1052. */
  1053. initWithDuration:function (t, deltaSkewX, deltaSkewY) {
  1054. var ret = false;
  1055. if (cc.SkewTo.prototype.initWithDuration.call(this, t, deltaSkewX, deltaSkewY)) {
  1056. this._skewX = deltaSkewX;
  1057. this._skewY = deltaSkewY;
  1058. ret = true;
  1059. }
  1060. return ret;
  1061. },
  1062. /**
  1063. * returns a new clone of the action
  1064. * @returns {cc.SkewBy}
  1065. */
  1066. clone:function () {
  1067. var action = new cc.SkewBy();
  1068. action.initWithDuration(this._duration, this._skewX, this._skewY);
  1069. return action;
  1070. },
  1071. /**
  1072. * @param {cc.Node} target
  1073. */
  1074. startWithTarget:function (target) {
  1075. cc.SkewTo.prototype.startWithTarget.call(this, target);
  1076. this._deltaX = this._skewX;
  1077. this._deltaY = this._skewY;
  1078. this._endSkewX = this._startSkewX + this._deltaX;
  1079. this._endSkewY = this._startSkewY + this._deltaY;
  1080. },
  1081. /**
  1082. * @return {cc.ActionInterval}
  1083. */
  1084. reverse:function () {
  1085. return cc.SkewBy.create(this._duration, -this._skewX, -this._skewY);
  1086. }
  1087. });
  1088. /**
  1089. * @param {Number} t time in seconds
  1090. * @param {Number} sx sx skew in degrees for X axis
  1091. * @param {Number} sy sy skew in degrees for Y axis
  1092. * @return {cc.SkewBy}
  1093. * @example
  1094. * // example
  1095. * var actionBy = cc.SkewBy.create(2, 0, -90);
  1096. */
  1097. cc.SkewBy.create = function (t, sx, sy) {
  1098. var skewBy = new cc.SkewBy();
  1099. if (skewBy)
  1100. skewBy.initWithDuration(t, sx, sy);
  1101. return skewBy;
  1102. };
  1103. /** Moves a cc.Node object simulating a parabolic jump movement by modifying it's position attribute.
  1104. * @class
  1105. * @extends cc.ActionInterval
  1106. */
  1107. cc.JumpBy = cc.ActionInterval.extend(/** @lends cc.JumpBy# */{
  1108. _startPosition:null,
  1109. _delta:null,
  1110. _height:0,
  1111. _jumps:0,
  1112. _previousPosition:null,
  1113. ctor:function () {
  1114. cc.ActionInterval.prototype.ctor.call(this);
  1115. this._startPosition = cc.p(0, 0);
  1116. this._previousPosition = cc.p(0, 0);
  1117. this._delta = cc.p(0, 0);
  1118. this._height = 0;
  1119. this._jumps = 0;
  1120. },
  1121. /**
  1122. * @param {Number} duration
  1123. * @param {cc.Point} position
  1124. * @param {Number} height
  1125. * @param {Number} jumps
  1126. * @return {Boolean}
  1127. */
  1128. initWithDuration:function (duration, position, height, jumps) {
  1129. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1130. this._delta.x = position.x;
  1131. this._delta.y = position.y;
  1132. this._height = height;
  1133. this._jumps = jumps;
  1134. return true;
  1135. }
  1136. return false;
  1137. },
  1138. /**
  1139. * returns a new clone of the action
  1140. * @returns {cc.JumpBy}
  1141. */
  1142. clone:function () {
  1143. var action = new cc.JumpBy();
  1144. action.initWithDuration(this._duration, this._delta, this._height, this._jumps);
  1145. return action;
  1146. },
  1147. /**
  1148. * @param {cc.Node} target
  1149. */
  1150. startWithTarget:function (target) {
  1151. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1152. var locPosX = target.getPositionX();
  1153. var locPosY = target.getPositionY();
  1154. this._previousPosition.x = locPosX;
  1155. this._previousPosition.y = locPosY;
  1156. this._startPosition.x = locPosX;
  1157. this._startPosition.y = locPosY;
  1158. },
  1159. /**
  1160. * @param {Number} time
  1161. */
  1162. update:function (time) {
  1163. if (this._target) {
  1164. var frac = time * this._jumps % 1.0;
  1165. var y = this._height * 4 * frac * (1 - frac);
  1166. y += this._delta.y * time;
  1167. var x = this._delta.x * time;
  1168. var locStartPosition = this._startPosition;
  1169. if (cc.ENABLE_STACKABLE_ACTIONS) {
  1170. var targetX = this._target.getPositionX();
  1171. var targetY = this._target.getPositionY();
  1172. var locPreviousPosition = this._previousPosition;
  1173. locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
  1174. locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
  1175. x = x + locStartPosition.x;
  1176. y = y + locStartPosition.y;
  1177. this._target.setPosition(x, y);
  1178. locPreviousPosition.x = x;
  1179. locPreviousPosition.y = y;
  1180. } else {
  1181. this._target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
  1182. }
  1183. }
  1184. },
  1185. /**
  1186. * @return {cc.ActionInterval}
  1187. */
  1188. reverse:function () {
  1189. return cc.JumpBy.create(this._duration, cc.p(-this._delta.x, -this._delta.y), this._height, this._jumps);
  1190. }
  1191. });
  1192. /**
  1193. * @param {Number} duration
  1194. * @param {cc.Point} position
  1195. * @param {Number} height
  1196. * @param {Number} jumps
  1197. * @return {cc.JumpBy}
  1198. * @example
  1199. * // example
  1200. * var actionBy = cc.JumpBy.create(2, cc.p(300, 0), 50, 4);
  1201. */
  1202. cc.JumpBy.create = function (duration, position, height, jumps) {
  1203. var jumpBy = new cc.JumpBy();
  1204. jumpBy.initWithDuration(duration, position, height, jumps);
  1205. return jumpBy;
  1206. };
  1207. /** Moves a cc.Node object to a parabolic position simulating a jump movement by modifying it's position attribute.
  1208. * @class
  1209. * @extends cc.JumpBy
  1210. */
  1211. cc.JumpTo = cc.JumpBy.extend(/** @lends cc.JumpTo# */{
  1212. /**
  1213. * @param {cc.Node} target
  1214. */
  1215. startWithTarget:function (target) {
  1216. cc.JumpBy.prototype.startWithTarget.call(this, target);
  1217. this._delta.x = this._delta.x - this._startPosition.x;
  1218. this._delta.y = this._delta.y - this._startPosition.y;
  1219. },
  1220. /**
  1221. * returns a new clone of the action
  1222. * @returns {cc.JumpTo}
  1223. */
  1224. clone:function () {
  1225. var action = new cc.JumpTo();
  1226. action.initWithDuration(this._duration, this._delta, this._height, this._jumps);
  1227. return action;
  1228. }
  1229. });
  1230. /**
  1231. * @param {Number} duration
  1232. * @param {cc.Point} position
  1233. * @param {Number} height
  1234. * @param {Number} jumps
  1235. * @return {cc.JumpTo}
  1236. * @example
  1237. * // example
  1238. * var actionTo = cc.JumpTo.create(2, cc.p(300, 300), 50, 4);
  1239. */
  1240. cc.JumpTo.create = function (duration, position, height, jumps) {
  1241. var jumpTo = new cc.JumpTo();
  1242. jumpTo.initWithDuration(duration, position, height, jumps);
  1243. return jumpTo;
  1244. };
  1245. /**
  1246. * @function
  1247. * @param {Number} a
  1248. * @param {Number} b
  1249. * @param {Number} c
  1250. * @param {Number} d
  1251. * @param {Number} t
  1252. * @return {Number}
  1253. */
  1254. cc.bezierAt = function (a, b, c, d, t) {
  1255. return (Math.pow(1 - t, 3) * a +
  1256. 3 * t * (Math.pow(1 - t, 2)) * b +
  1257. 3 * Math.pow(t, 2) * (1 - t) * c +
  1258. Math.pow(t, 3) * d );
  1259. };
  1260. /** An action that moves the target with a cubic Bezier curve by a certain distance.
  1261. * @class
  1262. * @extends cc.ActionInterval
  1263. */
  1264. cc.BezierBy = cc.ActionInterval.extend(/** @lends cc.BezierBy# */{
  1265. _config:null,
  1266. _startPosition:null,
  1267. _previousPosition:null,
  1268. /**
  1269. * Constructor
  1270. */
  1271. ctor:function () {
  1272. cc.ActionInterval.prototype.ctor.call(this);
  1273. this._config = [];
  1274. this._startPosition = cc.p(0, 0);
  1275. this._previousPosition = cc.p(0, 0);
  1276. },
  1277. /**
  1278. * @param {Number} t time in seconds
  1279. * @param {Array} c Array of points
  1280. * @return {Boolean}
  1281. */
  1282. initWithDuration:function (t, c) {
  1283. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  1284. this._config = c;
  1285. return true;
  1286. }
  1287. return false;
  1288. },
  1289. /**
  1290. * returns a new clone of the action
  1291. * @returns {cc.BezierBy}
  1292. */
  1293. clone:function () {
  1294. var action = new cc.BezierBy();
  1295. var newConfigs = [];
  1296. for (var i = 0; i < this._config.length; i++) {
  1297. var selConf = this._config[i];
  1298. newConfigs.push(cc.p(selConf.x, selConf.y));
  1299. }
  1300. action.initWithDuration(this._duration, newConfigs);
  1301. return action;
  1302. },
  1303. /**
  1304. * @param {cc.Node} target
  1305. */
  1306. startWithTarget:function (target) {
  1307. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1308. var locPosX = target.getPositionX();
  1309. var locPosY = target.getPositionY();
  1310. this._previousPosition.x = locPosX;
  1311. this._previousPosition.y = locPosY;
  1312. this._startPosition.x = locPosX;
  1313. this._startPosition.y = locPosY;
  1314. },
  1315. /**
  1316. * @param {Number} time
  1317. */
  1318. update:function (time) {
  1319. if (this._target) {
  1320. var locConfig = this._config;
  1321. var xa = 0;
  1322. var xb = locConfig[0].x;
  1323. var xc = locConfig[1].x;
  1324. var xd = locConfig[2].x;
  1325. var ya = 0;
  1326. var yb = locConfig[0].y;
  1327. var yc = locConfig[1].y;
  1328. var yd = locConfig[2].y;
  1329. var x = cc.bezierAt(xa, xb, xc, xd, time);
  1330. var y = cc.bezierAt(ya, yb, yc, yd, time);
  1331. var locStartPosition = this._startPosition;
  1332. if (cc.ENABLE_STACKABLE_ACTIONS) {
  1333. var targetX = this._target.getPositionX();
  1334. var targetY = this._target.getPositionY();
  1335. var locPreviousPosition = this._previousPosition;
  1336. locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
  1337. locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
  1338. x = x + locStartPosition.x;
  1339. y = y + locStartPosition.y;
  1340. this._target.setPosition(x, y);
  1341. locPreviousPosition.x = x;
  1342. locPreviousPosition.y = y;
  1343. } else {
  1344. this._target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
  1345. }
  1346. }
  1347. },
  1348. /**
  1349. * @return {cc.ActionInterval}
  1350. */
  1351. reverse:function () {
  1352. var locConfig = this._config;
  1353. var r = [
  1354. cc.pAdd(locConfig[1], cc.pNeg(locConfig[2])),
  1355. cc.pAdd(locConfig[0], cc.pNeg(locConfig[2])),
  1356. cc.pNeg(locConfig[2]) ];
  1357. return cc.BezierBy.create(this._duration, r);
  1358. }
  1359. });
  1360. /**
  1361. * @param {Number} t time in seconds
  1362. * @param {Array} c Array of points
  1363. * @return {cc.BezierBy}
  1364. * @example
  1365. * // example
  1366. * var bezier = [cc.p(0, windowSize.height / 2), cc.p(300, -windowSize.height / 2), cc.p(300, 100)];
  1367. * var bezierForward = cc.BezierBy.create(3, bezier);
  1368. *
  1369. */
  1370. cc.BezierBy.create = function (t, c) {
  1371. var bezierBy = new cc.BezierBy();
  1372. bezierBy.initWithDuration(t, c);
  1373. return bezierBy;
  1374. };
  1375. /** An action that moves the target with a cubic Bezier curve to a destination point.
  1376. * @class
  1377. * @extends cc.BezierBy
  1378. */
  1379. cc.BezierTo = cc.BezierBy.extend(/** @lends cc.BezierTo# */{
  1380. _toConfig:null,
  1381. ctor:function () {
  1382. cc.BezierBy.prototype.ctor.call(this);
  1383. this._toConfig = [];
  1384. },
  1385. /**
  1386. * @param {Number} t time in seconds
  1387. * @param {Array} c Array of points
  1388. * @return {Boolean}
  1389. */
  1390. initWithDuration:function (t, c) {
  1391. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  1392. this._toConfig = c;
  1393. return true;
  1394. }
  1395. return false;
  1396. },
  1397. /**
  1398. * returns a new clone of the action
  1399. * @returns {cc.BezierTo}
  1400. */
  1401. clone:function () {
  1402. var action = new cc.BezierTo();
  1403. action.initWithDuration(this._duration, this._toConfig);
  1404. return action;
  1405. },
  1406. /**
  1407. * @param {cc.Node} target
  1408. */
  1409. startWithTarget:function (target) {
  1410. cc.BezierBy.prototype.startWithTarget.call(this, target);
  1411. var locStartPos = this._startPosition;
  1412. var locToConfig = this._toConfig;
  1413. var locConfig = this._config;
  1414. locConfig[0] = cc.pSub(locToConfig[0], locStartPos);
  1415. locConfig[1] = cc.pSub(locToConfig[1], locStartPos);
  1416. locConfig[2] = cc.pSub(locToConfig[2], locStartPos);
  1417. }
  1418. });
  1419. /**
  1420. * @param {Number} t
  1421. * @param {Array} c array of points
  1422. * @return {cc.BezierTo}
  1423. * @example
  1424. * // example
  1425. * var bezier = [cc.p(0, windowSize.height / 2), cc.p(300, -windowSize.height / 2), cc.p(300, 100)];
  1426. * var bezierTo = cc.BezierTo.create(2, bezier);
  1427. */
  1428. cc.BezierTo.create = function (t, c) {
  1429. var bezierTo = new cc.BezierTo();
  1430. bezierTo.initWithDuration(t, c);
  1431. return bezierTo;
  1432. };
  1433. /** Scales a cc.Node object to a zoom factor by modifying it's scale attribute.
  1434. * @warning This action doesn't support "reverse"
  1435. * @class
  1436. * @extends cc.ActionInterval
  1437. */
  1438. cc.ScaleTo = cc.ActionInterval.extend(/** @lends cc.ScaleTo# */{
  1439. _scaleX:1,
  1440. _scaleY:1,
  1441. _startScaleX:1,
  1442. _startScaleY:1,
  1443. _endScaleX:0,
  1444. _endScaleY:0,
  1445. _deltaX:0,
  1446. _deltaY:0,
  1447. ctor:function () {
  1448. cc.ActionInterval.prototype.ctor.call(this);
  1449. this._scaleX = 1;
  1450. this._scaleY = 1;
  1451. this._startScaleX = 1;
  1452. this._startScaleY = 1;
  1453. this._endScaleX = 0;
  1454. this._endScaleY = 0;
  1455. this._deltaX = 0;
  1456. this._deltaY = 0;
  1457. },
  1458. /**
  1459. * @param {Number} duration
  1460. * @param {Number} sx
  1461. * @param {Number} [sy=]
  1462. * @return {Boolean}
  1463. */
  1464. initWithDuration:function (duration, sx, sy) { //function overload here
  1465. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1466. this._endScaleX = sx;
  1467. this._endScaleY = (sy != null) ? sy : sx;
  1468. return true;
  1469. }
  1470. return false;
  1471. },
  1472. /**
  1473. * returns a new clone of the action
  1474. * @returns {cc.ScaleTo}
  1475. */
  1476. clone:function () {
  1477. var action = new cc.ScaleTo();
  1478. action.initWithDuration(this._duration, this._endScaleX, this._endScaleY);
  1479. return action;
  1480. },
  1481. /**
  1482. * @param {cc.Node} target
  1483. */
  1484. startWithTarget:function (target) {
  1485. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1486. this._startScaleX = target.getScaleX();
  1487. this._startScaleY = target.getScaleY();
  1488. this._deltaX = this._endScaleX - this._startScaleX;
  1489. this._deltaY = this._endScaleY - this._startScaleY;
  1490. },
  1491. /**
  1492. * @param {Number} time
  1493. */
  1494. update:function (time) {
  1495. if (this._target)
  1496. this._target.setScale(this._startScaleX + this._deltaX * time, this._startScaleY + this._deltaY * time);
  1497. }
  1498. });
  1499. /**
  1500. * @param {Number} duration
  1501. * @param {Number} sx scale parameter in X
  1502. * @param {Number} [sy=] scale parameter in Y, if Null equal to sx
  1503. * @return {cc.ScaleTo}
  1504. * @example
  1505. * // example
  1506. * // It scales to 0.5 in both X and Y.
  1507. * var actionTo = cc.ScaleTo.create(2, 0.5);
  1508. *
  1509. * // It scales to 0.5 in x and 2 in Y
  1510. * var actionTo = cc.ScaleTo.create(2, 0.5, 2);
  1511. */
  1512. cc.ScaleTo.create = function (duration, sx, sy) { //function overload
  1513. var scaleTo = new cc.ScaleTo();
  1514. scaleTo.initWithDuration(duration, sx, sy);
  1515. return scaleTo;
  1516. };
  1517. /** Scales a cc.Node object a zoom factor by modifying it's scale attribute.
  1518. * @class
  1519. * @extends cc.ScaleTo
  1520. */
  1521. cc.ScaleBy = cc.ScaleTo.extend(/** @lends cc.ScaleBy# */{
  1522. /**
  1523. * @param {Number} target
  1524. */
  1525. startWithTarget:function (target) {
  1526. cc.ScaleTo.prototype.startWithTarget.call(this, target);
  1527. this._deltaX = this._startScaleX * this._endScaleX - this._startScaleX;
  1528. this._deltaY = this._startScaleY * this._endScaleY - this._startScaleY;
  1529. },
  1530. /**
  1531. * @return {cc.ActionInterval}
  1532. */
  1533. reverse:function () {
  1534. return cc.ScaleBy.create(this._duration, 1 / this._endScaleX, 1 / this._endScaleY);
  1535. },
  1536. /**
  1537. * returns a new clone of the action
  1538. * @returns {cc.ScaleBy}
  1539. */
  1540. clone:function () {
  1541. var action = new cc.ScaleBy();
  1542. action.initWithDuration(this._duration, this._endScaleX, this._endScaleY);
  1543. return action;
  1544. }
  1545. });
  1546. /**
  1547. * @param {Number} duration duration in seconds
  1548. * @param {Number} sx sx scale parameter in X
  1549. * @param {Number|Null} [sy=] sy scale parameter in Y, if Null equal to sx
  1550. * @return {cc.ScaleBy}
  1551. * @example
  1552. * // example without sy, it scales by 2 both in X and Y
  1553. * var actionBy = cc.ScaleBy.create(2, 2);
  1554. *
  1555. * //example with sy, it scales by 0.25 in X and 4.5 in Y
  1556. * var actionBy2 = cc.ScaleBy.create(2, 0.25, 4.5);
  1557. */
  1558. cc.ScaleBy.create = function (duration, sx, sy) {
  1559. var scaleBy = new cc.ScaleBy();
  1560. scaleBy.initWithDuration(duration, sx, sy);
  1561. return scaleBy;
  1562. };
  1563. /** Blinks a cc.Node object by modifying it's visible attribute
  1564. * @class
  1565. * @extends cc.ActionInterval
  1566. */
  1567. cc.Blink = cc.ActionInterval.extend(/** @lends cc.Blink# */{
  1568. _times:0,
  1569. _originalState:false,
  1570. ctor:function () {
  1571. cc.ActionInterval.prototype.ctor.call(this);
  1572. this._times = 0;
  1573. this._originalState = false;
  1574. },
  1575. /**
  1576. * @param {Number} duration duration in seconds
  1577. * @param {Number} blinks blinks in times
  1578. * @return {Boolean}
  1579. */
  1580. initWithDuration:function (duration, blinks) {
  1581. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1582. this._times = blinks;
  1583. return true;
  1584. }
  1585. return false;
  1586. },
  1587. /**
  1588. * returns a new clone of the action
  1589. * @returns {cc.Blink}
  1590. */
  1591. clone:function () {
  1592. var action = new cc.Blink();
  1593. action.initWithDuration(this._duration, this._times);
  1594. return action;
  1595. },
  1596. /**
  1597. * @param {Number} time time in seconds
  1598. */
  1599. update:function (time) {
  1600. if (this._target && !this.isDone()) {
  1601. var slice = 1.0 / this._times;
  1602. var m = time % slice;
  1603. this._target.setVisible(m > (slice / 2));
  1604. }
  1605. },
  1606. startWithTarget:function (target) {
  1607. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1608. this._originalState = target.isVisible();
  1609. },
  1610. stop:function () {
  1611. this._target.setVisible(this._originalState);
  1612. cc.ActionInterval.prototype.stop.call(this);
  1613. },
  1614. /**
  1615. * @return {cc.ActionInterval}
  1616. */
  1617. reverse:function () {
  1618. return cc.Blink.create(this._duration, this._times);
  1619. }
  1620. });
  1621. /**
  1622. * @param {Number} duration duration in seconds
  1623. * @param blinks blinks in times
  1624. * @return {cc.Blink}
  1625. * @example
  1626. * // example
  1627. * var action = cc.Blink.create(2, 10);
  1628. */
  1629. cc.Blink.create = function (duration, blinks) {
  1630. var blink = new cc.Blink();
  1631. blink.initWithDuration(duration, blinks);
  1632. return blink;
  1633. };
  1634. /** Fades an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from the current value to a custom one.
  1635. * @warning This action doesn't support "reverse"
  1636. * @class
  1637. * @extends cc.ActionInterval
  1638. */
  1639. cc.FadeTo = cc.ActionInterval.extend(/** @lends cc.FadeTo# */{
  1640. _toOpacity:null,
  1641. _fromOpacity:null,
  1642. ctor:function () {
  1643. cc.ActionInterval.prototype.ctor.call(this);
  1644. this._toOpacity = 0;
  1645. this._fromOpacity = 0;
  1646. },
  1647. /**
  1648. * @param {Number} duration duration in seconds
  1649. * @param {Number} opacity
  1650. * @return {Boolean}
  1651. */
  1652. initWithDuration:function (duration, opacity) {
  1653. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1654. this._toOpacity = opacity;
  1655. return true;
  1656. }
  1657. return false;
  1658. },
  1659. /**
  1660. * returns a new clone of the action
  1661. * @returns {cc.FadeTo}
  1662. */
  1663. clone:function () {
  1664. var action = new cc.FadeTo();
  1665. action.initWithDuration(this._duration, this._toOpacity);
  1666. return action;
  1667. },
  1668. /**
  1669. * @param {Number} time time in seconds
  1670. */
  1671. update:function (time) {
  1672. if (this._target.RGBAProtocol) {
  1673. var fromOpacity = this._fromOpacity;
  1674. this._target.setOpacity((fromOpacity + (this._toOpacity - fromOpacity) * time));
  1675. }
  1676. },
  1677. /**
  1678. * @param {cc.Sprite} target
  1679. */
  1680. startWithTarget:function (target) {
  1681. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1682. if(this._target.RGBAProtocol){
  1683. this._fromOpacity = target.getOpacity();
  1684. }
  1685. }
  1686. });
  1687. /**
  1688. * @param {Number} duration
  1689. * @param {Number} opacity 0-255, 0 is transparent
  1690. * @return {cc.FadeTo}
  1691. * @example
  1692. * // example
  1693. * var action = cc.FadeTo.create(1.0, 0);
  1694. */
  1695. cc.FadeTo.create = function (duration, opacity) {
  1696. var fadeTo = new cc.FadeTo();
  1697. fadeTo.initWithDuration(duration, opacity);
  1698. return fadeTo;
  1699. };
  1700. /** Fades In an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 0 to 255.<br/>
  1701. * The "reverse" of this action is FadeOut
  1702. * @class
  1703. * @extends cc.FadeTo
  1704. */
  1705. cc.FadeIn = cc.FadeTo.extend(/** @lends cc.FadeIn# */{
  1706. _reverseAction: null,
  1707. /**
  1708. * @return {cc.ActionInterval}
  1709. */
  1710. reverse:function () {
  1711. var action = new cc.FadeOut();
  1712. action.initWithDuration(this._duration, 0);
  1713. return action;
  1714. },
  1715. /**
  1716. * returns a new clone of the action
  1717. * @returns {cc.FadeIn}
  1718. */
  1719. clone:function () {
  1720. var action = new cc.FadeIn();
  1721. action.initWithDuration(this._duration, this._toOpacity);
  1722. return action;
  1723. },
  1724. /**
  1725. * @param {cc.Sprite} target
  1726. */
  1727. startWithTarget:function (target) {
  1728. if(this._reverseAction)
  1729. this._toOpacity = this._reverseAction._fromOpacity;
  1730. cc.FadeTo.prototype.startWithTarget.call(this, target);
  1731. }
  1732. });
  1733. /**
  1734. * @param {Number} duration duration in seconds
  1735. * @param {Number} [toOpacity] to opacity
  1736. * @return {cc.FadeIn}
  1737. * @example
  1738. * //example
  1739. * var action = cc.FadeIn.create(1.0);
  1740. */
  1741. cc.FadeIn.create = function (duration, toOpacity) {
  1742. if(toOpacity == null)
  1743. toOpacity = 255;
  1744. var action = new cc.FadeIn();
  1745. action.initWithDuration(duration, toOpacity);
  1746. return action;
  1747. };
  1748. /** Fades Out an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 255 to 0.
  1749. * The "reverse" of this action is FadeIn
  1750. * @class
  1751. * @extends cc.FadeTo
  1752. */
  1753. cc.FadeOut = cc.FadeTo.extend(/** @lends cc.FadeOut# */{
  1754. /**
  1755. * @return {cc.ActionInterval}
  1756. */
  1757. reverse:function () {
  1758. var action = new cc.FadeIn();
  1759. action._reverseAction = this;
  1760. action.initWithDuration(this._duration, 255);
  1761. return action;
  1762. },
  1763. /**
  1764. * returns a new clone of the action
  1765. * @returns {cc.FadeOut}
  1766. */
  1767. clone:function () {
  1768. var action = new cc.FadeOut();
  1769. action.initWithDuration(this._duration, this._toOpacity);
  1770. return action;
  1771. }
  1772. });
  1773. /**
  1774. * @param {Number} d duration in seconds
  1775. * @return {cc.FadeOut}
  1776. * @example
  1777. * // example
  1778. * var action = cc.FadeOut.create(1.0);
  1779. */
  1780. cc.FadeOut.create = function (d) {
  1781. var action = new cc.FadeOut();
  1782. action.initWithDuration(d, 0);
  1783. return action;
  1784. };
  1785. /** Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  1786. * @warning This action doesn't support "reverse"
  1787. * @class
  1788. * @extends cc.ActionInterval
  1789. */
  1790. cc.TintTo = cc.ActionInterval.extend(/** @lends cc.TintTo# */{
  1791. _to:null,
  1792. _from:null,
  1793. ctor:function () {
  1794. cc.ActionInterval.prototype.ctor.call(this);
  1795. this._to = cc.c3b(0, 0, 0);
  1796. this._from = cc.c3b(0, 0, 0);
  1797. },
  1798. /**
  1799. * @param {Number} duration
  1800. * @param {Number} red 0-255
  1801. * @param {Number} green 0-255
  1802. * @param {Number} blue 0-255
  1803. * @return {Boolean}
  1804. */
  1805. initWithDuration:function (duration, red, green, blue) {
  1806. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1807. this._to = cc.c3b(red, green, blue);
  1808. return true;
  1809. }
  1810. return false;
  1811. },
  1812. /**
  1813. * returns a new clone of the action
  1814. * @returns {cc.TintTo}
  1815. */
  1816. clone:function () {
  1817. var action = new cc.TintTo();
  1818. var locTo = this._to;
  1819. action.initWithDuration(this._duration, locTo.r, locTo.g, locTo.b);
  1820. return action;
  1821. },
  1822. /**
  1823. * @param {cc.Sprite} target
  1824. */
  1825. startWithTarget:function (target) {
  1826. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1827. if (this._target.RGBAProtocol) {
  1828. this._from = this._target.getColor();
  1829. }
  1830. },
  1831. /**
  1832. * @param {Number} time time in seconds
  1833. */
  1834. update:function (time) {
  1835. var locFrom = this._from, locTo = this._to;
  1836. if (this._target.RGBAProtocol) {
  1837. this._target.setColor(cc.c3b(locFrom.r + (locTo.r - locFrom.r) * time,
  1838. (locFrom.g + (locTo.g - locFrom.g) * time), (locFrom.b + (locTo.b - locFrom.b) * time)));
  1839. }
  1840. }
  1841. });
  1842. /**
  1843. * @param {Number} duration
  1844. * @param {Number} red 0-255
  1845. * @param {Number} green 0-255
  1846. * @param {Number} blue 0-255
  1847. * @return {cc.TintTo}
  1848. * @example
  1849. * // example
  1850. * var action = cc.TintTo.create(2, 255, 0, 255);
  1851. */
  1852. cc.TintTo.create = function (duration, red, green, blue) {
  1853. var tintTo = new cc.TintTo();
  1854. tintTo.initWithDuration(duration, red, green, blue);
  1855. return tintTo;
  1856. };
  1857. /** Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  1858. * @class
  1859. * @extends cc.ActionInterval
  1860. */
  1861. cc.TintBy = cc.ActionInterval.extend(/** @lends cc.TintBy# */{
  1862. _deltaR:0,
  1863. _deltaG:0,
  1864. _deltaB:0,
  1865. _fromR:0,
  1866. _fromG:0,
  1867. _fromB:0,
  1868. ctor:function () {
  1869. cc.ActionInterval.prototype.ctor.call(this);
  1870. this._deltaR = 0;
  1871. this._deltaG = 0;
  1872. this._deltaB = 0;
  1873. this._fromR = 0;
  1874. this._fromG = 0;
  1875. this._fromB = 0;
  1876. },
  1877. /**
  1878. * @param {Number} duration
  1879. * @param {Number} deltaRed 0-255
  1880. * @param {Number} deltaGreen 0-255
  1881. * @param {Number} deltaBlue 0-255
  1882. * @return {Boolean}
  1883. */
  1884. initWithDuration:function (duration, deltaRed, deltaGreen, deltaBlue) {
  1885. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1886. this._deltaR = deltaRed;
  1887. this._deltaG = deltaGreen;
  1888. this._deltaB = deltaBlue;
  1889. return true;
  1890. }
  1891. return false;
  1892. },
  1893. /**
  1894. * returns a new clone of the action
  1895. * @returns {cc.TintBy}
  1896. */
  1897. clone:function () {
  1898. var action = new cc.TintBy();
  1899. action.initWithDuration(this._duration, this._deltaR, this._deltaG, this._deltaB);
  1900. return action;
  1901. },
  1902. /**
  1903. * @param {cc.Sprite} target
  1904. */
  1905. startWithTarget:function (target) {
  1906. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1907. if (target.RGBAProtocol) {
  1908. var color = target.getColor();
  1909. this._fromR = color.r;
  1910. this._fromG = color.g;
  1911. this._fromB = color.b;
  1912. }
  1913. },
  1914. /**
  1915. * @param {Number} time time in seconds
  1916. */
  1917. update:function (time) {
  1918. if (this._target.RGBAProtocol) {
  1919. this._target.setColor(cc.c3b((this._fromR + this._deltaR * time),
  1920. (this._fromG + this._deltaG * time),
  1921. (this._fromB + this._deltaB * time)));
  1922. }
  1923. },
  1924. /**
  1925. * @return {cc.ActionInterval}
  1926. */
  1927. reverse:function () {
  1928. return cc.TintBy.create(this._duration, -this._deltaR, -this._deltaG, -this._deltaB);
  1929. }
  1930. });
  1931. /**
  1932. * @param {Number} duration duration in seconds
  1933. * @param {Number} deltaRed
  1934. * @param {Number} deltaGreen
  1935. * @param {Number} deltaBlue
  1936. * @return {cc.TintBy}
  1937. * @example
  1938. * // example
  1939. * var action = cc.TintBy.create(2, -127, -255, -127);
  1940. */
  1941. cc.TintBy.create = function (duration, deltaRed, deltaGreen, deltaBlue) {
  1942. var tintBy = new cc.TintBy();
  1943. tintBy.initWithDuration(duration, deltaRed, deltaGreen, deltaBlue);
  1944. return tintBy;
  1945. };
  1946. /** Delays the action a certain amount of seconds
  1947. * @class
  1948. * @extends cc.ActionInterval
  1949. */
  1950. cc.DelayTime = cc.ActionInterval.extend(/** @lends cc.DelayTime# */{
  1951. /**
  1952. * @param {Number} time time in seconds
  1953. */
  1954. update:function (time) {
  1955. },
  1956. /**
  1957. * @return {cc.ActionInterval}
  1958. */
  1959. reverse:function () {
  1960. return cc.DelayTime.create(this._duration);
  1961. },
  1962. /**
  1963. * returns a new clone of the action
  1964. * @returns {cc.DelayTime}
  1965. */
  1966. clone:function () {
  1967. var action = new cc.DelayTime();
  1968. action.initWithDuration(this._duration);
  1969. return action;
  1970. }
  1971. });
  1972. /**
  1973. * @param {Number} d duration in seconds
  1974. * @return {cc.DelayTime}
  1975. * @example
  1976. * // example
  1977. * var delay = cc.DelayTime.create(1);
  1978. */
  1979. cc.DelayTime.create = function (d) {
  1980. var action = new cc.DelayTime();
  1981. action.initWithDuration(d);
  1982. return action;
  1983. };
  1984. /**
  1985. * Executes an action in reverse order, from time=duration to time=0
  1986. * @warning Use this action carefully. This action is not
  1987. * sequenceable. Use it as the default "reversed" method
  1988. * of your own actions, but using it outside the "reversed"
  1989. * scope is not recommended.
  1990. * @class
  1991. * @extends cc.ActionInterval
  1992. */
  1993. cc.ReverseTime = cc.ActionInterval.extend(/** @lends cc.ReverseTime# */{
  1994. _other:null,
  1995. ctor:function () {
  1996. cc.ActionInterval.prototype.ctor.call(this);
  1997. this._other = null;
  1998. },
  1999. /**
  2000. * @param {cc.FiniteTimeAction} action
  2001. * @return {Boolean}
  2002. */
  2003. initWithAction:function (action) {
  2004. if(!action)
  2005. throw "cc.ReverseTime.initWithAction(): action must be non null";
  2006. if(action == this._other)
  2007. throw "cc.ReverseTime.initWithAction(): the action was already passed in.";
  2008. if (cc.ActionInterval.prototype.initWithDuration.call(this, action.getDuration())) {
  2009. // Don't leak if action is reused
  2010. this._other = action;
  2011. return true;
  2012. }
  2013. return false;
  2014. },
  2015. /**
  2016. * returns a new clone of the action
  2017. * @returns {cc.ReverseTime}
  2018. */
  2019. clone:function () {
  2020. var action = new cc.ReverseTime();
  2021. action.initWithAction(this._other.clone());
  2022. return action;
  2023. },
  2024. /**
  2025. * @param {cc.Node} target
  2026. */
  2027. startWithTarget:function (target) {
  2028. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2029. this._other.startWithTarget(target);
  2030. },
  2031. /**
  2032. * @param {Number} time time in seconds
  2033. */
  2034. update:function (time) {
  2035. if (this._other)
  2036. this._other.update(1 - time);
  2037. },
  2038. /**
  2039. * @return {cc.ActionInterval}
  2040. */
  2041. reverse:function () {
  2042. return this._other.clone();
  2043. },
  2044. /**
  2045. * Stop the action
  2046. */
  2047. stop:function () {
  2048. this._other.stop();
  2049. cc.Action.prototype.stop.call(this);
  2050. }
  2051. });
  2052. /**
  2053. * @param {cc.FiniteTimeAction} action
  2054. * @return {cc.ReverseTime}
  2055. * @example
  2056. * // example
  2057. * var reverse = cc.ReverseTime.create(this);
  2058. */
  2059. cc.ReverseTime.create = function (action) {
  2060. var reverseTime = new cc.ReverseTime();
  2061. reverseTime.initWithAction(action);
  2062. return reverseTime;
  2063. };
  2064. /** Animates a sprite given the name of an Animation
  2065. * @class
  2066. * @extends cc.ActionInterval
  2067. */
  2068. cc.Animate = cc.ActionInterval.extend(/** @lends cc.Animate# */{
  2069. _animation:null,
  2070. _nextFrame:0,
  2071. _origFrame:null,
  2072. _executedLoops:0,
  2073. _splitTimes:null,
  2074. ctor:function () {
  2075. cc.ActionInterval.prototype.ctor.call(this);
  2076. this._animation = null;
  2077. this._nextFrame = 0;
  2078. this._origFrame = null;
  2079. this._executedLoops = 0;
  2080. this._splitTimes = [];
  2081. },
  2082. /**
  2083. * @return {cc.Animation}
  2084. */
  2085. getAnimation:function () {
  2086. return this._animation;
  2087. },
  2088. /**
  2089. * @param {cc.Animation} animation
  2090. */
  2091. setAnimation:function (animation) {
  2092. this._animation = animation;
  2093. },
  2094. /**
  2095. * @param {cc.Animation} animation
  2096. * @return {Boolean}
  2097. */
  2098. initWithAnimation:function (animation) {
  2099. if(!animation)
  2100. throw "cc.Animate.initWithAnimation(): animation must be non-NULL";
  2101. var singleDuration = animation.getDuration();
  2102. if (this.initWithDuration(singleDuration * animation.getLoops())) {
  2103. this._nextFrame = 0;
  2104. this.setAnimation(animation);
  2105. this._origFrame = null;
  2106. this._executedLoops = 0;
  2107. var locTimes = this._splitTimes;
  2108. locTimes.length = 0;
  2109. var accumUnitsOfTime = 0;
  2110. var newUnitOfTimeValue = singleDuration / animation.getTotalDelayUnits();
  2111. var frames = animation.getFrames();
  2112. cc.ArrayVerifyType(frames, cc.AnimationFrame);
  2113. for (var i = 0; i < frames.length; i++) {
  2114. var frame = frames[i];
  2115. var value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;
  2116. accumUnitsOfTime += frame.getDelayUnits();
  2117. locTimes.push(value);
  2118. }
  2119. return true;
  2120. }
  2121. return false;
  2122. },
  2123. /**
  2124. * returns a new clone of the action
  2125. * @returns {cc.Animate}
  2126. */
  2127. clone:function () {
  2128. var action = new cc.Animate();
  2129. action.initWithAnimation(this._animation.clone());
  2130. return action;
  2131. },
  2132. /**
  2133. * @param {cc.Sprite} target
  2134. */
  2135. startWithTarget:function (target) {
  2136. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2137. if (this._animation.getRestoreOriginalFrame())
  2138. this._origFrame = target.displayFrame();
  2139. this._nextFrame = 0;
  2140. this._executedLoops = 0;
  2141. },
  2142. /**
  2143. * @param {Number} time
  2144. */
  2145. update:function (time) {
  2146. // if t==1, ignore. Animation should finish with t==1
  2147. if (time < 1.0) {
  2148. time *= this._animation.getLoops();
  2149. // new loop? If so, reset frame counter
  2150. var loopNumber = 0 | time;
  2151. if (loopNumber > this._executedLoops) {
  2152. this._nextFrame = 0;
  2153. this._executedLoops++;
  2154. }
  2155. // new t for animations
  2156. time = time % 1.0;
  2157. }
  2158. var frames = this._animation.getFrames();
  2159. var numberOfFrames = frames.length, locSplitTimes = this._splitTimes;
  2160. for (var i = this._nextFrame; i < numberOfFrames; i++) {
  2161. if (locSplitTimes[i] <= time) {
  2162. this._target.setDisplayFrame(frames[i].getSpriteFrame());
  2163. this._nextFrame = i + 1;
  2164. } else {
  2165. // Issue 1438. Could be more than one frame per tick, due to low frame rate or frame delta < 1/FPS
  2166. break;
  2167. }
  2168. }
  2169. },
  2170. /**
  2171. * @return {cc.ActionInterval}
  2172. */
  2173. reverse:function () {
  2174. var locAnimation = this._animation;
  2175. var oldArray = locAnimation.getFrames();
  2176. var newArray = [];
  2177. cc.ArrayVerifyType(oldArray, cc.AnimationFrame);
  2178. if (oldArray.length > 0) {
  2179. for (var i = oldArray.length - 1; i >= 0; i--) {
  2180. var element = oldArray[i];
  2181. if (!element)
  2182. break;
  2183. newArray.push(element.clone());
  2184. }
  2185. }
  2186. var newAnim = cc.Animation.createWithAnimationFrames(newArray, locAnimation.getDelayPerUnit(), locAnimation.getLoops());
  2187. newAnim.setRestoreOriginalFrame(locAnimation.getRestoreOriginalFrame());
  2188. return cc.Animate.create(newAnim);
  2189. },
  2190. /**
  2191. * stop the action
  2192. */
  2193. stop:function () {
  2194. if (this._animation.getRestoreOriginalFrame() && this._target)
  2195. this._target.setDisplayFrame(this._origFrame);
  2196. cc.Action.prototype.stop.call(this);
  2197. }
  2198. });
  2199. /**
  2200. * create the animate with animation
  2201. * @param {cc.Animation} animation
  2202. * @return {cc.Animate}
  2203. * @example
  2204. * // example
  2205. * // create the animation with animation
  2206. * var anim = cc.Animate.create(dance_grey);
  2207. */
  2208. cc.Animate.create = function (animation) {
  2209. var animate = new cc.Animate();
  2210. animate.initWithAnimation(animation);
  2211. return animate;
  2212. };
  2213. /**
  2214. * <p>
  2215. * Overrides the target of an action so that it always runs on the target<br/>
  2216. * specified at action creation rather than the one specified by runAction.
  2217. * </p>
  2218. * @class
  2219. * @extends cc.ActionInterval
  2220. */
  2221. cc.TargetedAction = cc.ActionInterval.extend(/** @lends cc.TargetedAction# */{
  2222. _action:null,
  2223. _forcedTarget:null,
  2224. ctor:function () {
  2225. cc.ActionInterval.prototype.ctor.call(this);
  2226. this._action = null;
  2227. this._forcedTarget = null;
  2228. },
  2229. /**
  2230. * Init an action with the specified action and forced target
  2231. * @param {cc.Node} target
  2232. * @param {cc.FiniteTimeAction} action
  2233. * @return {Boolean}
  2234. */
  2235. initWithTarget:function (target, action) {
  2236. if (this.initWithDuration(action.getDuration())) {
  2237. this._forcedTarget = target;
  2238. this._action = action;
  2239. return true;
  2240. }
  2241. return false;
  2242. },
  2243. /**
  2244. * returns a new clone of the action
  2245. * @returns {cc.TargetedAction}
  2246. */
  2247. clone:function () {
  2248. var action = new cc.TargetedAction();
  2249. action.initWithTarget(this._forcedTarget, this._action.clone());
  2250. return action;
  2251. },
  2252. startWithTarget:function (target) {
  2253. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2254. this._action.startWithTarget(this._forcedTarget);
  2255. },
  2256. stop:function () {
  2257. this._action.stop();
  2258. },
  2259. update:function (time) {
  2260. this._action.update(time);
  2261. },
  2262. /**
  2263. * return the target that the action will be forced to run with
  2264. * @return {cc.Node}
  2265. */
  2266. getForcedTarget:function () {
  2267. return this._forcedTarget;
  2268. },
  2269. /**
  2270. * set the target that the action will be forced to run with
  2271. * @param {cc.Node} forcedTarget
  2272. */
  2273. setForcedTarget:function (forcedTarget) {
  2274. if (this._forcedTarget != forcedTarget)
  2275. this._forcedTarget = forcedTarget;
  2276. }
  2277. });
  2278. /**
  2279. * Create an action with the specified action and forced target
  2280. * @param {cc.Node} target
  2281. * @param {cc.FiniteTimeAction} action
  2282. */
  2283. cc.TargetedAction.create = function (target, action) {
  2284. var retObj = new cc.TargetedAction();
  2285. retObj.initWithTarget(target, action);
  2286. return retObj;
  2287. };