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