CCActionInterval.js 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534
  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. * <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. * @param {Number} d duration in seconds
  38. * @example
  39. * var actionInterval = new cc.ActionInterval(3);
  40. */
  41. cc.ActionInterval = cc.FiniteTimeAction.extend(/** @lends cc.ActionInterval# */{
  42. _elapsed:0,
  43. _firstTick:false,
  44. _easeList: null,
  45. _times:1,
  46. _repeatForever: false,
  47. _repeatMethod: false,//Compatible with repeat class, Discard after can be deleted
  48. _speed: 1,
  49. _speedMethod: false,//Compatible with speed class, Discard after can be deleted
  50. /**
  51. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  52. * @param {Number} d duration in seconds
  53. */
  54. ctor:function (d) {
  55. this._speed = 1;
  56. this._times = 1;
  57. this._repeatForever = false;
  58. this.MAX_VALUE = 2;
  59. this._repeatMethod = false;//Compatible with repeat class, Discard after can be deleted
  60. this._speedMethod = false;//Compatible with repeat class, Discard after can be deleted
  61. cc.FiniteTimeAction.prototype.ctor.call(this);
  62. d !== undefined && this.initWithDuration(d);
  63. },
  64. /**
  65. * How many seconds had elapsed since the actions started to run.
  66. * @return {Number}
  67. */
  68. getElapsed:function () {
  69. return this._elapsed;
  70. },
  71. /**
  72. * Initializes the action.
  73. * @param {Number} d duration in seconds
  74. * @return {Boolean}
  75. */
  76. initWithDuration:function (d) {
  77. this._duration = (d === 0) ? cc.FLT_EPSILON : d;
  78. // prevent division by 0
  79. // This comparison could be in step:, but it might decrease the performance
  80. // by 3% in heavy based action games.
  81. this._elapsed = 0;
  82. this._firstTick = true;
  83. return true;
  84. },
  85. /**
  86. * Returns true if the action has finished.
  87. * @return {Boolean}
  88. */
  89. isDone:function () {
  90. return (this._elapsed >= this._duration);
  91. },
  92. /**
  93. * Some additional parameters of cloning.
  94. * @param {cc.Action} action
  95. * @private
  96. */
  97. _cloneDecoration: function(action){
  98. action._repeatForever = this._repeatForever;
  99. action._speed = this._speed;
  100. action._times = this._times;
  101. action._easeList = this._easeList;
  102. action._speedMethod = this._speedMethod;
  103. action._repeatMethod = this._repeatMethod;
  104. },
  105. _reverseEaseList: function(action){
  106. if(this._easeList){
  107. action._easeList = [];
  108. for(var i=0; i<this._easeList.length; i++){
  109. action._easeList.push(this._easeList[i].reverse());
  110. }
  111. }
  112. },
  113. /**
  114. * Returns a new clone of the action.
  115. * @returns {cc.ActionInterval}
  116. */
  117. clone:function () {
  118. var action = new cc.ActionInterval(this._duration);
  119. this._cloneDecoration(action);
  120. return action;
  121. },
  122. /**
  123. * Implementation of ease motion.
  124. *
  125. * @example
  126. * //example
  127. * action.easeing(cc.easeIn(3.0));
  128. * @param {Object} easeObj
  129. * @returns {cc.ActionInterval}
  130. */
  131. easing: function (easeObj) {
  132. if (this._easeList)
  133. this._easeList.length = 0;
  134. else
  135. this._easeList = [];
  136. for (var i = 0; i < arguments.length; i++)
  137. this._easeList.push(arguments[i]);
  138. return this;
  139. },
  140. _computeEaseTime: function (dt) {
  141. var locList = this._easeList;
  142. if ((!locList) || (locList.length === 0))
  143. return dt;
  144. for (var i = 0, n = locList.length; i < n; i++)
  145. dt = locList[i].easing(dt);
  146. return dt;
  147. },
  148. /**
  149. * called every frame with it's delta time. <br />
  150. * DON'T override unless you know what you are doing.
  151. *
  152. * @param {Number} dt
  153. */
  154. step:function (dt) {
  155. if (this._firstTick) {
  156. this._firstTick = false;
  157. this._elapsed = 0;
  158. } else
  159. this._elapsed += dt;
  160. //this.update((1 > (this._elapsed / this._duration)) ? this._elapsed / this._duration : 1);
  161. //this.update(Math.max(0, Math.min(1, this._elapsed / Math.max(this._duration, cc.FLT_EPSILON))));
  162. var t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
  163. t = (1 > t ? t : 1);
  164. this.update(t > 0 ? t : 0);
  165. //Compatible with repeat class, Discard after can be deleted (this._repeatMethod)
  166. if(this._repeatMethod && this._times > 1 && this.isDone()){
  167. if(!this._repeatForever){
  168. this._times--;
  169. }
  170. //var diff = locInnerAction.getElapsed() - locInnerAction._duration;
  171. this.startWithTarget(this.target);
  172. // to prevent jerk. issue #390 ,1247
  173. //this._innerAction.step(0);
  174. //this._innerAction.step(diff);
  175. this.step(this._elapsed - this._duration);
  176. }
  177. },
  178. /**
  179. * Start this action with target.
  180. * @param {cc.Node} target
  181. */
  182. startWithTarget:function (target) {
  183. cc.Action.prototype.startWithTarget.call(this, target);
  184. this._elapsed = 0;
  185. this._firstTick = true;
  186. },
  187. /**
  188. * returns a reversed action. <br />
  189. * Will be overwrite.
  190. *
  191. * @return {null}
  192. */
  193. reverse:function () {
  194. cc.log("cc.IntervalAction: reverse not implemented.");
  195. return null;
  196. },
  197. /**
  198. * Set amplitude rate.
  199. * @warning It should be overridden in subclass.
  200. * @param {Number} amp
  201. */
  202. setAmplitudeRate:function (amp) {
  203. // Abstract class needs implementation
  204. cc.log("cc.ActionInterval.setAmplitudeRate(): it should be overridden in subclass.");
  205. },
  206. /**
  207. * Get amplitude rate.
  208. * @warning It should be overridden in subclass.
  209. * @return {Number} 0
  210. */
  211. getAmplitudeRate:function () {
  212. // Abstract class needs implementation
  213. cc.log("cc.ActionInterval.getAmplitudeRate(): it should be overridden in subclass.");
  214. return 0;
  215. },
  216. /**
  217. * Changes the speed of an action, making it take longer (speed>1)
  218. * or less (speed<1) time. <br/>
  219. * Useful to simulate 'slow motion' or 'fast forward' effect.
  220. *
  221. * @param speed
  222. * @returns {cc.Action}
  223. */
  224. speed: function(speed){
  225. if(speed <= 0){
  226. cc.log("The speed parameter error");
  227. return this;
  228. }
  229. this._speedMethod = true;//Compatible with repeat class, Discard after can be deleted
  230. this._speed *= speed;
  231. return this;
  232. },
  233. /**
  234. * Get this action speed.
  235. * @return {Number}
  236. */
  237. getSpeed: function(){
  238. return this._speed;
  239. },
  240. /**
  241. * Set this action speed.
  242. * @param {Number} speed
  243. * @returns {cc.ActionInterval}
  244. */
  245. setSpeed: function(speed){
  246. this._speed = speed;
  247. return this;
  248. },
  249. /**
  250. * Repeats an action a number of times.
  251. * To repeat an action forever use the CCRepeatForever action.
  252. * @param times
  253. * @returns {cc.ActionInterval}
  254. */
  255. repeat: function(times){
  256. times = Math.round(times);
  257. if(isNaN(times) || times < 1){
  258. cc.log("The repeat parameter error");
  259. return this;
  260. }
  261. this._repeatMethod = true;//Compatible with repeat class, Discard after can be deleted
  262. this._times *= times;
  263. return this;
  264. },
  265. /**
  266. * Repeats an action for ever. <br/>
  267. * To repeat the an action for a limited number of times use the Repeat action. <br/>
  268. * @returns {cc.ActionInterval}
  269. */
  270. repeatForever: function(){
  271. this._repeatMethod = true;//Compatible with repeat class, Discard after can be deleted
  272. this._times = this.MAX_VALUE;
  273. this._repeatForever = true;
  274. return this;
  275. }
  276. });
  277. /**
  278. * An interval action is an action that takes place within a certain period of time.
  279. * @function
  280. * @param {Number} d duration in seconds
  281. * @return {cc.ActionInterval}
  282. * @example
  283. * // example
  284. * var actionInterval = cc.actionInterval(3);
  285. */
  286. cc.actionInterval = function (d) {
  287. return new cc.ActionInterval(d);
  288. };
  289. /**
  290. * Please use cc.actionInterval instead.
  291. * An interval action is an action that takes place within a certain period of time.
  292. * @static
  293. * @deprecated since v3.0 <br /> Please use cc.actionInterval instead.
  294. * @param {Number} d duration in seconds
  295. * @return {cc.ActionInterval}
  296. */
  297. cc.ActionInterval.create = cc.actionInterval;
  298. /**
  299. * Runs actions sequentially, one after another.
  300. * @class
  301. * @extends cc.ActionInterval
  302. * @param {Array|cc.FiniteTimeAction} tempArray
  303. * @example
  304. * // create sequence with actions
  305. * var seq = new cc.Sequence(act1, act2);
  306. *
  307. * // create sequence with array
  308. * var seq = new cc.Sequence(actArray);
  309. */
  310. cc.Sequence = cc.ActionInterval.extend(/** @lends cc.Sequence# */{
  311. _actions:null,
  312. _split:null,
  313. _last:0,
  314. /**
  315. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  316. * Create an array of sequenceable actions.
  317. * @param {Array|cc.FiniteTimeAction} tempArray
  318. */
  319. ctor:function (tempArray) {
  320. cc.ActionInterval.prototype.ctor.call(this);
  321. this._actions = [];
  322. var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
  323. var last = paramArray.length - 1;
  324. if ((last >= 0) && (paramArray[last] == null))
  325. cc.log("parameters should not be ending with null in Javascript");
  326. if (last >= 0) {
  327. var prev = paramArray[0], action1;
  328. for (var i = 1; i < last; i++) {
  329. if (paramArray[i]) {
  330. action1 = prev;
  331. prev = cc.Sequence._actionOneTwo(action1, paramArray[i]);
  332. }
  333. }
  334. this.initWithTwoActions(prev, paramArray[last]);
  335. }
  336. },
  337. /**
  338. * Initializes the action <br/>
  339. * @param {cc.FiniteTimeAction} actionOne
  340. * @param {cc.FiniteTimeAction} actionTwo
  341. * @return {Boolean}
  342. */
  343. initWithTwoActions:function (actionOne, actionTwo) {
  344. if(!actionOne || !actionTwo)
  345. throw "cc.Sequence.initWithTwoActions(): arguments must all be non nil";
  346. var d = actionOne._duration + actionTwo._duration;
  347. this.initWithDuration(d);
  348. this._actions[0] = actionOne;
  349. this._actions[1] = actionTwo;
  350. return true;
  351. },
  352. /**
  353. * returns a new clone of the action
  354. * @returns {cc.Sequence}
  355. */
  356. clone:function () {
  357. var action = new cc.Sequence();
  358. this._cloneDecoration(action);
  359. action.initWithTwoActions(this._actions[0].clone(), this._actions[1].clone());
  360. return action;
  361. },
  362. /**
  363. * Start the action with target.
  364. * @param {cc.Node} target
  365. */
  366. startWithTarget:function (target) {
  367. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  368. this._split = this._actions[0]._duration / this._duration;
  369. this._last = -1;
  370. },
  371. /**
  372. * stop the action.
  373. */
  374. stop:function () {
  375. // Issue #1305
  376. if (this._last !== -1)
  377. this._actions[this._last].stop();
  378. cc.Action.prototype.stop.call(this);
  379. },
  380. /**
  381. * Called once per frame. Time is the number of seconds of a frame interval.
  382. * @param {Number} dt
  383. */
  384. update:function (dt) {
  385. dt = this._computeEaseTime(dt);
  386. var new_t, found = 0;
  387. var locSplit = this._split, locActions = this._actions, locLast = this._last;
  388. if (dt < locSplit) {
  389. // action[0]
  390. new_t = (locSplit !== 0) ? dt / locSplit : 1;
  391. if (found === 0 && locLast === 1) {
  392. // Reverse mode ?
  393. // XXX: Bug. this case doesn't contemplate when _last==-1, found=0 and in "reverse mode"
  394. // since it will require a hack to know if an action is on reverse mode or not.
  395. // "step" should be overriden, and the "reverseMode" value propagated to inner Sequences.
  396. locActions[1].update(0);
  397. locActions[1].stop();
  398. }
  399. } else {
  400. // action[1]
  401. found = 1;
  402. new_t = (locSplit === 1) ? 1 : (dt - locSplit) / (1 - locSplit);
  403. if (locLast === -1) {
  404. // action[0] was skipped, execute it.
  405. locActions[0].startWithTarget(this.target);
  406. locActions[0].update(1);
  407. locActions[0].stop();
  408. }
  409. if (!locLast) {
  410. // switching to action 1. stop action 0.
  411. locActions[0].update(1);
  412. locActions[0].stop();
  413. }
  414. }
  415. // Last action found and it is done.
  416. if (locLast === found && locActions[found].isDone())
  417. return;
  418. // Last action found and it is done
  419. if (locLast !== found)
  420. locActions[found].startWithTarget(this.target);
  421. locActions[found].update(new_t);
  422. this._last = found;
  423. },
  424. /**
  425. * Returns a reversed action.
  426. * @return {cc.Sequence}
  427. */
  428. reverse:function () {
  429. var action = cc.Sequence._actionOneTwo(this._actions[1].reverse(), this._actions[0].reverse());
  430. this._cloneDecoration(action);
  431. this._reverseEaseList(action);
  432. return action;
  433. }
  434. });
  435. /** helper constructor to create an array of sequenceable actions
  436. * @function
  437. * @param {Array|cc.FiniteTimeAction} tempArray
  438. * @return {cc.Sequence}
  439. * @example
  440. * // example
  441. * // create sequence with actions
  442. * var seq = cc.sequence(act1, act2);
  443. *
  444. * // create sequence with array
  445. * var seq = cc.sequence(actArray);
  446. */
  447. cc.sequence = function (/*Multiple Arguments*/tempArray) {
  448. var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
  449. if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null))
  450. cc.log("parameters should not be ending with null in Javascript");
  451. var prev = paramArray[0];
  452. for (var i = 1; i < paramArray.length; i++) {
  453. if (paramArray[i])
  454. prev = cc.Sequence._actionOneTwo(prev, paramArray[i]);
  455. }
  456. return prev;
  457. };
  458. /**
  459. * Please use cc.sequence instead.
  460. * helper constructor to create an array of sequenceable actions
  461. * @static
  462. * @deprecated since v3.0 <br /> Please use cc.sequence instead.
  463. * @param {Array|cc.FiniteTimeAction} tempArray
  464. * @return {cc.Sequence}
  465. */
  466. cc.Sequence.create = cc.sequence;
  467. /** creates the action
  468. * @param {cc.FiniteTimeAction} actionOne
  469. * @param {cc.FiniteTimeAction} actionTwo
  470. * @return {cc.Sequence}
  471. * @private
  472. */
  473. cc.Sequence._actionOneTwo = function (actionOne, actionTwo) {
  474. var sequence = new cc.Sequence();
  475. sequence.initWithTwoActions(actionOne, actionTwo);
  476. return sequence;
  477. };
  478. /**
  479. * Repeats an action a number of times.
  480. * To repeat an action forever use the CCRepeatForever action.
  481. * @class
  482. * @extends cc.ActionInterval
  483. * @param {cc.FiniteTimeAction} action
  484. * @param {Number} times
  485. * @example
  486. * var rep = new cc.Repeat(cc.sequence(jump2, jump1), 5);
  487. */
  488. cc.Repeat = cc.ActionInterval.extend(/** @lends cc.Repeat# */{
  489. _times:0,
  490. _total:0,
  491. _nextDt:0,
  492. _actionInstant:false,
  493. _innerAction:null, //CCFiniteTimeAction
  494. /**
  495. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  496. * Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30).
  497. * @param {cc.FiniteTimeAction} action
  498. * @param {Number} times
  499. */
  500. ctor: function (action, times) {
  501. cc.ActionInterval.prototype.ctor.call(this);
  502. times !== undefined && this.initWithAction(action, times);
  503. },
  504. /**
  505. * @param {cc.FiniteTimeAction} action
  506. * @param {Number} times
  507. * @return {Boolean}
  508. */
  509. initWithAction:function (action, times) {
  510. var duration = action._duration * times;
  511. if (this.initWithDuration(duration)) {
  512. this._times = times;
  513. this._innerAction = action;
  514. if (action instanceof cc.ActionInstant){
  515. this._actionInstant = true;
  516. this._times -= 1;
  517. }
  518. this._total = 0;
  519. return true;
  520. }
  521. return false;
  522. },
  523. /**
  524. * returns a new clone of the action
  525. * @returns {cc.Repeat}
  526. */
  527. clone:function () {
  528. var action = new cc.Repeat();
  529. this._cloneDecoration(action);
  530. action.initWithAction(this._innerAction.clone(), this._times);
  531. return action;
  532. },
  533. /**
  534. * Start the action with target.
  535. * @param {cc.Node} target
  536. */
  537. startWithTarget:function (target) {
  538. this._total = 0;
  539. this._nextDt = this._innerAction._duration / this._duration;
  540. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  541. this._innerAction.startWithTarget(target);
  542. },
  543. /**
  544. * stop the action
  545. */
  546. stop:function () {
  547. this._innerAction.stop();
  548. cc.Action.prototype.stop.call(this);
  549. },
  550. /**
  551. * Called once per frame. Time is the number of seconds of a frame interval.
  552. * @param {Number} dt
  553. */
  554. update:function (dt) {
  555. dt = this._computeEaseTime(dt);
  556. var locInnerAction = this._innerAction;
  557. var locDuration = this._duration;
  558. var locTimes = this._times;
  559. var locNextDt = this._nextDt;
  560. if (dt >= locNextDt) {
  561. while (dt > locNextDt && this._total < locTimes) {
  562. locInnerAction.update(1);
  563. this._total++;
  564. locInnerAction.stop();
  565. locInnerAction.startWithTarget(this.target);
  566. locNextDt += locInnerAction._duration / locDuration;
  567. this._nextDt = locNextDt;
  568. }
  569. // fix for issue #1288, incorrect end value of repeat
  570. if (dt >= 1.0 && this._total < locTimes)
  571. this._total++;
  572. // don't set a instant action back or update it, it has no use because it has no duration
  573. if (!this._actionInstant) {
  574. if (this._total === locTimes) {
  575. locInnerAction.update(1);
  576. locInnerAction.stop();
  577. } else {
  578. // issue #390 prevent jerk, use right update
  579. locInnerAction.update(dt - (locNextDt - locInnerAction._duration / locDuration));
  580. }
  581. }
  582. } else {
  583. locInnerAction.update((dt * locTimes) % 1.0);
  584. }
  585. },
  586. /**
  587. * Return true if the action has finished.
  588. * @return {Boolean}
  589. */
  590. isDone:function () {
  591. return this._total == this._times;
  592. },
  593. /**
  594. * returns a reversed action.
  595. * @return {cc.Repeat}
  596. */
  597. reverse:function () {
  598. var action = new cc.Repeat(this._innerAction.reverse(), this._times);
  599. this._cloneDecoration(action);
  600. this._reverseEaseList(action);
  601. return action;
  602. },
  603. /**
  604. * Set inner Action.
  605. * @param {cc.FiniteTimeAction} action
  606. */
  607. setInnerAction:function (action) {
  608. if (this._innerAction != action) {
  609. this._innerAction = action;
  610. }
  611. },
  612. /**
  613. * Get inner Action.
  614. * @return {cc.FiniteTimeAction}
  615. */
  616. getInnerAction:function () {
  617. return this._innerAction;
  618. }
  619. });
  620. /**
  621. * Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30)
  622. * @function
  623. * @param {cc.FiniteTimeAction} action
  624. * @param {Number} times
  625. * @return {cc.Repeat}
  626. * @example
  627. * // example
  628. * var rep = cc.repeat(cc.sequence(jump2, jump1), 5);
  629. */
  630. cc.repeat = function (action, times) {
  631. return new cc.Repeat(action, times);
  632. };
  633. /**
  634. * Please use cc.repeat instead
  635. * Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30)
  636. * @static
  637. * @deprecated since v3.0 <br /> Please use cc.repeat instead.
  638. * @param {cc.FiniteTimeAction} action
  639. * @param {Number} times
  640. * @return {cc.Repeat}
  641. */
  642. cc.Repeat.create = cc.repeat;
  643. /** Repeats an action for ever. <br/>
  644. * To repeat the an action for a limited number of times use the Repeat action. <br/>
  645. * @warning This action can't be Sequenceable because it is not an IntervalAction
  646. * @class
  647. * @extends cc.ActionInterval
  648. * @param {cc.FiniteTimeAction} action
  649. * @example
  650. * var rep = new cc.RepeatForever(cc.sequence(jump2, jump1), 5);
  651. */
  652. cc.RepeatForever = cc.ActionInterval.extend(/** @lends cc.RepeatForever# */{
  653. _innerAction:null, //CCActionInterval
  654. /**
  655. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  656. * Create a acton which repeat forever.
  657. * @param {cc.FiniteTimeAction} action
  658. */
  659. ctor:function (action) {
  660. cc.ActionInterval.prototype.ctor.call(this);
  661. this._innerAction = null;
  662. action && this.initWithAction(action);
  663. },
  664. /**
  665. * @param {cc.ActionInterval} action
  666. * @return {Boolean}
  667. */
  668. initWithAction:function (action) {
  669. if(!action)
  670. throw "cc.RepeatForever.initWithAction(): action must be non null";
  671. this._innerAction = action;
  672. return true;
  673. },
  674. /**
  675. * returns a new clone of the action
  676. * @returns {cc.RepeatForever}
  677. */
  678. clone:function () {
  679. var action = new cc.RepeatForever();
  680. this._cloneDecoration(action);
  681. action.initWithAction(this._innerAction.clone());
  682. return action;
  683. },
  684. /**
  685. * Start the action with target.
  686. * @param {cc.Node} target
  687. */
  688. startWithTarget:function (target) {
  689. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  690. this._innerAction.startWithTarget(target);
  691. },
  692. /**
  693. * called every frame with it's delta time. <br />
  694. * DON'T override unless you know what you are doing.
  695. * @param dt delta time in seconds
  696. */
  697. step:function (dt) {
  698. var locInnerAction = this._innerAction;
  699. locInnerAction.step(dt);
  700. if (locInnerAction.isDone()) {
  701. //var diff = locInnerAction.getElapsed() - locInnerAction._duration;
  702. locInnerAction.startWithTarget(this.target);
  703. // to prevent jerk. issue #390 ,1247
  704. //this._innerAction.step(0);
  705. //this._innerAction.step(diff);
  706. locInnerAction.step(locInnerAction.getElapsed() - locInnerAction._duration);
  707. }
  708. },
  709. /**
  710. * Return true if the action has finished.
  711. * @return {Boolean}
  712. */
  713. isDone:function () {
  714. return false;
  715. },
  716. /**
  717. * Returns a reversed action.
  718. * @return {cc.RepeatForever}
  719. */
  720. reverse:function () {
  721. var action = new cc.RepeatForever(this._innerAction.reverse());
  722. this._cloneDecoration(action);
  723. this._reverseEaseList(action);
  724. return action;
  725. },
  726. /**
  727. * Set inner action.
  728. * @param {cc.ActionInterval} action
  729. */
  730. setInnerAction:function (action) {
  731. if (this._innerAction != action) {
  732. this._innerAction = action;
  733. }
  734. },
  735. /**
  736. * Get inner action.
  737. * @return {cc.ActionInterval}
  738. */
  739. getInnerAction:function () {
  740. return this._innerAction;
  741. }
  742. });
  743. /**
  744. * Create a acton which repeat forever
  745. * @function
  746. * @param {cc.FiniteTimeAction} action
  747. * @return {cc.RepeatForever}
  748. * @example
  749. * // example
  750. * var repeat = cc.repeatForever(cc.rotateBy(1.0, 360));
  751. */
  752. cc.repeatForever = function (action) {
  753. return new cc.RepeatForever(action);
  754. };
  755. /**
  756. * Please use cc.repeatForever instead
  757. * Create a acton which repeat forever
  758. * @static
  759. * @deprecated since v3.0 <br /> Please use cc.repeatForever instead.
  760. * @param {cc.FiniteTimeAction} action
  761. * @return {cc.RepeatForever}
  762. * @param {Array|cc.FiniteTimeAction} tempArray
  763. * @example
  764. * var action = new cc.Spawn(cc.jumpBy(2, cc.p(300, 0), 50, 4), cc.rotateBy(2, 720));
  765. */
  766. cc.RepeatForever.create = cc.repeatForever;
  767. /** Spawn a new action immediately
  768. * @class
  769. * @extends cc.ActionInterval
  770. */
  771. cc.Spawn = cc.ActionInterval.extend(/** @lends cc.Spawn# */{
  772. _one:null,
  773. _two:null,
  774. /**
  775. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  776. * @param {Array|cc.FiniteTimeAction} tempArray
  777. */
  778. ctor:function (tempArray) {
  779. cc.ActionInterval.prototype.ctor.call(this);
  780. this._one = null;
  781. this._two = null;
  782. var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
  783. var last = paramArray.length - 1;
  784. if ((last >= 0) && (paramArray[last] == null))
  785. cc.log("parameters should not be ending with null in Javascript");
  786. if (last >= 0) {
  787. var prev = paramArray[0], action1;
  788. for (var i = 1; i < last; i++) {
  789. if (paramArray[i]) {
  790. action1 = prev;
  791. prev = cc.Spawn._actionOneTwo(action1, paramArray[i]);
  792. }
  793. }
  794. this.initWithTwoActions(prev, paramArray[last]);
  795. }
  796. },
  797. /** initializes the Spawn action with the 2 actions to spawn
  798. * @param {cc.FiniteTimeAction} action1
  799. * @param {cc.FiniteTimeAction} action2
  800. * @return {Boolean}
  801. */
  802. initWithTwoActions:function (action1, action2) {
  803. if(!action1 || !action2)
  804. throw "cc.Spawn.initWithTwoActions(): arguments must all be non null" ;
  805. var ret = false;
  806. var d1 = action1._duration;
  807. var d2 = action2._duration;
  808. if (this.initWithDuration(Math.max(d1, d2))) {
  809. this._one = action1;
  810. this._two = action2;
  811. if (d1 > d2) {
  812. this._two = cc.Sequence._actionOneTwo(action2, cc.delayTime(d1 - d2));
  813. } else if (d1 < d2) {
  814. this._one = cc.Sequence._actionOneTwo(action1, cc.delayTime(d2 - d1));
  815. }
  816. ret = true;
  817. }
  818. return ret;
  819. },
  820. /**
  821. * returns a new clone of the action
  822. * @returns {cc.Spawn}
  823. */
  824. clone:function () {
  825. var action = new cc.Spawn();
  826. this._cloneDecoration(action);
  827. action.initWithTwoActions(this._one.clone(), this._two.clone());
  828. return action;
  829. },
  830. /**
  831. * Start the action with target.
  832. * @param {cc.Node} target
  833. */
  834. startWithTarget:function (target) {
  835. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  836. this._one.startWithTarget(target);
  837. this._two.startWithTarget(target);
  838. },
  839. /**
  840. * Stop the action
  841. */
  842. stop:function () {
  843. this._one.stop();
  844. this._two.stop();
  845. cc.Action.prototype.stop.call(this);
  846. },
  847. /**
  848. * Called once per frame. Time is the number of seconds of a frame interval.
  849. * @param {Number} dt
  850. */
  851. update:function (dt) {
  852. dt = this._computeEaseTime(dt);
  853. if (this._one)
  854. this._one.update(dt);
  855. if (this._two)
  856. this._two.update(dt);
  857. },
  858. /**
  859. * Returns a reversed action.
  860. * @return {cc.Spawn}
  861. */
  862. reverse:function () {
  863. var action = cc.Spawn._actionOneTwo(this._one.reverse(), this._two.reverse());
  864. this._cloneDecoration(action);
  865. this._reverseEaseList(action);
  866. return action;
  867. }
  868. });
  869. /**
  870. * Create a spawn action which runs several actions in parallel.
  871. * @function
  872. * @param {Array|cc.FiniteTimeAction}tempArray
  873. * @return {cc.FiniteTimeAction}
  874. * @example
  875. * // example
  876. * var action = cc.spawn(cc.jumpBy(2, cc.p(300, 0), 50, 4), cc.rotateBy(2, 720));
  877. */
  878. cc.spawn = function (/*Multiple Arguments*/tempArray) {
  879. var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
  880. if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null))
  881. cc.log("parameters should not be ending with null in Javascript");
  882. var prev = paramArray[0];
  883. for (var i = 1; i < paramArray.length; i++) {
  884. if (paramArray[i] != null)
  885. prev = cc.Spawn._actionOneTwo(prev, paramArray[i]);
  886. }
  887. return prev;
  888. };
  889. /**
  890. * Please use cc.spawn instead.
  891. * Create a spawn action which runs several actions in parallel.
  892. * @static
  893. * @deprecated since v3.0 <br /> Please use cc.spawn instead.
  894. * @param {Array|cc.FiniteTimeAction}tempArray
  895. * @return {cc.FiniteTimeAction}
  896. */
  897. cc.Spawn.create = cc.spawn;
  898. /**
  899. * @param {cc.FiniteTimeAction} action1
  900. * @param {cc.FiniteTimeAction} action2
  901. * @return {cc.Spawn}
  902. * @private
  903. */
  904. cc.Spawn._actionOneTwo = function (action1, action2) {
  905. var pSpawn = new cc.Spawn();
  906. pSpawn.initWithTwoActions(action1, action2);
  907. return pSpawn;
  908. };
  909. /**
  910. * Rotates a cc.Node object to a certain angle by modifying it's.
  911. * rotation attribute. <br/>
  912. * The direction will be decided by the shortest angle.
  913. * @class
  914. * @extends cc.ActionInterval
  915. * @param {Number} duration duration in seconds
  916. * @param {Number} deltaAngleX deltaAngleX in degrees.
  917. * @param {Number} [deltaAngleY] deltaAngleY in degrees.
  918. * @example
  919. * var rotateTo = new cc.RotateTo(2, 61.0);
  920. */
  921. cc.RotateTo = cc.ActionInterval.extend(/** @lends cc.RotateTo# */{
  922. _dstAngleX:0,
  923. _startAngleX:0,
  924. _diffAngleX:0,
  925. _dstAngleY:0,
  926. _startAngleY:0,
  927. _diffAngleY:0,
  928. /**
  929. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  930. * Creates a RotateTo action with x and y rotation angles.
  931. * @param {Number} duration duration in seconds
  932. * @param {Number} deltaAngleX deltaAngleX in degrees.
  933. * @param {Number} [deltaAngleY] deltaAngleY in degrees.
  934. */
  935. ctor:function (duration, deltaAngleX, deltaAngleY) {
  936. cc.ActionInterval.prototype.ctor.call(this);
  937. deltaAngleX !== undefined && this.initWithDuration(duration, deltaAngleX, deltaAngleY);
  938. },
  939. /**
  940. * Initializes the action.
  941. * @param {Number} duration
  942. * @param {Number} deltaAngleX
  943. * @param {Number} deltaAngleY
  944. * @return {Boolean}
  945. */
  946. initWithDuration:function (duration, deltaAngleX, deltaAngleY) {
  947. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  948. this._dstAngleX = deltaAngleX || 0;
  949. this._dstAngleY = deltaAngleY || this._dstAngleX;
  950. return true;
  951. }
  952. return false;
  953. },
  954. /**
  955. * returns a new clone of the action
  956. * @returns {cc.RotateTo}
  957. */
  958. clone:function () {
  959. var action = new cc.RotateTo();
  960. this._cloneDecoration(action);
  961. action.initWithDuration(this._duration, this._dstAngleX, this._dstAngleY);
  962. return action;
  963. },
  964. /**
  965. * Start the action with target.
  966. * @param {cc.Node} target
  967. */
  968. startWithTarget:function (target) {
  969. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  970. // Calculate X
  971. var locStartAngleX = target.rotationX % 360.0;
  972. var locDiffAngleX = this._dstAngleX - locStartAngleX;
  973. if (locDiffAngleX > 180)
  974. locDiffAngleX -= 360;
  975. if (locDiffAngleX < -180)
  976. locDiffAngleX += 360;
  977. this._startAngleX = locStartAngleX;
  978. this._diffAngleX = locDiffAngleX;
  979. // Calculate Y It's duplicated from calculating X since the rotation wrap should be the same
  980. this._startAngleY = target.rotationY % 360.0;
  981. var locDiffAngleY = this._dstAngleY - this._startAngleY;
  982. if (locDiffAngleY > 180)
  983. locDiffAngleY -= 360;
  984. if (locDiffAngleY < -180)
  985. locDiffAngleY += 360;
  986. this._diffAngleY = locDiffAngleY;
  987. },
  988. /**
  989. * RotateTo reverse not implemented.
  990. * Will be overridden.
  991. */
  992. reverse:function () {
  993. cc.log("cc.RotateTo.reverse(): it should be overridden in subclass.");
  994. },
  995. /**
  996. * Called once per frame. Time is the number of seconds of a frame interval.
  997. * @param {Number} dt
  998. */
  999. update:function (dt) {
  1000. dt = this._computeEaseTime(dt);
  1001. if (this.target) {
  1002. this.target.rotationX = this._startAngleX + this._diffAngleX * dt;
  1003. this.target.rotationY = this._startAngleY + this._diffAngleY * dt;
  1004. }
  1005. }
  1006. });
  1007. /**
  1008. * Creates a RotateTo action with separate rotation angles.
  1009. * To specify the angle of rotation.
  1010. * @function
  1011. * @param {Number} duration duration in seconds
  1012. * @param {Number} deltaAngleX deltaAngleX in degrees.
  1013. * @param {Number} [deltaAngleY] deltaAngleY in degrees.
  1014. * @return {cc.RotateTo}
  1015. * @example
  1016. * // example
  1017. * var rotateTo = cc.rotateTo(2, 61.0);
  1018. */
  1019. cc.rotateTo = function (duration, deltaAngleX, deltaAngleY) {
  1020. return new cc.RotateTo(duration, deltaAngleX, deltaAngleY);
  1021. };
  1022. /**
  1023. * Please use cc.rotateTo instead
  1024. * Creates a RotateTo action with separate rotation angles.
  1025. * To specify the angle of rotation.
  1026. * @static
  1027. * @deprecated since v3.0 <br /> Please use cc.rotateTo instead.
  1028. * @param {Number} duration duration in seconds
  1029. * @param {Number} deltaAngleX deltaAngleX in degrees.
  1030. * @param {Number} [deltaAngleY] deltaAngleY in degrees.
  1031. * @return {cc.RotateTo}
  1032. */
  1033. cc.RotateTo.create = cc.rotateTo;
  1034. /**
  1035. * Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
  1036. * Relative to its properties to modify.
  1037. * @class
  1038. * @extends cc.ActionInterval
  1039. * @param {Number} duration duration in seconds
  1040. * @param {Number} deltaAngleX deltaAngleX in degrees
  1041. * @param {Number} [deltaAngleY] deltaAngleY in degrees
  1042. * @example
  1043. * var actionBy = new cc.RotateBy(2, 360);
  1044. */
  1045. cc.RotateBy = cc.ActionInterval.extend(/** @lends cc.RotateBy# */{
  1046. _angleX:0,
  1047. _startAngleX:0,
  1048. _angleY:0,
  1049. _startAngleY:0,
  1050. /**
  1051. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1052. * @param {Number} duration duration in seconds
  1053. * @param {Number} deltaAngleX deltaAngleX in degrees
  1054. * @param {Number} [deltaAngleY] deltaAngleY in degrees
  1055. */
  1056. ctor: function (duration, deltaAngleX, deltaAngleY) {
  1057. cc.ActionInterval.prototype.ctor.call(this);
  1058. deltaAngleX !== undefined && this.initWithDuration(duration, deltaAngleX, deltaAngleY);
  1059. },
  1060. /**
  1061. * Initializes the action.
  1062. * @param {Number} duration duration in seconds
  1063. * @param {Number} deltaAngleX deltaAngleX in degrees
  1064. * @param {Number} [deltaAngleY=] deltaAngleY in degrees
  1065. * @return {Boolean}
  1066. */
  1067. initWithDuration:function (duration, deltaAngleX, deltaAngleY) {
  1068. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1069. this._angleX = deltaAngleX || 0;
  1070. this._angleY = deltaAngleY || this._angleX;
  1071. return true;
  1072. }
  1073. return false;
  1074. },
  1075. /**
  1076. * returns a new clone of the action
  1077. * @returns {cc.RotateBy}
  1078. */
  1079. clone:function () {
  1080. var action = new cc.RotateBy();
  1081. this._cloneDecoration(action);
  1082. action.initWithDuration(this._duration, this._angleX, this._angleY);
  1083. return action;
  1084. },
  1085. /**
  1086. * Start the action with target.
  1087. * @param {cc.Node} target
  1088. */
  1089. startWithTarget:function (target) {
  1090. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1091. this._startAngleX = target.rotationX;
  1092. this._startAngleY = target.rotationY;
  1093. },
  1094. /**
  1095. * Called once per frame. Time is the number of seconds of a frame interval.
  1096. * @param {Number} dt
  1097. */
  1098. update:function (dt) {
  1099. dt = this._computeEaseTime(dt);
  1100. if (this.target) {
  1101. this.target.rotationX = this._startAngleX + this._angleX * dt;
  1102. this.target.rotationY = this._startAngleY + this._angleY * dt;
  1103. }
  1104. },
  1105. /**
  1106. * Returns a reversed action.
  1107. * @return {cc.RotateBy}
  1108. */
  1109. reverse:function () {
  1110. var action = new cc.RotateBy(this._duration, -this._angleX, -this._angleY);
  1111. this._cloneDecoration(action);
  1112. this._reverseEaseList(action);
  1113. return action;
  1114. }
  1115. });
  1116. /**
  1117. * Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
  1118. * Relative to its properties to modify.
  1119. * @function
  1120. * @param {Number} duration duration in seconds
  1121. * @param {Number} deltaAngleX deltaAngleX in degrees
  1122. * @param {Number} [deltaAngleY] deltaAngleY in degrees
  1123. * @return {cc.RotateBy}
  1124. * @example
  1125. * // example
  1126. * var actionBy = cc.rotateBy(2, 360);
  1127. */
  1128. cc.rotateBy = function (duration, deltaAngleX, deltaAngleY) {
  1129. return new cc.RotateBy(duration, deltaAngleX, deltaAngleY);
  1130. };
  1131. /**
  1132. * Please use cc.rotateBy instead.
  1133. * Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
  1134. * Relative to its properties to modify.
  1135. * @static
  1136. * @deprecated since v3.0 <br /> Please use cc.rotateBy instead.
  1137. * @param {Number} duration duration in seconds
  1138. * @param {Number} deltaAngleX deltaAngleX in degrees
  1139. * @param {Number} [deltaAngleY] deltaAngleY in degrees
  1140. * @return {cc.RotateBy}
  1141. */
  1142. cc.RotateBy.create = cc.rotateBy;
  1143. /**
  1144. * <p>
  1145. * Moves a CCNode object x,y pixels by modifying it's position attribute. <br/>
  1146. * x and y are relative to the position of the object. <br/>
  1147. * Several CCMoveBy actions can be concurrently called, and the resulting <br/>
  1148. * movement will be the sum of individual movements.
  1149. * </p>
  1150. * @class
  1151. * @extends cc.ActionInterval
  1152. * @param {Number} duration duration in seconds
  1153. * @param {cc.Point|Number} deltaPos
  1154. * @param {Number} [deltaY]
  1155. * @example
  1156. * var actionTo = cc.moveBy(2, cc.p(windowSize.width - 40, windowSize.height - 40));
  1157. */
  1158. cc.MoveBy = cc.ActionInterval.extend(/** @lends cc.MoveBy# */{
  1159. _positionDelta:null,
  1160. _startPosition:null,
  1161. _previousPosition:null,
  1162. /**
  1163. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1164. * @param {Number} duration duration in seconds
  1165. * @param {cc.Point|Number} deltaPos
  1166. * @param {Number} [deltaY]
  1167. */
  1168. ctor:function (duration, deltaPos, deltaY) {
  1169. cc.ActionInterval.prototype.ctor.call(this);
  1170. this._positionDelta = cc.p(0, 0);
  1171. this._startPosition = cc.p(0, 0);
  1172. this._previousPosition = cc.p(0, 0);
  1173. deltaPos !== undefined && this.initWithDuration(duration, deltaPos, deltaY);
  1174. },
  1175. /**
  1176. * Initializes the action.
  1177. * @param {Number} duration duration in seconds
  1178. * @param {cc.Point} position
  1179. * @param {Number} [y]
  1180. * @return {Boolean}
  1181. */
  1182. initWithDuration:function (duration, position, y) {
  1183. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1184. if(position.x !== undefined) {
  1185. y = position.y;
  1186. position = position.x;
  1187. }
  1188. this._positionDelta.x = position;
  1189. this._positionDelta.y = y;
  1190. return true;
  1191. }
  1192. return false;
  1193. },
  1194. /**
  1195. * returns a new clone of the action
  1196. * @returns {cc.MoveBy}
  1197. */
  1198. clone:function () {
  1199. var action = new cc.MoveBy();
  1200. this._cloneDecoration(action);
  1201. action.initWithDuration(this._duration, this._positionDelta);
  1202. return action;
  1203. },
  1204. /**
  1205. * Start the action with target.
  1206. * @param {cc.Node} target
  1207. */
  1208. startWithTarget:function (target) {
  1209. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1210. var locPosX = target.getPositionX();
  1211. var locPosY = target.getPositionY();
  1212. this._previousPosition.x = locPosX;
  1213. this._previousPosition.y = locPosY;
  1214. this._startPosition.x = locPosX;
  1215. this._startPosition.y = locPosY;
  1216. },
  1217. /**
  1218. * Called once per frame. Time is the number of seconds of a frame interval.
  1219. * @param {Number} dt
  1220. */
  1221. update:function (dt) {
  1222. dt = this._computeEaseTime(dt);
  1223. if (this.target) {
  1224. var x = this._positionDelta.x * dt;
  1225. var y = this._positionDelta.y * dt;
  1226. var locStartPosition = this._startPosition;
  1227. if (cc.ENABLE_STACKABLE_ACTIONS) {
  1228. var targetX = this.target.getPositionX();
  1229. var targetY = this.target.getPositionY();
  1230. var locPreviousPosition = this._previousPosition;
  1231. locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
  1232. locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
  1233. x = x + locStartPosition.x;
  1234. y = y + locStartPosition.y;
  1235. locPreviousPosition.x = x;
  1236. locPreviousPosition.y = y;
  1237. this.target.setPosition(x, y);
  1238. } else {
  1239. this.target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
  1240. }
  1241. }
  1242. },
  1243. /**
  1244. * MoveTo reverse is not implemented
  1245. * @return {cc.MoveBy}
  1246. */
  1247. reverse:function () {
  1248. var action = new cc.MoveBy(this._duration, cc.p(-this._positionDelta.x, -this._positionDelta.y));
  1249. this._cloneDecoration(action);
  1250. this._reverseEaseList(action);
  1251. return action;
  1252. }
  1253. });
  1254. /**
  1255. * Create the action.
  1256. * Relative to its coordinate moves a certain distance.
  1257. * @function
  1258. * @param {Number} duration duration in seconds
  1259. * @param {cc.Point|Number} deltaPos
  1260. * @param {Number} deltaY
  1261. * @return {cc.MoveBy}
  1262. * @example
  1263. * // example
  1264. * var actionTo = cc.moveBy(2, cc.p(windowSize.width - 40, windowSize.height - 40));
  1265. */
  1266. cc.moveBy = function (duration, deltaPos, deltaY) {
  1267. return new cc.MoveBy(duration, deltaPos, deltaY);
  1268. };
  1269. /**
  1270. * Please use cc.moveBy instead.
  1271. * Relative to its coordinate moves a certain distance.
  1272. * @static
  1273. * @deprecated since v3.0 please use cc.moveBy instead.
  1274. * @param {Number} duration duration in seconds
  1275. * @param {cc.Point|Number} deltaPos
  1276. * @param {Number} deltaY
  1277. * @return {cc.MoveBy}
  1278. */
  1279. cc.MoveBy.create = cc.moveBy;
  1280. /**
  1281. * Moves a CCNode object to the position x,y. x and y are absolute coordinates by modifying it's position attribute. <br/>
  1282. * Several CCMoveTo actions can be concurrently called, and the resulting <br/>
  1283. * movement will be the sum of individual movements.
  1284. * @class
  1285. * @extends cc.MoveBy
  1286. * @param {Number} duration duration in seconds
  1287. * @param {cc.Point|Number} position
  1288. * @param {Number} y
  1289. * @example
  1290. * var actionBy = new cc.MoveTo(2, cc.p(80, 80));
  1291. */
  1292. cc.MoveTo = cc.MoveBy.extend(/** @lends cc.MoveTo# */{
  1293. _endPosition:null,
  1294. /**
  1295. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1296. * @param {Number} duration duration in seconds
  1297. * @param {cc.Point|Number} position
  1298. * @param {Number} y
  1299. */
  1300. ctor:function (duration, position, y) {
  1301. cc.MoveBy.prototype.ctor.call(this);
  1302. this._endPosition = cc.p(0, 0);
  1303. position !== undefined && this.initWithDuration(duration, position, y);
  1304. },
  1305. /**
  1306. * Initializes the action.
  1307. * @param {Number} duration duration in seconds
  1308. * @param {cc.Point} position
  1309. * @param {Number} y
  1310. * @return {Boolean}
  1311. */
  1312. initWithDuration:function (duration, position, y) {
  1313. if (cc.MoveBy.prototype.initWithDuration.call(this, duration, position, y)) {
  1314. if(position.x !== undefined) {
  1315. y = position.y;
  1316. position = position.x;
  1317. }
  1318. this._endPosition.x = position;
  1319. this._endPosition.y = y;
  1320. return true;
  1321. }
  1322. return false;
  1323. },
  1324. /**
  1325. * returns a new clone of the action
  1326. * @returns {cc.MoveTo}
  1327. */
  1328. clone:function () {
  1329. var action = new cc.MoveTo();
  1330. this._cloneDecoration(action);
  1331. action.initWithDuration(this._duration, this._endPosition);
  1332. return action;
  1333. },
  1334. /**
  1335. * Start the action with target.
  1336. * @param {cc.Node} target
  1337. */
  1338. startWithTarget:function (target) {
  1339. cc.MoveBy.prototype.startWithTarget.call(this, target);
  1340. this._positionDelta.x = this._endPosition.x - target.getPositionX();
  1341. this._positionDelta.y = this._endPosition.y - target.getPositionY();
  1342. }
  1343. });
  1344. /**
  1345. * Create new action.
  1346. * Moving to the specified coordinates.
  1347. * @function
  1348. * @param {Number} duration duration in seconds
  1349. * @param {cc.Point} position
  1350. * @param {Number} y
  1351. * @return {cc.MoveBy}
  1352. * @example
  1353. * // example
  1354. * var actionBy = cc.moveTo(2, cc.p(80, 80));
  1355. */
  1356. cc.moveTo = function (duration, position, y) {
  1357. return new cc.MoveTo(duration, position, y);
  1358. };
  1359. /**
  1360. * Please use cc.moveTo instead.
  1361. * Moving to the specified coordinates.
  1362. * @static
  1363. * @deprecated since v3.0 <br /> Please use cc.moveTo instead.
  1364. * @param {Number} duration duration in seconds
  1365. * @param {cc.Point} position
  1366. * @param {Number} y
  1367. * @return {cc.MoveBy}
  1368. */
  1369. cc.MoveTo.create = cc.moveTo;
  1370. /**
  1371. * Skews a cc.Node object to given angles by modifying it's skewX and skewY attributes
  1372. * @class
  1373. * @extends cc.ActionInterval
  1374. * @param {Number} t time in seconds
  1375. * @param {Number} sx
  1376. * @param {Number} sy
  1377. * @example
  1378. * var actionTo = new cc.SkewTo(2, 37.2, -37.2);
  1379. */
  1380. cc.SkewTo = cc.ActionInterval.extend(/** @lends cc.SkewTo# */{
  1381. _skewX:0,
  1382. _skewY:0,
  1383. _startSkewX:0,
  1384. _startSkewY:0,
  1385. _endSkewX:0,
  1386. _endSkewY:0,
  1387. _deltaX:0,
  1388. _deltaY:0,
  1389. /**
  1390. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1391. * @param {Number} t time in seconds
  1392. * @param {Number} sx
  1393. * @param {Number} sy
  1394. */
  1395. ctor: function (t, sx, sy) {
  1396. cc.ActionInterval.prototype.ctor.call(this);
  1397. sy !== undefined && this.initWithDuration(t, sx, sy);
  1398. },
  1399. /**
  1400. * Initializes the action.
  1401. * @param {Number} t time in seconds
  1402. * @param {Number} sx
  1403. * @param {Number} sy
  1404. * @return {Boolean}
  1405. */
  1406. initWithDuration:function (t, sx, sy) {
  1407. var ret = false;
  1408. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  1409. this._endSkewX = sx;
  1410. this._endSkewY = sy;
  1411. ret = true;
  1412. }
  1413. return ret;
  1414. },
  1415. /**
  1416. * returns a new clone of the action
  1417. * @returns {cc.SkewTo}
  1418. */
  1419. clone:function () {
  1420. var action = new cc.SkewTo();
  1421. this._cloneDecoration(action);
  1422. action.initWithDuration(this._duration, this._endSkewX, this._endSkewY);
  1423. return action;
  1424. },
  1425. /**
  1426. * Start the action with target.
  1427. * @param {cc.Node} target
  1428. */
  1429. startWithTarget:function (target) {
  1430. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1431. this._startSkewX = target.skewX % 180;
  1432. this._deltaX = this._endSkewX - this._startSkewX;
  1433. if (this._deltaX > 180)
  1434. this._deltaX -= 360;
  1435. if (this._deltaX < -180)
  1436. this._deltaX += 360;
  1437. this._startSkewY = target.skewY % 360;
  1438. this._deltaY = this._endSkewY - this._startSkewY;
  1439. if (this._deltaY > 180)
  1440. this._deltaY -= 360;
  1441. if (this._deltaY < -180)
  1442. this._deltaY += 360;
  1443. },
  1444. /**
  1445. * Called once per frame. Time is the number of seconds of a frame interval.
  1446. * @param {Number} dt
  1447. */
  1448. update:function (dt) {
  1449. dt = this._computeEaseTime(dt);
  1450. this.target.skewX = this._startSkewX + this._deltaX * dt;
  1451. this.target.skewY = this._startSkewY + this._deltaY * dt;
  1452. }
  1453. });
  1454. /**
  1455. * Create new action.
  1456. * Skews a cc.Node object to given angles by modifying it's skewX and skewY attributes.
  1457. * Changes to the specified value.
  1458. * @function
  1459. * @param {Number} t time in seconds
  1460. * @param {Number} sx
  1461. * @param {Number} sy
  1462. * @return {cc.SkewTo}
  1463. * @example
  1464. * // example
  1465. * var actionTo = cc.skewTo(2, 37.2, -37.2);
  1466. */
  1467. cc.skewTo = function (t, sx, sy) {
  1468. return new cc.SkewTo(t, sx, sy);
  1469. };
  1470. /**
  1471. * Please use cc.skewTo instead.
  1472. * Skews a cc.Node object to given angles by modifying it's skewX and skewY attributes。
  1473. * Changes to the specified value.
  1474. * @static
  1475. * @deprecated since v3.0 <br /> Please use cc.skewTo instead.
  1476. * @param {Number} t time in seconds
  1477. * @param {Number} sx
  1478. * @param {Number} sy
  1479. * @return {cc.SkewTo}
  1480. */
  1481. cc.SkewTo.create = cc.skewTo;
  1482. /**
  1483. * Skews a cc.Node object by skewX and skewY degrees.
  1484. * Relative to its attribute modification.
  1485. * @class
  1486. * @extends cc.SkewTo
  1487. * @param {Number} t time in seconds
  1488. * @param {Number} sx skew in degrees for X axis
  1489. * @param {Number} sy skew in degrees for Y axis
  1490. */
  1491. cc.SkewBy = cc.SkewTo.extend(/** @lends cc.SkewBy# */{
  1492. /**
  1493. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1494. * @param {Number} t time in seconds
  1495. * @param {Number} sx skew in degrees for X axis
  1496. * @param {Number} sy skew in degrees for Y axis
  1497. */
  1498. ctor: function(t, sx, sy) {
  1499. cc.SkewTo.prototype.ctor.call(this);
  1500. sy !== undefined && this.initWithDuration(t, sx, sy);
  1501. },
  1502. /**
  1503. * Initializes the action.
  1504. * @param {Number} t time in seconds
  1505. * @param {Number} deltaSkewX skew in degrees for X axis
  1506. * @param {Number} deltaSkewY skew in degrees for Y axis
  1507. * @return {Boolean}
  1508. */
  1509. initWithDuration:function (t, deltaSkewX, deltaSkewY) {
  1510. var ret = false;
  1511. if (cc.SkewTo.prototype.initWithDuration.call(this, t, deltaSkewX, deltaSkewY)) {
  1512. this._skewX = deltaSkewX;
  1513. this._skewY = deltaSkewY;
  1514. ret = true;
  1515. }
  1516. return ret;
  1517. },
  1518. /**
  1519. * returns a new clone of the action
  1520. * @returns {cc.SkewBy}
  1521. */
  1522. clone:function () {
  1523. var action = new cc.SkewBy();
  1524. this._cloneDecoration(action);
  1525. action.initWithDuration(this._duration, this._skewX, this._skewY);
  1526. return action;
  1527. },
  1528. /**
  1529. * Start the action width target.
  1530. * @param {cc.Node} target
  1531. */
  1532. startWithTarget:function (target) {
  1533. cc.SkewTo.prototype.startWithTarget.call(this, target);
  1534. this._deltaX = this._skewX;
  1535. this._deltaY = this._skewY;
  1536. this._endSkewX = this._startSkewX + this._deltaX;
  1537. this._endSkewY = this._startSkewY + this._deltaY;
  1538. },
  1539. /**
  1540. * Returns a reversed action.
  1541. * @return {cc.SkewBy}
  1542. */
  1543. reverse:function () {
  1544. var action = new cc.SkewBy(this._duration, -this._skewX, -this._skewY);
  1545. this._cloneDecoration(action);
  1546. this._reverseEaseList(action);
  1547. return action;
  1548. }
  1549. });
  1550. /**
  1551. * Skews a cc.Node object by skewX and skewY degrees. <br />
  1552. * Relative to its attribute modification.
  1553. * @function
  1554. * @param {Number} t time in seconds
  1555. * @param {Number} sx sx skew in degrees for X axis
  1556. * @param {Number} sy sy skew in degrees for Y axis
  1557. * @return {cc.SkewBy}
  1558. * @example
  1559. * // example
  1560. * var actionBy = cc.skewBy(2, 0, -90);
  1561. */
  1562. cc.skewBy = function (t, sx, sy) {
  1563. return new cc.SkewBy(t, sx, sy);
  1564. };
  1565. /**
  1566. * Please use cc.skewBy instead. <br />
  1567. * Skews a cc.Node object by skewX and skewY degrees. <br />
  1568. * Relative to its attribute modification.
  1569. * @static
  1570. * @deprecated since v3.0 please use cc.skewBy instead.
  1571. * @param {Number} t time in seconds
  1572. * @param {Number} sx sx skew in degrees for X axis
  1573. * @param {Number} sy sy skew in degrees for Y axis
  1574. * @return {cc.SkewBy}
  1575. */
  1576. cc.SkewBy.create = cc.skewBy;
  1577. /**
  1578. * Moves a cc.Node object simulating a parabolic jump movement by modifying it's position attribute.
  1579. * Relative to its movement.
  1580. * @class
  1581. * @extends cc.ActionInterval
  1582. * @param {Number} duration
  1583. * @param {cc.Point|Number} position
  1584. * @param {Number} [y]
  1585. * @param {Number} height
  1586. * @param {Number} jumps
  1587. * @example
  1588. * var actionBy = new cc.JumpBy(2, cc.p(300, 0), 50, 4);
  1589. * var actionBy = new cc.JumpBy(2, 300, 0, 50, 4);
  1590. */
  1591. cc.JumpBy = cc.ActionInterval.extend(/** @lends cc.JumpBy# */{
  1592. _startPosition:null,
  1593. _delta:null,
  1594. _height:0,
  1595. _jumps:0,
  1596. _previousPosition:null,
  1597. /**
  1598. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1599. * @param {Number} duration
  1600. * @param {cc.Point|Number} position
  1601. * @param {Number} [y]
  1602. * @param {Number} height
  1603. * @param {Number} jumps
  1604. */
  1605. ctor:function (duration, position, y, height, jumps) {
  1606. cc.ActionInterval.prototype.ctor.call(this);
  1607. this._startPosition = cc.p(0, 0);
  1608. this._previousPosition = cc.p(0, 0);
  1609. this._delta = cc.p(0, 0);
  1610. height !== undefined && this.initWithDuration(duration, position, y, height, jumps);
  1611. },
  1612. /**
  1613. * Initializes the action.
  1614. * @param {Number} duration
  1615. * @param {cc.Point|Number} position
  1616. * @param {Number} [y]
  1617. * @param {Number} height
  1618. * @param {Number} jumps
  1619. * @return {Boolean}
  1620. * @example
  1621. * actionBy.initWithDuration(2, cc.p(300, 0), 50, 4);
  1622. * actionBy.initWithDuration(2, 300, 0, 50, 4);
  1623. */
  1624. initWithDuration:function (duration, position, y, height, jumps) {
  1625. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  1626. if (jumps === undefined) {
  1627. jumps = height;
  1628. height = y;
  1629. y = position.y;
  1630. position = position.x;
  1631. }
  1632. this._delta.x = position;
  1633. this._delta.y = y;
  1634. this._height = height;
  1635. this._jumps = jumps;
  1636. return true;
  1637. }
  1638. return false;
  1639. },
  1640. /**
  1641. * returns a new clone of the action
  1642. * @returns {cc.JumpBy}
  1643. */
  1644. clone:function () {
  1645. var action = new cc.JumpBy();
  1646. this._cloneDecoration(action);
  1647. action.initWithDuration(this._duration, this._delta, this._height, this._jumps);
  1648. return action;
  1649. },
  1650. /**
  1651. * Start the action with target.
  1652. * @param {cc.Node} target
  1653. */
  1654. startWithTarget:function (target) {
  1655. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1656. var locPosX = target.getPositionX();
  1657. var locPosY = target.getPositionY();
  1658. this._previousPosition.x = locPosX;
  1659. this._previousPosition.y = locPosY;
  1660. this._startPosition.x = locPosX;
  1661. this._startPosition.y = locPosY;
  1662. },
  1663. /**
  1664. * Called once per frame. Time is the number of seconds of a frame interval.
  1665. * @param {Number} dt
  1666. */
  1667. update:function (dt) {
  1668. dt = this._computeEaseTime(dt);
  1669. if (this.target) {
  1670. var frac = dt * this._jumps % 1.0;
  1671. var y = this._height * 4 * frac * (1 - frac);
  1672. y += this._delta.y * dt;
  1673. var x = this._delta.x * dt;
  1674. var locStartPosition = this._startPosition;
  1675. if (cc.ENABLE_STACKABLE_ACTIONS) {
  1676. var targetX = this.target.getPositionX();
  1677. var targetY = this.target.getPositionY();
  1678. var locPreviousPosition = this._previousPosition;
  1679. locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
  1680. locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
  1681. x = x + locStartPosition.x;
  1682. y = y + locStartPosition.y;
  1683. locPreviousPosition.x = x;
  1684. locPreviousPosition.y = y;
  1685. this.target.setPosition(x, y);
  1686. } else {
  1687. this.target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
  1688. }
  1689. }
  1690. },
  1691. /**
  1692. * Returns a reversed action.
  1693. * @return {cc.JumpBy}
  1694. */
  1695. reverse:function () {
  1696. var action = new cc.JumpBy(this._duration, cc.p(-this._delta.x, -this._delta.y), this._height, this._jumps);
  1697. this._cloneDecoration(action);
  1698. this._reverseEaseList(action);
  1699. return action;
  1700. }
  1701. });
  1702. /**
  1703. * Moves a cc.Node object simulating a parabolic jump movement by modifying it's position attribute.
  1704. * Relative to its movement.
  1705. * @function
  1706. * @param {Number} duration
  1707. * @param {cc.Point|Number} position
  1708. * @param {Number} [y]
  1709. * @param {Number} height
  1710. * @param {Number} jumps
  1711. * @return {cc.JumpBy}
  1712. * @example
  1713. * // example
  1714. * var actionBy = cc.jumpBy(2, cc.p(300, 0), 50, 4);
  1715. * var actionBy = cc.jumpBy(2, 300, 0, 50, 4);
  1716. */
  1717. cc.jumpBy = function (duration, position, y, height, jumps) {
  1718. return new cc.JumpBy(duration, position, y, height, jumps);
  1719. };
  1720. /**
  1721. * Please use cc.jumpBy instead. <br />
  1722. * Moves a cc.Node object simulating a parabolic jump movement by modifying it's position attribute. <br />
  1723. * Relative to its movement.
  1724. * @static
  1725. * @deprecated since v3.0 please use cc.jumpBy instead.
  1726. * @param {Number} duration
  1727. * @param {cc.Point|Number} position
  1728. * @param {Number} [y]
  1729. * @param {Number} height
  1730. * @param {Number} jumps
  1731. * @return {cc.JumpBy}
  1732. */
  1733. cc.JumpBy.create = cc.jumpBy;
  1734. /**
  1735. * Moves a cc.Node object to a parabolic position simulating a jump movement by modifying it's position attribute. <br />
  1736. * Jump to the specified location.
  1737. * @class
  1738. * @extends cc.JumpBy
  1739. * @param {Number} duration
  1740. * @param {cc.Point|Number} position
  1741. * @param {Number} [y]
  1742. * @param {Number} height
  1743. * @param {Number} jumps
  1744. * @example
  1745. * var actionTo = new cc.JumpTo(2, cc.p(300, 0), 50, 4);
  1746. * var actionTo = new cc.JumpTo(2, 300, 0, 50, 4);
  1747. */
  1748. cc.JumpTo = cc.JumpBy.extend(/** @lends cc.JumpTo# */{
  1749. _endPosition:null,
  1750. /**
  1751. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1752. * @param {Number} duration
  1753. * @param {cc.Point|Number} position
  1754. * @param {Number} [y]
  1755. * @param {Number} height
  1756. * @param {Number} jumps
  1757. */
  1758. ctor:function (duration, position, y, height, jumps) {
  1759. cc.JumpBy.prototype.ctor.call(this);
  1760. this._endPosition = cc.p(0, 0);
  1761. height !== undefined && this.initWithDuration(duration, position, y, height, jumps);
  1762. },
  1763. /**
  1764. * Initializes the action.
  1765. * @param {Number} duration
  1766. * @param {cc.Point|Number} position
  1767. * @param {Number} [y]
  1768. * @param {Number} height
  1769. * @param {Number} jumps
  1770. * @return {Boolean}
  1771. * @example
  1772. * actionTo.initWithDuration(2, cc.p(300, 0), 50, 4);
  1773. * actionTo.initWithDuration(2, 300, 0, 50, 4);
  1774. */
  1775. initWithDuration:function (duration, position, y, height, jumps) {
  1776. if (cc.JumpBy.prototype.initWithDuration.call(this, duration, position, y, height, jumps)) {
  1777. if (jumps === undefined) {
  1778. y = position.y;
  1779. position = position.x;
  1780. }
  1781. this._endPosition.x = position;
  1782. this._endPosition.y = y;
  1783. return true;
  1784. }
  1785. return false;
  1786. },
  1787. /**
  1788. * Start the action with target.
  1789. * @param {cc.Node} target
  1790. */
  1791. startWithTarget:function (target) {
  1792. cc.JumpBy.prototype.startWithTarget.call(this, target);
  1793. this._delta.x = this._endPosition.x - this._startPosition.x;
  1794. this._delta.y = this._endPosition.y - this._startPosition.y;
  1795. },
  1796. /**
  1797. * returns a new clone of the action
  1798. * @returns {cc.JumpTo}
  1799. */
  1800. clone:function () {
  1801. var action = new cc.JumpTo();
  1802. this._cloneDecoration(action);
  1803. action.initWithDuration(this._duration, this._endPosition, this._height, this._jumps);
  1804. return action;
  1805. }
  1806. });
  1807. /**
  1808. * Moves a cc.Node object to a parabolic position simulating a jump movement by modifying it's position attribute. <br />
  1809. * Jump to the specified location.
  1810. * @function
  1811. * @param {Number} duration
  1812. * @param {cc.Point|Number} position
  1813. * @param {Number} [y]
  1814. * @param {Number} height
  1815. * @param {Number} jumps
  1816. * @return {cc.JumpTo}
  1817. * @example
  1818. * // example
  1819. * var actionTo = cc.jumpTo(2, cc.p(300, 300), 50, 4);
  1820. * var actionTo = cc.jumpTo(2, 300, 300, 50, 4);
  1821. */
  1822. cc.jumpTo = function (duration, position, y, height, jumps) {
  1823. return new cc.JumpTo(duration, position, y, height, jumps);
  1824. };
  1825. /**
  1826. * Please use cc.jumpTo instead.
  1827. * Moves a cc.Node object to a parabolic position simulating a jump movement by modifying it's position attribute. <br />
  1828. * Jump to the specified location.
  1829. * @static
  1830. * @deprecated since v3.0 please use cc.jumpTo instead.
  1831. * @param {Number} duration
  1832. * @param {cc.Point|Number} position
  1833. * @param {Number} [y]
  1834. * @param {Number} height
  1835. * @param {Number} jumps
  1836. * @return {cc.JumpTo}
  1837. */
  1838. cc.JumpTo.create = cc.jumpTo;
  1839. /**
  1840. * @function
  1841. * @param {Number} a
  1842. * @param {Number} b
  1843. * @param {Number} c
  1844. * @param {Number} d
  1845. * @param {Number} t
  1846. * @return {Number}
  1847. */
  1848. cc.bezierAt = function (a, b, c, d, t) {
  1849. return (Math.pow(1 - t, 3) * a +
  1850. 3 * t * (Math.pow(1 - t, 2)) * b +
  1851. 3 * Math.pow(t, 2) * (1 - t) * c +
  1852. Math.pow(t, 3) * d );
  1853. };
  1854. /** An action that moves the target with a cubic Bezier curve by a certain distance.
  1855. * Relative to its movement.
  1856. * @class
  1857. * @extends cc.ActionInterval
  1858. * @param {Number} t time in seconds
  1859. * @param {Array} c Array of points
  1860. * @example
  1861. * var bezier = [cc.p(0, windowSize.height / 2), cc.p(300, -windowSize.height / 2), cc.p(300, 100)];
  1862. * var bezierForward = new cc.BezierBy(3, bezier);
  1863. */
  1864. cc.BezierBy = cc.ActionInterval.extend(/** @lends cc.BezierBy# */{
  1865. _config:null,
  1866. _startPosition:null,
  1867. _previousPosition:null,
  1868. /**
  1869. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1870. * @param {Number} t time in seconds
  1871. * @param {Array} c Array of points
  1872. */
  1873. ctor:function (t, c) {
  1874. cc.ActionInterval.prototype.ctor.call(this);
  1875. this._config = [];
  1876. this._startPosition = cc.p(0, 0);
  1877. this._previousPosition = cc.p(0, 0);
  1878. c && this.initWithDuration(t, c);
  1879. },
  1880. /**
  1881. * Initializes the action.
  1882. * @param {Number} t time in seconds
  1883. * @param {Array} c Array of points
  1884. * @return {Boolean}
  1885. */
  1886. initWithDuration:function (t, c) {
  1887. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  1888. this._config = c;
  1889. return true;
  1890. }
  1891. return false;
  1892. },
  1893. /**
  1894. * returns a new clone of the action
  1895. * @returns {cc.BezierBy}
  1896. */
  1897. clone:function () {
  1898. var action = new cc.BezierBy();
  1899. this._cloneDecoration(action);
  1900. var newConfigs = [];
  1901. for (var i = 0; i < this._config.length; i++) {
  1902. var selConf = this._config[i];
  1903. newConfigs.push(cc.p(selConf.x, selConf.y));
  1904. }
  1905. action.initWithDuration(this._duration, newConfigs);
  1906. return action;
  1907. },
  1908. /**
  1909. * Start the action with target.
  1910. * @param {cc.Node} target
  1911. */
  1912. startWithTarget:function (target) {
  1913. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  1914. var locPosX = target.getPositionX();
  1915. var locPosY = target.getPositionY();
  1916. this._previousPosition.x = locPosX;
  1917. this._previousPosition.y = locPosY;
  1918. this._startPosition.x = locPosX;
  1919. this._startPosition.y = locPosY;
  1920. },
  1921. /**
  1922. * Called once per frame. Time is the number of seconds of a frame interval.
  1923. * @param {Number} dt
  1924. */
  1925. update:function (dt) {
  1926. dt = this._computeEaseTime(dt);
  1927. if (this.target) {
  1928. var locConfig = this._config;
  1929. var xa = 0;
  1930. var xb = locConfig[0].x;
  1931. var xc = locConfig[1].x;
  1932. var xd = locConfig[2].x;
  1933. var ya = 0;
  1934. var yb = locConfig[0].y;
  1935. var yc = locConfig[1].y;
  1936. var yd = locConfig[2].y;
  1937. var x = cc.bezierAt(xa, xb, xc, xd, dt);
  1938. var y = cc.bezierAt(ya, yb, yc, yd, dt);
  1939. var locStartPosition = this._startPosition;
  1940. if (cc.ENABLE_STACKABLE_ACTIONS) {
  1941. var targetX = this.target.getPositionX();
  1942. var targetY = this.target.getPositionY();
  1943. var locPreviousPosition = this._previousPosition;
  1944. locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
  1945. locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
  1946. x = x + locStartPosition.x;
  1947. y = y + locStartPosition.y;
  1948. locPreviousPosition.x = x;
  1949. locPreviousPosition.y = y;
  1950. this.target.setPosition(x, y);
  1951. } else {
  1952. this.target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
  1953. }
  1954. }
  1955. },
  1956. /**
  1957. * Returns a reversed action.
  1958. * @return {cc.BezierBy}
  1959. */
  1960. reverse:function () {
  1961. var locConfig = this._config;
  1962. var r = [
  1963. cc.pAdd(locConfig[1], cc.pNeg(locConfig[2])),
  1964. cc.pAdd(locConfig[0], cc.pNeg(locConfig[2])),
  1965. cc.pNeg(locConfig[2]) ];
  1966. var action = new cc.BezierBy(this._duration, r);
  1967. this._cloneDecoration(action);
  1968. this._reverseEaseList(action);
  1969. return action;
  1970. }
  1971. });
  1972. /**
  1973. * An action that moves the target with a cubic Bezier curve by a certain distance.
  1974. * Relative to its movement.
  1975. * @function
  1976. * @param {Number} t time in seconds
  1977. * @param {Array} c Array of points
  1978. * @return {cc.BezierBy}
  1979. * @example
  1980. * // example
  1981. * var bezier = [cc.p(0, windowSize.height / 2), cc.p(300, -windowSize.height / 2), cc.p(300, 100)];
  1982. * var bezierForward = cc.bezierBy(3, bezier);
  1983. */
  1984. cc.bezierBy = function (t, c) {
  1985. return new cc.BezierBy(t, c);
  1986. };
  1987. /**
  1988. * Please use cc.bezierBy instead.
  1989. * An action that moves the target with a cubic Bezier curve by a certain distance.
  1990. * Relative to its movement.
  1991. * @static
  1992. * @deprecated since v3.0 please use cc.bezierBy instead.
  1993. * @param {Number} t time in seconds
  1994. * @param {Array} c Array of points
  1995. * @return {cc.BezierBy}
  1996. */
  1997. cc.BezierBy.create = cc.bezierBy;
  1998. /** An action that moves the target with a cubic Bezier curve to a destination point.
  1999. * @class
  2000. * @extends cc.BezierBy
  2001. * @param {Number} t
  2002. * @param {Array} c array of points
  2003. * @example
  2004. * var bezier = [cc.p(0, windowSize.height / 2), cc.p(300, -windowSize.height / 2), cc.p(300, 100)];
  2005. * var bezierTo = new cc.BezierTo(2, bezier);
  2006. */
  2007. cc.BezierTo = cc.BezierBy.extend(/** @lends cc.BezierTo# */{
  2008. _toConfig:null,
  2009. /**
  2010. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2011. * @param {Number} t
  2012. * @param {Array} c array of points
  2013. * var bezierTo = new cc.BezierTo(2, bezier);
  2014. */
  2015. ctor:function (t, c) {
  2016. cc.BezierBy.prototype.ctor.call(this);
  2017. this._toConfig = [];
  2018. c && this.initWithDuration(t, c);
  2019. },
  2020. /**
  2021. * Initializes the action.
  2022. * @param {Number} t time in seconds
  2023. * @param {Array} c Array of points
  2024. * @return {Boolean}
  2025. */
  2026. initWithDuration:function (t, c) {
  2027. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  2028. this._toConfig = c;
  2029. return true;
  2030. }
  2031. return false;
  2032. },
  2033. /**
  2034. * returns a new clone of the action
  2035. * @returns {cc.BezierTo}
  2036. */
  2037. clone:function () {
  2038. var action = new cc.BezierTo();
  2039. this._cloneDecoration(action);
  2040. action.initWithDuration(this._duration, this._toConfig);
  2041. return action;
  2042. },
  2043. /**
  2044. * Start the action with target.
  2045. * @param {cc.Node} target
  2046. */
  2047. startWithTarget:function (target) {
  2048. cc.BezierBy.prototype.startWithTarget.call(this, target);
  2049. var locStartPos = this._startPosition;
  2050. var locToConfig = this._toConfig;
  2051. var locConfig = this._config;
  2052. locConfig[0] = cc.pSub(locToConfig[0], locStartPos);
  2053. locConfig[1] = cc.pSub(locToConfig[1], locStartPos);
  2054. locConfig[2] = cc.pSub(locToConfig[2], locStartPos);
  2055. }
  2056. });
  2057. /**
  2058. * An action that moves the target with a cubic Bezier curve to a destination point.
  2059. * @function
  2060. * @param {Number} t
  2061. * @param {Array} c array of points
  2062. * @return {cc.BezierTo}
  2063. * @example
  2064. * // example
  2065. * var bezier = [cc.p(0, windowSize.height / 2), cc.p(300, -windowSize.height / 2), cc.p(300, 100)];
  2066. * var bezierTo = cc.bezierTo(2, bezier);
  2067. */
  2068. cc.bezierTo = function (t, c) {
  2069. return new cc.BezierTo(t, c);
  2070. };
  2071. /**
  2072. * Please use cc.bezierTo instead
  2073. * @static
  2074. * @deprecated since v3.0 please use cc.bezierTo instead.
  2075. * @param {Number} t
  2076. * @param {Array} c array of points
  2077. * @return {cc.BezierTo}
  2078. */
  2079. cc.BezierTo.create = cc.bezierTo;
  2080. /** Scales a cc.Node object to a zoom factor by modifying it's scale attribute.
  2081. * @warning This action doesn't support "reverse"
  2082. * @class
  2083. * @extends cc.ActionInterval
  2084. * @param {Number} duration
  2085. * @param {Number} sx scale parameter in X
  2086. * @param {Number} [sy] scale parameter in Y, if Null equal to sx
  2087. * @example
  2088. * // It scales to 0.5 in both X and Y.
  2089. * var actionTo = new cc.ScaleTo(2, 0.5);
  2090. *
  2091. * // It scales to 0.5 in x and 2 in Y
  2092. * var actionTo = new cc.ScaleTo(2, 0.5, 2);
  2093. */
  2094. cc.ScaleTo = cc.ActionInterval.extend(/** @lends cc.ScaleTo# */{
  2095. _scaleX:1,
  2096. _scaleY:1,
  2097. _startScaleX:1,
  2098. _startScaleY:1,
  2099. _endScaleX:0,
  2100. _endScaleY:0,
  2101. _deltaX:0,
  2102. _deltaY:0,
  2103. /**
  2104. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2105. * @param {Number} duration
  2106. * @param {Number} sx scale parameter in X
  2107. * @param {Number} [sy] scale parameter in Y, if Null equal to sx
  2108. */
  2109. ctor:function (duration, sx, sy) {
  2110. cc.ActionInterval.prototype.ctor.call(this);
  2111. sx !== undefined && this.initWithDuration(duration, sx, sy);
  2112. },
  2113. /**
  2114. * Initializes the action.
  2115. * @param {Number} duration
  2116. * @param {Number} sx
  2117. * @param {Number} [sy=]
  2118. * @return {Boolean}
  2119. */
  2120. initWithDuration:function (duration, sx, sy) { //function overload here
  2121. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  2122. this._endScaleX = sx;
  2123. this._endScaleY = (sy != null) ? sy : sx;
  2124. return true;
  2125. }
  2126. return false;
  2127. },
  2128. /**
  2129. * returns a new clone of the action
  2130. * @returns {cc.ScaleTo}
  2131. */
  2132. clone:function () {
  2133. var action = new cc.ScaleTo();
  2134. this._cloneDecoration(action);
  2135. action.initWithDuration(this._duration, this._endScaleX, this._endScaleY);
  2136. return action;
  2137. },
  2138. /**
  2139. * Start the action with target.
  2140. * @param {cc.Node} target
  2141. */
  2142. startWithTarget:function (target) {
  2143. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2144. this._startScaleX = target.scaleX;
  2145. this._startScaleY = target.scaleY;
  2146. this._deltaX = this._endScaleX - this._startScaleX;
  2147. this._deltaY = this._endScaleY - this._startScaleY;
  2148. },
  2149. /**
  2150. * Called once per frame. Time is the number of seconds of a frame interval.
  2151. * @param {Number} dt
  2152. */
  2153. update:function (dt) {
  2154. dt = this._computeEaseTime(dt);
  2155. if (this.target) {
  2156. this.target.scaleX = this._startScaleX + this._deltaX * dt;
  2157. this.target.scaleY = this._startScaleY + this._deltaY * dt;
  2158. }
  2159. }
  2160. });
  2161. /**
  2162. * Scales a cc.Node object to a zoom factor by modifying it's scale attribute.
  2163. * @function
  2164. * @param {Number} duration
  2165. * @param {Number} sx scale parameter in X
  2166. * @param {Number} [sy] scale parameter in Y, if Null equal to sx
  2167. * @return {cc.ScaleTo}
  2168. * @example
  2169. * // example
  2170. * // It scales to 0.5 in both X and Y.
  2171. * var actionTo = cc.scaleTo(2, 0.5);
  2172. *
  2173. * // It scales to 0.5 in x and 2 in Y
  2174. * var actionTo = cc.scaleTo(2, 0.5, 2);
  2175. */
  2176. cc.scaleTo = function (duration, sx, sy) { //function overload
  2177. return new cc.ScaleTo(duration, sx, sy);
  2178. };
  2179. /**
  2180. * Please use cc.scaleTo instead.
  2181. * Scales a cc.Node object to a zoom factor by modifying it's scale attribute.
  2182. * @static
  2183. * @deprecated since v3.0 please use cc.scaleTo instead.
  2184. * @param {Number} duration
  2185. * @param {Number} sx scale parameter in X
  2186. * @param {Number} [sy] scale parameter in Y, if Null equal to sx
  2187. * @return {cc.ScaleTo}
  2188. */
  2189. cc.ScaleTo.create = cc.scaleTo;
  2190. /** Scales a cc.Node object a zoom factor by modifying it's scale attribute.
  2191. * Relative to its changes.
  2192. * @class
  2193. * @extends cc.ScaleTo
  2194. */
  2195. cc.ScaleBy = cc.ScaleTo.extend(/** @lends cc.ScaleBy# */{
  2196. /**
  2197. * Start the action with target.
  2198. * @param {cc.Node} target
  2199. */
  2200. startWithTarget:function (target) {
  2201. cc.ScaleTo.prototype.startWithTarget.call(this, target);
  2202. this._deltaX = this._startScaleX * this._endScaleX - this._startScaleX;
  2203. this._deltaY = this._startScaleY * this._endScaleY - this._startScaleY;
  2204. },
  2205. /**
  2206. * Returns a reversed action.
  2207. * @return {cc.ScaleBy}
  2208. */
  2209. reverse:function () {
  2210. var action = new cc.ScaleBy(this._duration, 1 / this._endScaleX, 1 / this._endScaleY);
  2211. this._cloneDecoration(action);
  2212. this._reverseEaseList(action);
  2213. return action;
  2214. },
  2215. /**
  2216. * returns a new clone of the action
  2217. * @returns {cc.ScaleBy}
  2218. */
  2219. clone:function () {
  2220. var action = new cc.ScaleBy();
  2221. this._cloneDecoration(action);
  2222. action.initWithDuration(this._duration, this._endScaleX, this._endScaleY);
  2223. return action;
  2224. }
  2225. });
  2226. /**
  2227. * Scales a cc.Node object a zoom factor by modifying it's scale attribute.
  2228. * Relative to its changes.
  2229. * @function
  2230. * @param {Number} duration duration in seconds
  2231. * @param {Number} sx sx scale parameter in X
  2232. * @param {Number|Null} [sy=] sy scale parameter in Y, if Null equal to sx
  2233. * @return {cc.ScaleBy}
  2234. * @example
  2235. * // example without sy, it scales by 2 both in X and Y
  2236. * var actionBy = cc.scaleBy(2, 2);
  2237. *
  2238. * //example with sy, it scales by 0.25 in X and 4.5 in Y
  2239. * var actionBy2 = cc.scaleBy(2, 0.25, 4.5);
  2240. */
  2241. cc.scaleBy = function (duration, sx, sy) {
  2242. return new cc.ScaleBy(duration, sx, sy);
  2243. };
  2244. /**
  2245. * Please use cc.scaleBy instead.
  2246. * Scales a cc.Node object a zoom factor by modifying it's scale attribute.
  2247. * Relative to its changes.
  2248. * @static
  2249. * @deprecated since v3.0 please use cc.scaleBy() instead.
  2250. * @param {Number} duration duration in seconds
  2251. * @param {Number} sx sx scale parameter in X
  2252. * @param {Number|Null} [sy=] sy scale parameter in Y, if Null equal to sx
  2253. * @return {cc.ScaleBy}
  2254. */
  2255. cc.ScaleBy.create = cc.scaleBy;
  2256. /** Blinks a cc.Node object by modifying it's visible attribute
  2257. * @class
  2258. * @extends cc.ActionInterval
  2259. * @param {Number} duration duration in seconds
  2260. * @param {Number} blinks blinks in times
  2261. * @example
  2262. * var action = new cc.Blink(2, 10);
  2263. */
  2264. cc.Blink = cc.ActionInterval.extend(/** @lends cc.Blink# */{
  2265. _times:0,
  2266. _originalState:false,
  2267. /**
  2268. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2269. * @param {Number} duration duration in seconds
  2270. * @param {Number} blinks blinks in times
  2271. */
  2272. ctor:function (duration, blinks) {
  2273. cc.ActionInterval.prototype.ctor.call(this);
  2274. blinks !== undefined && this.initWithDuration(duration, blinks);
  2275. },
  2276. /**
  2277. * Initializes the action.
  2278. * @param {Number} duration duration in seconds
  2279. * @param {Number} blinks blinks in times
  2280. * @return {Boolean}
  2281. */
  2282. initWithDuration:function (duration, blinks) {
  2283. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  2284. this._times = blinks;
  2285. return true;
  2286. }
  2287. return false;
  2288. },
  2289. /**
  2290. * returns a new clone of the action
  2291. * @returns {cc.Blink}
  2292. */
  2293. clone:function () {
  2294. var action = new cc.Blink();
  2295. this._cloneDecoration(action);
  2296. action.initWithDuration(this._duration, this._times);
  2297. return action;
  2298. },
  2299. /**
  2300. * Called once per frame. Time is the number of seconds of a frame interval.
  2301. * @param {Number} dt time in seconds
  2302. */
  2303. update:function (dt) {
  2304. dt = this._computeEaseTime(dt);
  2305. if (this.target && !this.isDone()) {
  2306. var slice = 1.0 / this._times;
  2307. var m = dt % slice;
  2308. this.target.visible = (m > (slice / 2));
  2309. }
  2310. },
  2311. /**
  2312. * Start the action with target.
  2313. * @param {cc.Node} target
  2314. */
  2315. startWithTarget:function (target) {
  2316. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2317. this._originalState = target.visible;
  2318. },
  2319. /**
  2320. * stop the action
  2321. */
  2322. stop:function () {
  2323. this.target.visible = this._originalState;
  2324. cc.ActionInterval.prototype.stop.call(this);
  2325. },
  2326. /**
  2327. * Returns a reversed action.
  2328. * @return {cc.Blink}
  2329. */
  2330. reverse:function () {
  2331. var action = new cc.Blink(this._duration, this._times);
  2332. this._cloneDecoration(action);
  2333. this._reverseEaseList(action);
  2334. return action;
  2335. }
  2336. });
  2337. /**
  2338. * Blinks a cc.Node object by modifying it's visible attribute.
  2339. * @function
  2340. * @param {Number} duration duration in seconds
  2341. * @param blinks blinks in times
  2342. * @return {cc.Blink}
  2343. * @example
  2344. * // example
  2345. * var action = cc.blink(2, 10);
  2346. */
  2347. cc.blink = function (duration, blinks) {
  2348. return new cc.Blink(duration, blinks);
  2349. };
  2350. /**
  2351. * Please use cc.blink instead.
  2352. * Blinks a cc.Node object by modifying it's visible attribute.
  2353. * @static
  2354. * @deprecated since v3.0 please use cc.blink instead.
  2355. * @param {Number} duration duration in seconds
  2356. * @param blinks blinks in times
  2357. * @return {cc.Blink}
  2358. */
  2359. cc.Blink.create = cc.blink;
  2360. /** Fades an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from the current value to a custom one.
  2361. * @warning This action doesn't support "reverse"
  2362. * @class
  2363. * @extends cc.ActionInterval
  2364. * @param {Number} duration
  2365. * @param {Number} opacity 0-255, 0 is transparent
  2366. * @example
  2367. * var action = new cc.FadeTo(1.0, 0);
  2368. */
  2369. cc.FadeTo = cc.ActionInterval.extend(/** @lends cc.FadeTo# */{
  2370. _toOpacity:0,
  2371. _fromOpacity:0,
  2372. /**
  2373. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2374. * @param {Number} duration
  2375. * @param {Number} opacity 0-255, 0 is transparent
  2376. */
  2377. ctor:function (duration, opacity) {
  2378. cc.ActionInterval.prototype.ctor.call(this);
  2379. opacity !== undefined && this.initWithDuration(duration, opacity);
  2380. },
  2381. /**
  2382. * Initializes the action.
  2383. * @param {Number} duration duration in seconds
  2384. * @param {Number} opacity
  2385. * @return {Boolean}
  2386. */
  2387. initWithDuration:function (duration, opacity) {
  2388. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  2389. this._toOpacity = opacity;
  2390. return true;
  2391. }
  2392. return false;
  2393. },
  2394. /**
  2395. * returns a new clone of the action
  2396. * @returns {cc.FadeTo}
  2397. */
  2398. clone:function () {
  2399. var action = new cc.FadeTo();
  2400. this._cloneDecoration(action);
  2401. action.initWithDuration(this._duration, this._toOpacity);
  2402. return action;
  2403. },
  2404. /**
  2405. * Called once per frame. Time is the number of seconds of a frame interval.
  2406. * @param {Number} time time in seconds
  2407. */
  2408. update:function (time) {
  2409. time = this._computeEaseTime(time);
  2410. var fromOpacity = this._fromOpacity !== undefined ? this._fromOpacity : 255;
  2411. this.target.opacity = fromOpacity + (this._toOpacity - fromOpacity) * time;
  2412. },
  2413. /**
  2414. * Start this action with target.
  2415. * @param {cc.Node} target
  2416. */
  2417. startWithTarget:function (target) {
  2418. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2419. this._fromOpacity = target.opacity;
  2420. }
  2421. });
  2422. /**
  2423. * Fades an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from the current value to a custom one.
  2424. * @function
  2425. * @param {Number} duration
  2426. * @param {Number} opacity 0-255, 0 is transparent
  2427. * @return {cc.FadeTo}
  2428. * @example
  2429. * // example
  2430. * var action = cc.fadeTo(1.0, 0);
  2431. */
  2432. cc.fadeTo = function (duration, opacity) {
  2433. return new cc.FadeTo(duration, opacity);
  2434. };
  2435. /**
  2436. * Please use cc.fadeTo instead.
  2437. * Fades an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from the current value to a custom one.
  2438. * @static
  2439. * @deprecated since v3.0 please use cc.fadeTo instead.
  2440. * @param {Number} duration
  2441. * @param {Number} opacity 0-255, 0 is transparent
  2442. * @return {cc.FadeTo}
  2443. */
  2444. cc.FadeTo.create = cc.fadeTo;
  2445. /** Fades In an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 0 to 255.<br/>
  2446. * The "reverse" of this action is FadeOut
  2447. * @class
  2448. * @extends cc.FadeTo
  2449. * @param {Number} duration duration in seconds
  2450. */
  2451. cc.FadeIn = cc.FadeTo.extend(/** @lends cc.FadeIn# */{
  2452. _reverseAction: null,
  2453. /**
  2454. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2455. * @param {Number} duration duration in seconds
  2456. */
  2457. ctor:function (duration) {
  2458. cc.FadeTo.prototype.ctor.call(this);
  2459. duration && this.initWithDuration(duration, 255);
  2460. },
  2461. /**
  2462. * Returns a reversed action.
  2463. * @return {cc.FadeOut}
  2464. */
  2465. reverse:function () {
  2466. var action = new cc.FadeOut();
  2467. action.initWithDuration(this._duration, 0);
  2468. this._cloneDecoration(action);
  2469. this._reverseEaseList(action);
  2470. return action;
  2471. },
  2472. /**
  2473. * returns a new clone of the action
  2474. * @returns {cc.FadeIn}
  2475. */
  2476. clone:function () {
  2477. var action = new cc.FadeIn();
  2478. this._cloneDecoration(action);
  2479. action.initWithDuration(this._duration, this._toOpacity);
  2480. return action;
  2481. },
  2482. /**
  2483. * Start the action with target.
  2484. * @param {cc.Node} target
  2485. */
  2486. startWithTarget:function (target) {
  2487. if(this._reverseAction)
  2488. this._toOpacity = this._reverseAction._fromOpacity;
  2489. cc.FadeTo.prototype.startWithTarget.call(this, target);
  2490. }
  2491. });
  2492. /**
  2493. * Fades In an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 0 to 255.
  2494. * @function
  2495. * @param {Number} duration duration in seconds
  2496. * @return {cc.FadeIn}
  2497. * @example
  2498. * //example
  2499. * var action = cc.fadeIn(1.0);
  2500. */
  2501. cc.fadeIn = function (duration) {
  2502. return new cc.FadeIn(duration);
  2503. };
  2504. /**
  2505. * Please use cc.fadeIn instead.
  2506. * Fades In an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 0 to 255.
  2507. * @static
  2508. * @deprecated since v3.0 please use cc.fadeIn() instead.
  2509. * @param {Number} duration duration in seconds
  2510. * @return {cc.FadeIn}
  2511. */
  2512. cc.FadeIn.create = cc.fadeIn;
  2513. /** Fades Out an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 255 to 0.
  2514. * The "reverse" of this action is FadeIn
  2515. * @class
  2516. * @extends cc.FadeTo
  2517. * @param {Number} duration duration in seconds
  2518. */
  2519. cc.FadeOut = cc.FadeTo.extend(/** @lends cc.FadeOut# */{
  2520. /**
  2521. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2522. * @param {Number} duration duration in seconds
  2523. */
  2524. ctor:function (duration) {
  2525. cc.FadeTo.prototype.ctor.call(this);
  2526. duration && this.initWithDuration(duration, 0);
  2527. },
  2528. /**
  2529. * Returns a reversed action.
  2530. * @return {cc.FadeIn}
  2531. */
  2532. reverse:function () {
  2533. var action = new cc.FadeIn();
  2534. action._reverseAction = this;
  2535. action.initWithDuration(this._duration, 255);
  2536. this._cloneDecoration(action);
  2537. this._reverseEaseList(action);
  2538. return action;
  2539. },
  2540. /**
  2541. * returns a new clone of the action
  2542. * @returns {cc.FadeOut}
  2543. */
  2544. clone:function () {
  2545. var action = new cc.FadeOut();
  2546. this._cloneDecoration(action);
  2547. action.initWithDuration(this._duration, this._toOpacity);
  2548. return action;
  2549. }
  2550. });
  2551. /**
  2552. * Fades Out an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 255 to 0.
  2553. * @function
  2554. * @param {Number} d duration in seconds
  2555. * @return {cc.FadeOut}
  2556. * @example
  2557. * // example
  2558. * var action = cc.fadeOut(1.0);
  2559. */
  2560. cc.fadeOut = function (d) {
  2561. return new cc.FadeOut(d);
  2562. };
  2563. /**
  2564. * Please use cc.fadeOut instead.
  2565. * Fades Out an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 255 to 0.
  2566. * @static
  2567. * @deprecated since v3.0 please use cc.fadeOut instead.
  2568. * @param {Number} d duration in seconds
  2569. * @return {cc.FadeOut}
  2570. */
  2571. cc.FadeOut.create = cc.fadeOut;
  2572. /** Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  2573. * @warning This action doesn't support "reverse"
  2574. * @class
  2575. * @extends cc.ActionInterval
  2576. * @param {Number} duration
  2577. * @param {Number} red 0-255
  2578. * @param {Number} green 0-255
  2579. * @param {Number} blue 0-255
  2580. * @example
  2581. * var action = new cc.TintTo(2, 255, 0, 255);
  2582. */
  2583. cc.TintTo = cc.ActionInterval.extend(/** @lends cc.TintTo# */{
  2584. _to:null,
  2585. _from:null,
  2586. /**
  2587. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2588. * @param {Number} duration
  2589. * @param {Number} red 0-255
  2590. * @param {Number} green 0-255
  2591. * @param {Number} blue 0-255
  2592. */
  2593. ctor:function (duration, red, green, blue) {
  2594. cc.ActionInterval.prototype.ctor.call(this);
  2595. this._to = cc.color(0, 0, 0);
  2596. this._from = cc.color(0, 0, 0);
  2597. blue !== undefined && this.initWithDuration(duration, red, green, blue);
  2598. },
  2599. /**
  2600. * Initializes the action.
  2601. * @param {Number} duration
  2602. * @param {Number} red 0-255
  2603. * @param {Number} green 0-255
  2604. * @param {Number} blue 0-255
  2605. * @return {Boolean}
  2606. */
  2607. initWithDuration:function (duration, red, green, blue) {
  2608. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  2609. this._to = cc.color(red, green, blue);
  2610. return true;
  2611. }
  2612. return false;
  2613. },
  2614. /**
  2615. * returns a new clone of the action
  2616. * @returns {cc.TintTo}
  2617. */
  2618. clone:function () {
  2619. var action = new cc.TintTo();
  2620. this._cloneDecoration(action);
  2621. var locTo = this._to;
  2622. action.initWithDuration(this._duration, locTo.r, locTo.g, locTo.b);
  2623. return action;
  2624. },
  2625. /**
  2626. * Start the action with target.
  2627. * @param {cc.Node} target
  2628. */
  2629. startWithTarget:function (target) {
  2630. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2631. this._from = this.target.color;
  2632. },
  2633. /**
  2634. * Called once per frame. Time is the number of seconds of a frame interval.
  2635. * @param {Number} dt time in seconds
  2636. */
  2637. update:function (dt) {
  2638. dt = this._computeEaseTime(dt);
  2639. var locFrom = this._from, locTo = this._to;
  2640. if (locFrom) {
  2641. this.target.color = cc.color(locFrom.r + (locTo.r - locFrom.r) * dt,
  2642. locFrom.g + (locTo.g - locFrom.g) * dt,
  2643. locFrom.b + (locTo.b - locFrom.b) * dt);
  2644. }
  2645. }
  2646. });
  2647. /**
  2648. * Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  2649. * @function
  2650. * @param {Number} duration
  2651. * @param {Number} red 0-255
  2652. * @param {Number} green 0-255
  2653. * @param {Number} blue 0-255
  2654. * @return {cc.TintTo}
  2655. * @example
  2656. * // example
  2657. * var action = cc.tintTo(2, 255, 0, 255);
  2658. */
  2659. cc.tintTo = function (duration, red, green, blue) {
  2660. return new cc.TintTo(duration, red, green, blue);
  2661. };
  2662. /**
  2663. * Please use cc.tintTo instead.
  2664. * Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  2665. * @static
  2666. * @deprecated since v3.0 please use cc.tintTo instead.
  2667. * @param {Number} duration
  2668. * @param {Number} red 0-255
  2669. * @param {Number} green 0-255
  2670. * @param {Number} blue 0-255
  2671. * @return {cc.TintTo}
  2672. */
  2673. cc.TintTo.create = cc.tintTo;
  2674. /** Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  2675. * Relative to their own color change.
  2676. * @class
  2677. * @extends cc.ActionInterval
  2678. * @param {Number} duration duration in seconds
  2679. * @param {Number} deltaRed
  2680. * @param {Number} deltaGreen
  2681. * @param {Number} deltaBlue
  2682. * @example
  2683. * var action = new cc.TintBy(2, -127, -255, -127);
  2684. */
  2685. cc.TintBy = cc.ActionInterval.extend(/** @lends cc.TintBy# */{
  2686. _deltaR:0,
  2687. _deltaG:0,
  2688. _deltaB:0,
  2689. _fromR:0,
  2690. _fromG:0,
  2691. _fromB:0,
  2692. /**
  2693. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2694. * @param {Number} duration duration in seconds
  2695. * @param {Number} deltaRed
  2696. * @param {Number} deltaGreen
  2697. * @param {Number} deltaBlue
  2698. */
  2699. ctor:function (duration, deltaRed, deltaGreen, deltaBlue) {
  2700. cc.ActionInterval.prototype.ctor.call(this);
  2701. deltaBlue !== undefined && this.initWithDuration(duration, deltaRed, deltaGreen, deltaBlue);
  2702. },
  2703. /**
  2704. * Initializes the action.
  2705. * @param {Number} duration
  2706. * @param {Number} deltaRed 0-255
  2707. * @param {Number} deltaGreen 0-255
  2708. * @param {Number} deltaBlue 0-255
  2709. * @return {Boolean}
  2710. */
  2711. initWithDuration:function (duration, deltaRed, deltaGreen, deltaBlue) {
  2712. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  2713. this._deltaR = deltaRed;
  2714. this._deltaG = deltaGreen;
  2715. this._deltaB = deltaBlue;
  2716. return true;
  2717. }
  2718. return false;
  2719. },
  2720. /**
  2721. * returns a new clone of the action
  2722. * @returns {cc.TintBy}
  2723. */
  2724. clone:function () {
  2725. var action = new cc.TintBy();
  2726. this._cloneDecoration(action);
  2727. action.initWithDuration(this._duration, this._deltaR, this._deltaG, this._deltaB);
  2728. return action;
  2729. },
  2730. /**
  2731. * Start the action with target.
  2732. * @param {cc.Node} target
  2733. */
  2734. startWithTarget:function (target) {
  2735. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2736. var color = target.color;
  2737. this._fromR = color.r;
  2738. this._fromG = color.g;
  2739. this._fromB = color.b;
  2740. },
  2741. /**
  2742. * Called once per frame. Time is the number of seconds of a frame interval.
  2743. * @param {Number} dt time in seconds
  2744. */
  2745. update:function (dt) {
  2746. dt = this._computeEaseTime(dt);
  2747. this.target.color = cc.color(this._fromR + this._deltaR * dt,
  2748. this._fromG + this._deltaG * dt,
  2749. this._fromB + this._deltaB * dt);
  2750. },
  2751. /**
  2752. * Returns a reversed action.
  2753. * @return {cc.TintBy}
  2754. */
  2755. reverse:function () {
  2756. var action = new cc.TintBy(this._duration, -this._deltaR, -this._deltaG, -this._deltaB);
  2757. this._cloneDecoration(action);
  2758. this._reverseEaseList(action);
  2759. return action;
  2760. }
  2761. });
  2762. /**
  2763. * Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  2764. * Relative to their own color change.
  2765. * @function
  2766. * @param {Number} duration duration in seconds
  2767. * @param {Number} deltaRed
  2768. * @param {Number} deltaGreen
  2769. * @param {Number} deltaBlue
  2770. * @return {cc.TintBy}
  2771. * @example
  2772. * // example
  2773. * var action = cc.tintBy(2, -127, -255, -127);
  2774. */
  2775. cc.tintBy = function (duration, deltaRed, deltaGreen, deltaBlue) {
  2776. return new cc.TintBy(duration, deltaRed, deltaGreen, deltaBlue);
  2777. };
  2778. /**
  2779. * Please use cc.tintBy instead.
  2780. * Tints a cc.Node that implements the cc.NodeRGB protocol from current tint to a custom one.
  2781. * Relative to their own color change.
  2782. * @static
  2783. * @deprecated since v3.0 please use cc.tintBy instead.
  2784. * @param {Number} duration duration in seconds
  2785. * @param {Number} deltaRed
  2786. * @param {Number} deltaGreen
  2787. * @param {Number} deltaBlue
  2788. * @return {cc.TintBy}
  2789. */
  2790. cc.TintBy.create = cc.tintBy;
  2791. /** Delays the action a certain amount of seconds
  2792. * @class
  2793. * @extends cc.ActionInterval
  2794. */
  2795. cc.DelayTime = cc.ActionInterval.extend(/** @lends cc.DelayTime# */{
  2796. /**
  2797. * Called once per frame. Time is the number of seconds of a frame interval.
  2798. * Will be overwrite.
  2799. * @param {Number} dt time in seconds
  2800. */
  2801. update:function (dt) {},
  2802. /**
  2803. * Returns a reversed action.
  2804. * @return {cc.DelayTime}
  2805. */
  2806. reverse:function () {
  2807. var action = new cc.DelayTime(this._duration);
  2808. this._cloneDecoration(action);
  2809. this._reverseEaseList(action);
  2810. return action;
  2811. },
  2812. /**
  2813. * returns a new clone of the action
  2814. * @returns {cc.DelayTime}
  2815. */
  2816. clone:function () {
  2817. var action = new cc.DelayTime();
  2818. this._cloneDecoration(action);
  2819. action.initWithDuration(this._duration);
  2820. return action;
  2821. }
  2822. });
  2823. /**
  2824. * Delays the action a certain amount of seconds
  2825. * @function
  2826. * @param {Number} d duration in seconds
  2827. * @return {cc.DelayTime}
  2828. * @example
  2829. * // example
  2830. * var delay = cc.delayTime(1);
  2831. */
  2832. cc.delayTime = function (d) {
  2833. return new cc.DelayTime(d);
  2834. };
  2835. /**
  2836. * Please use cc.delayTime instead.
  2837. * Delays the action a certain amount of seconds
  2838. * @static
  2839. * @deprecated since v3.0 please use cc.delaTime instead.
  2840. * @param {Number} d duration in seconds
  2841. * @return {cc.DelayTime}
  2842. */
  2843. cc.DelayTime.create = cc.delayTime;
  2844. /**
  2845. * <p>
  2846. * Executes an action in reverse order, from time=duration to time=0 <br/>
  2847. * @warning Use this action carefully. This action is not sequenceable. <br/>
  2848. * Use it as the default "reversed" method of your own actions, but using it outside the "reversed" <br/>
  2849. * scope is not recommended.
  2850. * </p>
  2851. * @class
  2852. * @extends cc.ActionInterval
  2853. * @param {cc.FiniteTimeAction} action
  2854. * @example
  2855. * var reverse = new cc.ReverseTime(this);
  2856. */
  2857. cc.ReverseTime = cc.ActionInterval.extend(/** @lends cc.ReverseTime# */{
  2858. _other:null,
  2859. /**
  2860. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  2861. * @param {cc.FiniteTimeAction} action
  2862. */
  2863. ctor:function (action) {
  2864. cc.ActionInterval.prototype.ctor.call(this);
  2865. this._other = null;
  2866. action && this.initWithAction(action);
  2867. },
  2868. /**
  2869. * @param {cc.FiniteTimeAction} action
  2870. * @return {Boolean}
  2871. */
  2872. initWithAction:function (action) {
  2873. if(!action)
  2874. throw "cc.ReverseTime.initWithAction(): action must be non null";
  2875. if(action == this._other)
  2876. throw "cc.ReverseTime.initWithAction(): the action was already passed in.";
  2877. if (cc.ActionInterval.prototype.initWithDuration.call(this, action._duration)) {
  2878. // Don't leak if action is reused
  2879. this._other = action;
  2880. return true;
  2881. }
  2882. return false;
  2883. },
  2884. /**
  2885. * returns a new clone of the action
  2886. * @returns {cc.ReverseTime}
  2887. */
  2888. clone:function () {
  2889. var action = new cc.ReverseTime();
  2890. this._cloneDecoration(action);
  2891. action.initWithAction(this._other.clone());
  2892. return action;
  2893. },
  2894. /**
  2895. * Start the action with target.
  2896. * @param {cc.Node} target
  2897. */
  2898. startWithTarget:function (target) {
  2899. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  2900. this._other.startWithTarget(target);
  2901. },
  2902. /**
  2903. * Called once per frame. Time is the number of seconds of a frame interval.
  2904. * @param {Number} dt time in seconds
  2905. */
  2906. update:function (dt) {
  2907. dt = this._computeEaseTime(dt);
  2908. if (this._other)
  2909. this._other.update(1 - dt);
  2910. },
  2911. /**
  2912. * Returns a reversed action.
  2913. * @return {cc.ActionInterval}
  2914. */
  2915. reverse:function () {
  2916. return this._other.clone();
  2917. },
  2918. /**
  2919. * Stop the action
  2920. */
  2921. stop:function () {
  2922. this._other.stop();
  2923. cc.Action.prototype.stop.call(this);
  2924. }
  2925. });
  2926. /**
  2927. * Executes an action in reverse order, from time=duration to time=0.
  2928. * @function
  2929. * @param {cc.FiniteTimeAction} action
  2930. * @return {cc.ReverseTime}
  2931. * @example
  2932. * // example
  2933. * var reverse = cc.reverseTime(this);
  2934. */
  2935. cc.reverseTime = function (action) {
  2936. return new cc.ReverseTime(action);
  2937. };
  2938. /**
  2939. * Please use cc.reverseTime instead.
  2940. * Executes an action in reverse order, from time=duration to time=0.
  2941. * @static
  2942. * @deprecated since v3.0 please use cc.reverseTime instead.
  2943. * @param {cc.FiniteTimeAction} action
  2944. * @return {cc.ReverseTime}
  2945. */
  2946. cc.ReverseTime.create = cc.reverseTime;
  2947. /** Animates a sprite given the name of an Animation
  2948. * @class
  2949. * @extends cc.ActionInterval
  2950. * @param {cc.Animation} animation
  2951. * @example
  2952. * // create the animation with animation
  2953. * var anim = new cc.Animate(dance_grey);
  2954. */
  2955. cc.Animate = cc.ActionInterval.extend(/** @lends cc.Animate# */{
  2956. _animation:null,
  2957. _nextFrame:0,
  2958. _origFrame:null,
  2959. _executedLoops:0,
  2960. _splitTimes:null,
  2961. /**
  2962. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  2963. * create the animate with animation.
  2964. * @param {cc.Animation} animation
  2965. */
  2966. ctor:function (animation) {
  2967. cc.ActionInterval.prototype.ctor.call(this);
  2968. this._splitTimes = [];
  2969. animation && this.initWithAnimation(animation);
  2970. },
  2971. /**
  2972. * @return {cc.Animation}
  2973. */
  2974. getAnimation:function () {
  2975. return this._animation;
  2976. },
  2977. /**
  2978. * @param {cc.Animation} animation
  2979. */
  2980. setAnimation:function (animation) {
  2981. this._animation = animation;
  2982. },
  2983. /**
  2984. * @param {cc.Animation} animation
  2985. * @return {Boolean}
  2986. */
  2987. initWithAnimation:function (animation) {
  2988. if(!animation)
  2989. throw "cc.Animate.initWithAnimation(): animation must be non-NULL";
  2990. var singleDuration = animation.getDuration();
  2991. if (this.initWithDuration(singleDuration * animation.getLoops())) {
  2992. this._nextFrame = 0;
  2993. this.setAnimation(animation);
  2994. this._origFrame = null;
  2995. this._executedLoops = 0;
  2996. var locTimes = this._splitTimes;
  2997. locTimes.length = 0;
  2998. var accumUnitsOfTime = 0;
  2999. var newUnitOfTimeValue = singleDuration / animation.getTotalDelayUnits();
  3000. var frames = animation.getFrames();
  3001. cc.arrayVerifyType(frames, cc.AnimationFrame);
  3002. for (var i = 0; i < frames.length; i++) {
  3003. var frame = frames[i];
  3004. var value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;
  3005. accumUnitsOfTime += frame.getDelayUnits();
  3006. locTimes.push(value);
  3007. }
  3008. return true;
  3009. }
  3010. return false;
  3011. },
  3012. /**
  3013. * returns a new clone of the action
  3014. * @returns {cc.Animate}
  3015. */
  3016. clone:function () {
  3017. var action = new cc.Animate();
  3018. this._cloneDecoration(action);
  3019. action.initWithAnimation(this._animation.clone());
  3020. return action;
  3021. },
  3022. /**
  3023. * Start the action with target.
  3024. * @param {cc.Sprite} target
  3025. */
  3026. startWithTarget:function (target) {
  3027. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  3028. if (this._animation.getRestoreOriginalFrame())
  3029. this._origFrame = target.displayFrame();
  3030. this._nextFrame = 0;
  3031. this._executedLoops = 0;
  3032. },
  3033. /**
  3034. * Called once per frame. Time is the number of seconds of a frame interval.
  3035. * @param {Number} dt
  3036. */
  3037. update:function (dt) {
  3038. dt = this._computeEaseTime(dt);
  3039. // if t==1, ignore. Animation should finish with t==1
  3040. if (dt < 1.0) {
  3041. dt *= this._animation.getLoops();
  3042. // new loop? If so, reset frame counter
  3043. var loopNumber = 0 | dt;
  3044. if (loopNumber > this._executedLoops) {
  3045. this._nextFrame = 0;
  3046. this._executedLoops++;
  3047. }
  3048. // new t for animations
  3049. dt = dt % 1.0;
  3050. }
  3051. var frames = this._animation.getFrames();
  3052. var numberOfFrames = frames.length, locSplitTimes = this._splitTimes;
  3053. for (var i = this._nextFrame; i < numberOfFrames; i++) {
  3054. if (locSplitTimes[i] <= dt) {
  3055. this.target.setSpriteFrame(frames[i].getSpriteFrame());
  3056. this._nextFrame = i + 1;
  3057. } else {
  3058. // Issue 1438. Could be more than one frame per tick, due to low frame rate or frame delta < 1/FPS
  3059. break;
  3060. }
  3061. }
  3062. },
  3063. /**
  3064. * Returns a reversed action.
  3065. * @return {cc.Animate}
  3066. */
  3067. reverse:function () {
  3068. var locAnimation = this._animation;
  3069. var oldArray = locAnimation.getFrames();
  3070. var newArray = [];
  3071. cc.arrayVerifyType(oldArray, cc.AnimationFrame);
  3072. if (oldArray.length > 0) {
  3073. for (var i = oldArray.length - 1; i >= 0; i--) {
  3074. var element = oldArray[i];
  3075. if (!element)
  3076. break;
  3077. newArray.push(element.clone());
  3078. }
  3079. }
  3080. var newAnim = new cc.Animation(newArray, locAnimation.getDelayPerUnit(), locAnimation.getLoops());
  3081. newAnim.setRestoreOriginalFrame(locAnimation.getRestoreOriginalFrame());
  3082. var action = new cc.Animate(newAnim);
  3083. this._cloneDecoration(action);
  3084. this._reverseEaseList(action);
  3085. return action;
  3086. },
  3087. /**
  3088. * stop the action
  3089. */
  3090. stop:function () {
  3091. if (this._animation.getRestoreOriginalFrame() && this.target)
  3092. this.target.setSpriteFrame(this._origFrame);
  3093. cc.Action.prototype.stop.call(this);
  3094. }
  3095. });
  3096. /**
  3097. * create the animate with animation
  3098. * @function
  3099. * @param {cc.Animation} animation
  3100. * @return {cc.Animate}
  3101. * @example
  3102. * // example
  3103. * // create the animation with animation
  3104. * var anim = cc.animate(dance_grey);
  3105. */
  3106. cc.animate = function (animation) {
  3107. return new cc.Animate(animation);
  3108. };
  3109. /**
  3110. * Please use cc.animate instead
  3111. * create the animate with animation
  3112. * @static
  3113. * @deprecated since v3.0 please use cc.animate instead.
  3114. * @param {cc.Animation} animation
  3115. * @return {cc.Animate}
  3116. */
  3117. cc.Animate.create = cc.animate;
  3118. /**
  3119. * <p>
  3120. * Overrides the target of an action so that it always runs on the target<br/>
  3121. * specified at action creation rather than the one specified by runAction.
  3122. * </p>
  3123. * @class
  3124. * @extends cc.ActionInterval
  3125. * @param {cc.Node} target
  3126. * @param {cc.FiniteTimeAction} action
  3127. */
  3128. cc.TargetedAction = cc.ActionInterval.extend(/** @lends cc.TargetedAction# */{
  3129. _action:null,
  3130. _forcedTarget:null,
  3131. /**
  3132. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  3133. * Create an action with the specified action and forced target.
  3134. * @param {cc.Node} target
  3135. * @param {cc.FiniteTimeAction} action
  3136. */
  3137. ctor: function (target, action) {
  3138. cc.ActionInterval.prototype.ctor.call(this);
  3139. action && this.initWithTarget(target, action);
  3140. },
  3141. /**
  3142. * Init an action with the specified action and forced target
  3143. * @param {cc.Node} target
  3144. * @param {cc.FiniteTimeAction} action
  3145. * @return {Boolean}
  3146. */
  3147. initWithTarget:function (target, action) {
  3148. if (this.initWithDuration(action._duration)) {
  3149. this._forcedTarget = target;
  3150. this._action = action;
  3151. return true;
  3152. }
  3153. return false;
  3154. },
  3155. /**
  3156. * returns a new clone of the action
  3157. * @returns {cc.TargetedAction}
  3158. */
  3159. clone:function () {
  3160. var action = new cc.TargetedAction();
  3161. this._cloneDecoration(action);
  3162. action.initWithTarget(this._forcedTarget, this._action.clone());
  3163. return action;
  3164. },
  3165. /**
  3166. * Start the action with target.
  3167. * @param {cc.Node} target
  3168. */
  3169. startWithTarget:function (target) {
  3170. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  3171. this._action.startWithTarget(this._forcedTarget);
  3172. },
  3173. /**
  3174. * stop the action
  3175. */
  3176. stop:function () {
  3177. this._action.stop();
  3178. },
  3179. /**
  3180. * Called once per frame. Time is the number of seconds of a frame interval.
  3181. * @param {Number} dt
  3182. */
  3183. update:function (dt) {
  3184. dt = this._computeEaseTime(dt);
  3185. this._action.update(dt);
  3186. },
  3187. /**
  3188. * return the target that the action will be forced to run with
  3189. * @return {cc.Node}
  3190. */
  3191. getForcedTarget:function () {
  3192. return this._forcedTarget;
  3193. },
  3194. /**
  3195. * set the target that the action will be forced to run with
  3196. * @param {cc.Node} forcedTarget
  3197. */
  3198. setForcedTarget:function (forcedTarget) {
  3199. if (this._forcedTarget != forcedTarget)
  3200. this._forcedTarget = forcedTarget;
  3201. }
  3202. });
  3203. /**
  3204. * Create an action with the specified action and forced target
  3205. * @function
  3206. * @param {cc.Node} target
  3207. * @param {cc.FiniteTimeAction} action
  3208. * @return {cc.TargetedAction}
  3209. */
  3210. cc.targetedAction = function (target, action) {
  3211. return new cc.TargetedAction(target, action);
  3212. };
  3213. /**
  3214. * Please use cc.targetedAction instead
  3215. * Create an action with the specified action and forced target
  3216. * @static
  3217. * @deprecated since v3.0 please use cc.targetedAction instead.
  3218. * @param {cc.Node} target
  3219. * @param {cc.FiniteTimeAction} action
  3220. * @return {cc.TargetedAction}
  3221. */
  3222. cc.TargetedAction.create = cc.targetedAction;