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 * Base class for Easing actions 29 * @class 30 * @extends cc.ActionInterval 31 */ 32 33 cc.ActionEase = cc.ActionInterval.extend(/** @lends cc.ActionEase# */{ 34 _inner:null, 35 36 /** 37 * creates the action of ActionEase 38 * 39 * Constructor of cc.ActionEase 40 * @param {cc.ActionInterval} action 41 * 42 * @example 43 * var moveEase = new cc.ActionEase(action); 44 */ 45 ctor: function (action) { 46 cc.ActionInterval.prototype.ctor.call(this); 47 action && this.initWithAction(action); 48 }, 49 50 /** initializes the action 51 * @param {cc.ActionInterval} action 52 * @return {Boolean} 53 */ 54 initWithAction:function (action) { 55 if(!action) 56 throw "cc.ActionEase.initWithAction(): action must be non nil"; 57 58 if (this.initWithDuration(action.getDuration())) { 59 this._inner = action; 60 return true; 61 } 62 return false; 63 }, 64 65 clone:function(){ 66 var action = new cc.ActionEase(); 67 action.initWithAction(this._inner.clone()); 68 return action; 69 }, 70 71 /** 72 * @param {cc.Node} target 73 */ 74 startWithTarget:function (target) { 75 cc.ActionInterval.prototype.startWithTarget.call(this, target); 76 this._inner.startWithTarget(this.target); 77 }, 78 79 /** 80 * Stop the action. 81 */ 82 stop:function () { 83 this._inner.stop(); 84 cc.ActionInterval.prototype.stop.call(this); 85 }, 86 87 /** 88 * @param {Number} time1 89 */ 90 update:function (time1) { 91 this._inner.update(time1); 92 }, 93 94 /** 95 * @return {cc.ActionInterval} 96 */ 97 reverse:function () { 98 return cc.ActionEase.create(this._inner.reverse()); 99 }, 100 101 getInnerAction:function(){ 102 return this._inner; 103 } 104 }); 105 106 /** creates the action of ActionEase 107 * @param {cc.ActionInterval} action 108 * @return {cc.ActionEase} 109 * @example 110 * // example 111 * var moveEase = cc.ActionEase.create(action); 112 */ 113 cc.ActionEase.create = function (action) { 114 return new cc.ActionEase(action); 115 }; 116 117 /** 118 * Base class for Easing actions with rate parameters 119 * @class 120 * @extends cc.ActionEase 121 */ 122 cc.EaseRateAction = cc.ActionEase.extend(/** @lends cc.EaseRateAction# */{ 123 _rate:0, 124 125 /** 126 * Creates the action with the inner action and the rate parameter 127 * 128 * Constructor of cc.EaseRateAction 129 * @param {cc.ActionInterval} action 130 * @param {Number} rate 131 * 132 * @example 133 * // example 134 * var moveEaseRateAction = new cc.EaseRateAction(action, 3.0); 135 */ 136 ctor: function(action, rate){ 137 cc.ActionEase.prototype.ctor.call(this); 138 139 rate !== undefined && this.initWithAction(action, rate); 140 }, 141 142 /** set rate value for the actions 143 * @param {Number} rate 144 */ 145 setRate:function (rate) { 146 this._rate = rate; 147 }, 148 149 /** get rate value for the actions 150 * @return {Number} 151 */ 152 getRate:function () { 153 return this._rate; 154 }, 155 156 /** 157 * Initializes the action with the inner action and the rate parameter 158 * @param {cc.ActionInterval} action 159 * @param {Number} rate 160 * @return {Boolean} 161 */ 162 initWithAction:function (action, rate) { 163 if (cc.ActionEase.prototype.initWithAction.call(this, action)) { 164 this._rate = rate; 165 return true; 166 } 167 return false; 168 }, 169 170 clone:function(){ 171 var action = new cc.EaseRateAction(); 172 action.initWithAction(this._inner.clone(), this._rate); 173 return action; 174 }, 175 176 /** 177 * @return {cc.EaseRateAction} 178 */ 179 reverse:function () { 180 return cc.EaseRateAction.create(this._inner.reverse(), 1 / this._rate); 181 } 182 }); 183 184 /** Creates the action with the inner action and the rate parameter 185 * @param {cc.ActionInterval} action 186 * @param {Number} rate 187 * @return {cc.EaseRateAction} 188 * @example 189 * // example 190 * var moveEaseRateAction = cc.EaseRateAction.create(action, 3.0); 191 */ 192 cc.EaseRateAction.create = function (action, rate) { 193 return new cc.EaseRateAction(action, rate); 194 }; 195 196 /** 197 * cc.EaseIn action with a rate 198 * @class 199 * @extends cc.EaseRateAction 200 */ 201 cc.EaseIn = cc.EaseRateAction.extend(/** @lends cc.EaseIn# */{ 202 /** 203 * @param {Number} time1 204 */ 205 update:function (time1) { 206 this._inner.update(Math.pow(time1, this._rate)); 207 }, 208 209 /** 210 * @return {cc.EaseIn} 211 */ 212 reverse:function () { 213 return cc.EaseIn.create(this._inner.reverse(), 1 / this._rate); 214 }, 215 216 clone:function(){ 217 var action = new cc.EaseIn(); 218 action.initWithAction(this._inner.clone(), this._rate); 219 return action; 220 } 221 }); 222 223 /** Creates the action with the inner action and the rate parameter 224 * @param {cc.ActionInterval} action 225 * @param {Number} rate 226 * @return {cc.EaseIn} 227 * @example 228 * // example 229 * var moveEaseIn = cc.EaseIn.create(action, 3.0); 230 */ 231 cc.EaseIn.create = function (action, rate) { 232 return new cc.EaseIn(action, rate); 233 }; 234 235 cc.easeIn = function (rate) { 236 return { 237 _rate: rate, 238 easing: function (dt) { 239 return Math.pow(dt, this._rate); 240 }, 241 reverse: function(){ 242 return cc.easeIn(1 / this._rate); 243 } 244 }; 245 }; 246 247 /** 248 * cc.EaseOut action with a rate 249 * @class 250 * @extends cc.EaseRateAction 251 */ 252 cc.EaseOut = cc.EaseRateAction.extend(/** @lends cc.EaseOut# */{ 253 /** 254 * @param {Number} time1 255 */ 256 update:function (time1) { 257 this._inner.update(Math.pow(time1, 1 / this._rate)); 258 }, 259 260 /** 261 * @return {cc.EaseOut} 262 */ 263 reverse:function () { 264 return cc.EaseOut.create(this._inner.reverse(), 1 / this._rate); 265 }, 266 267 clone:function(){ 268 var action = new cc.EaseOut(); 269 action.initWithAction(this._inner.clone(),this._rate); 270 return action; 271 } 272 }); 273 274 /** Creates the action with the inner action and the rate parameter 275 * @param {cc.ActionInterval} action 276 * @param {Number} rate 277 * @return {cc.EaseOut} 278 * @example 279 * // example 280 * var moveEaseOut = cc.EaseOut.create(action, 3.0); 281 */ 282 cc.EaseOut.create = function (action, rate) { 283 return new cc.EaseOut(action, rate); 284 }; 285 286 cc.easeOut = function (rate) { 287 return { 288 _rate: rate, 289 easing: function (dt) { 290 return Math.pow(dt, 1 / this._rate); 291 }, 292 reverse: function(){ 293 return cc.easeOut(1 / this._rate) 294 } 295 }; 296 }; 297 298 /** 299 * cc.EaseInOut action with a rate 300 * @class 301 * @extends cc.EaseRateAction 302 */ 303 cc.EaseInOut = cc.EaseRateAction.extend(/** @lends cc.EaseInOut# */{ 304 /** 305 * @param {Number} time1 306 */ 307 update:function (time1) { 308 time1 *= 2; 309 if (time1 < 1) 310 this._inner.update(0.5 * Math.pow(time1, this._rate)); 311 else 312 this._inner.update(1.0 - 0.5 * Math.pow(2 - time1, this._rate)); 313 }, 314 315 clone:function(){ 316 var action = new cc.EaseInOut(); 317 action.initWithAction(this._inner.clone(), this._rate); 318 return action; 319 }, 320 321 /** 322 * @return {cc.EaseInOut} 323 */ 324 reverse:function () { 325 return cc.EaseInOut.create(this._inner.reverse(), this._rate); 326 } 327 }); 328 329 /** Creates the action with the inner action and the rate parameter 330 * @param {cc.ActionInterval} action 331 * @param {Number} rate 332 * @return {cc.EaseInOut} 333 * @example 334 * // example 335 * var moveEaseInOut = cc.EaseInOut.create(action, 3.0); 336 */ 337 cc.EaseInOut.create = function (action, rate) { 338 return new cc.EaseInOut(action, rate); 339 }; 340 341 cc.easeInOut = function (rate) { 342 return { 343 _rate: rate, 344 easing: function (dt) { 345 dt *= 2; 346 if (dt < 1) 347 return 0.5 * Math.pow(dt, this._rate); 348 else 349 return 1.0 - 0.5 * Math.pow(2 - dt, this._rate); 350 }, 351 reverse: function(){ 352 return cc.easeInOut(this._rate); 353 } 354 }; 355 }; 356 357 /** 358 * cc.Ease Exponential In 359 * @class 360 * @extends cc.ActionEase 361 */ 362 cc.EaseExponentialIn = cc.ActionEase.extend(/** @lends cc.EaseExponentialIn# */{ 363 /** 364 * @param {Number} time1 365 */ 366 update:function (time1) { 367 this._inner.update(time1 === 0 ? 0 : Math.pow(2, 10 * (time1 - 1))); 368 }, 369 370 /** 371 * @return {cc.EaseExponentialOut} 372 */ 373 reverse:function () { 374 return cc.EaseExponentialOut.create(this._inner.reverse()); 375 }, 376 377 clone:function(){ 378 var action = new cc.EaseExponentialIn(); 379 action.initWithAction(this._inner.clone()); 380 return action; 381 } 382 }); 383 384 /** creates the action 385 * @param {cc.ActionInterval} action 386 * @return {cc.EaseExponentialIn} 387 * @example 388 * // example 389 * var moveEaseExponentialIn = cc.EaseExponentialIn.create(action); 390 */ 391 cc.EaseExponentialIn.create = function (action) { 392 return new cc.EaseExponentialIn(action); 393 }; 394 395 cc._easeExponentialInObj = { 396 easing: function(dt){ 397 return dt === 0 ? 0 : Math.pow(2, 10 * (dt - 1)); 398 }, 399 reverse: function(){ 400 return cc._easeExponentialOutObj; 401 } 402 }; 403 cc.easeExponentialIn = function(){ 404 return cc._easeExponentialInObj; 405 }; 406 407 /** 408 * Ease Exponential Out 409 * @class 410 * @extends cc.ActionEase 411 */ 412 cc.EaseExponentialOut = cc.ActionEase.extend(/** @lends cc.EaseExponentialOut# */{ 413 /** 414 * @param {Number} time1 415 */ 416 update:function (time1) { 417 this._inner.update(time1 == 1 ? 1 : (-(Math.pow(2, -10 * time1)) + 1)); 418 }, 419 420 /** 421 * @return {cc.EaseExponentialIn} 422 */ 423 reverse:function () { 424 return cc.EaseExponentialIn.create(this._inner.reverse()); 425 }, 426 427 clone:function(){ 428 var action = new cc.EaseExponentialOut(); 429 action.initWithAction(this._inner.clone()); 430 return action; 431 } 432 }); 433 434 /** creates the action 435 * @param {cc.ActionInterval} action 436 * @return {cc.EaseExponentialOut} 437 * @example 438 * // example 439 * var moveEaseExponentialOut = cc.EaseExponentialOut.create(action); 440 */ 441 cc.EaseExponentialOut.create = function (action) { 442 return new cc.EaseExponentialOut(action); 443 }; 444 445 cc._easeExponentialOutObj = { 446 easing: function(dt){ 447 return dt == 1 ? 1 : (-(Math.pow(2, -10 * dt)) + 1); 448 }, 449 reverse: function(){ 450 return cc._easeExponentialInObj; 451 } 452 }; 453 cc.easeExponentialOut = function(){ 454 return cc._easeExponentialOutObj; 455 }; 456 457 /** 458 * Ease Exponential InOut 459 * @class 460 * @extends cc.ActionEase 461 */ 462 cc.EaseExponentialInOut = cc.ActionEase.extend(/** @lends cc.EaseExponentialInOut# */{ 463 /** 464 * @param {Number} time 465 */ 466 update:function (time) { 467 if( time != 1 && time !== 0) { 468 time *= 2; 469 if (time < 1) 470 time = 0.5 * Math.pow(2, 10 * (time - 1)); 471 else 472 time = 0.5 * (-Math.pow(2, -10 * (time - 1)) + 2); 473 } 474 this._inner.update(time); 475 }, 476 477 /** 478 * @return {cc.EaseExponentialInOut} 479 */ 480 reverse:function () { 481 return cc.EaseExponentialInOut.create(this._inner.reverse()); 482 }, 483 484 clone:function(){ 485 var action = new cc.EaseExponentialInOut(); 486 action.initWithAction(this._inner.clone()); 487 return action; 488 } 489 }); 490 491 /** creates an EaseExponentialInOut action 492 * @param {cc.ActionInterval} action 493 * @return {cc.EaseExponentialInOut} 494 * @example 495 * // example 496 * var moveEaseExponentialInOut = cc.EaseExponentialInOut.create(action); 497 */ 498 cc.EaseExponentialInOut.create = function (action) { 499 return new cc.EaseExponentialInOut(action); 500 }; 501 502 cc._easeExponentialInOutObj = { 503 easing: function(dt){ 504 if( dt !== 1 && dt !== 0) { 505 dt *= 2; 506 if (dt < 1) 507 return 0.5 * Math.pow(2, 10 * (dt - 1)); 508 else 509 return 0.5 * (-Math.pow(2, -10 * (dt - 1)) + 2); 510 } 511 return dt; 512 }, 513 reverse: function(){ 514 return cc._easeExponentialInOutObj; 515 } 516 }; 517 cc.easeExponentialInOut = function(){ 518 return cc._easeExponentialInOutObj; 519 }; 520 521 /** 522 * Ease Sine In 523 * @class 524 * @extends cc.ActionEase 525 */ 526 cc.EaseSineIn = cc.ActionEase.extend(/** @lends cc.EaseSineIn# */{ 527 /** 528 * @param {Number} time1 529 */ 530 update:function (time1) { 531 time1 = time1===0 || time1===1 ? time1 : -1 * Math.cos(time1 * Math.PI / 2) + 1; 532 this._inner.update(time1); 533 }, 534 535 /** 536 * @return {cc.EaseSineOut} 537 */ 538 reverse:function () { 539 return cc.EaseSineOut.create(this._inner.reverse()); 540 }, 541 542 clone:function(){ 543 var action = new cc.EaseSineIn(); 544 action.initWithAction(this._inner.clone()); 545 return action; 546 } 547 }); 548 549 /** creates an EaseSineIn action 550 * @param {cc.ActionInterval} action 551 * @return {cc.EaseSineIn} 552 * @example 553 * // example 554 * var moveSineIn = cc.EaseSineIn.create(action); 555 */ 556 cc.EaseSineIn.create = function (action) { 557 return new cc.EaseSineIn(action); 558 }; 559 560 cc._easeSineInObj = { 561 easing: function(dt){ 562 return (dt===0 || dt===1) ? dt : -1 * Math.cos(dt * Math.PI / 2) + 1; 563 }, 564 reverse: function(){ 565 return cc._easeSineOutObj; 566 } 567 }; 568 cc.easeSineIn = function(){ 569 return cc._easeSineInObj; 570 }; 571 572 /** 573 * Ease Sine Out 574 * @class 575 * @extends cc.ActionEase 576 */ 577 cc.EaseSineOut = cc.ActionEase.extend(/** @lends cc.EaseSineOut# */{ 578 /** 579 * @param {Number} time1 580 */ 581 update:function (time1) { 582 time1 = time1===0 || time1===1 ? time1 : Math.sin(time1 * Math.PI / 2); 583 this._inner.update(time1); 584 }, 585 586 /** 587 * @return {cc.EaseSineIn} 588 */ 589 reverse:function () { 590 return cc.EaseSineIn.create(this._inner.reverse()); 591 }, 592 593 clone:function(){ 594 var action = new cc.EaseSineOut(); 595 action.initWithAction(this._inner.clone()); 596 return action; 597 } 598 }); 599 600 /** creates an EaseSineOut action 601 * @param {cc.ActionInterval} action 602 * @return {cc.EaseSineOut} 603 * @example 604 * // example 605 * var moveEaseOut = cc.EaseSineOut.create(action); 606 */ 607 cc.EaseSineOut.create = function (action) { 608 return new cc.EaseSineOut(action); 609 }; 610 611 cc._easeSineOutObj = { 612 easing: function(dt){ 613 return (dt===0 || dt==1) ? dt : Math.sin(dt * Math.PI / 2); 614 }, 615 reverse: function(){ 616 return cc._easeSineInObj; 617 } 618 }; 619 cc.easeSineOut = function(){ 620 return cc._easeSineOutObj; 621 }; 622 623 /** 624 * Ease Sine InOut 625 * @class 626 * @extends cc.ActionEase 627 */ 628 cc.EaseSineInOut = cc.ActionEase.extend(/** @lends cc.EaseSineInOut# */{ 629 /** 630 * @param {Number} time1 631 */ 632 update:function (time1) { 633 time1 = time1===0 || time1===1 ? time1 : -0.5 * (Math.cos(Math.PI * time1) - 1); 634 this._inner.update(time1); 635 }, 636 637 clone:function(){ 638 var action = new cc.EaseSineInOut(); 639 action.initWithAction(this._inner.clone()); 640 return action; 641 }, 642 643 /** 644 * @return {cc.EaseSineInOut} 645 */ 646 reverse:function () { 647 return cc.EaseSineInOut.create(this._inner.reverse()); 648 } 649 }); 650 651 /** creates the action 652 * @param {cc.ActionInterval} action 653 * @return {cc.EaseSineInOut} 654 * @example 655 * // example 656 * var moveEaseSineInOut = cc.EaseSineInOut.create(action); 657 */ 658 cc.EaseSineInOut.create = function (action) { 659 return new cc.EaseSineInOut(action); 660 }; 661 662 cc._easeSineInOutObj = { 663 easing: function(dt){ 664 return (dt === 0 || dt === 1) ? dt : -0.5 * (Math.cos(Math.PI * dt) - 1); 665 }, 666 reverse: function(){ 667 return cc._easeSineInOutObj; 668 } 669 }; 670 cc.easeSineInOut = function(){ 671 return cc._easeSineInOutObj; 672 }; 673 674 /** 675 * Ease Elastic abstract class 676 * @class 677 * @extends cc.ActionEase 678 */ 679 cc.EaseElastic = cc.ActionEase.extend(/** @lends cc.EaseElastic# */{ 680 _period: 0.3, 681 682 /** Creates the action with the inner action and the period in radians (default is 0.3) 683 * 684 * Constructor of cc.EaseElastic 685 * @param {cc.ActionInterval} action 686 * @param {Number} [period=0.3] 687 * 688 * @example 689 * // example 690 * var moveEaseElastic = new cc.EaseElastic(action, 3.0); 691 */ 692 ctor:function(action, period){ 693 cc.ActionEase.prototype.ctor.call(this); 694 695 action && this.initWithAction(action, period); 696 }, 697 698 /** get period of the wave in radians. default is 0.3 699 * @return {Number} 700 */ 701 getPeriod:function () { 702 return this._period; 703 }, 704 705 /** set period of the wave in radians. 706 * @param {Number} period 707 */ 708 setPeriod:function (period) { 709 this._period = period; 710 }, 711 712 /** Initializes the action with the inner action and the period in radians (default is 0.3) 713 * @param {cc.ActionInterval} action 714 * @param {Number} [period=0.3] 715 * @return {Boolean} 716 */ 717 initWithAction:function (action, period) { 718 cc.ActionEase.prototype.initWithAction.call(this, action); 719 this._period = (period == null) ? 0.3 : period; 720 return true; 721 }, 722 723 /** 724 * @return {Null} 725 */ 726 reverse:function () { 727 cc.log("cc.EaseElastic.reverse(): it should be overridden in subclass."); 728 return null; 729 }, 730 731 clone:function(){ 732 var action = new cc.EaseElastic(); 733 action.initWithAction(this._inner.clone(), this._period); 734 return action; 735 } 736 }); 737 738 /** Creates the action with the inner action and the period in radians (default is 0.3) 739 * @param {cc.ActionInterval} action 740 * @param {Number} [period=0.3] 741 * @return {cc.EaseElastic} 742 * @example 743 * // example 744 * var moveEaseElastic = cc.EaseElastic.create(action, 3.0); 745 */ 746 cc.EaseElastic.create = function (action, period) { 747 return new cc.EaseElastic(action, period); 748 }; 749 750 /** 751 * Ease Elastic In action. 752 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 753 * @class 754 * @extends cc.EaseElastic 755 */ 756 cc.EaseElasticIn = cc.EaseElastic.extend(/** @lends cc.EaseElasticIn# */{ 757 /** 758 * @param {Number} time1 759 */ 760 update:function (time1) { 761 var newT = 0; 762 if (time1 === 0 || time1 === 1) { 763 newT = time1; 764 } else { 765 var s = this._period / 4; 766 time1 = time1 - 1; 767 newT = -Math.pow(2, 10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / this._period); 768 } 769 this._inner.update(newT); 770 }, 771 772 /** 773 * @return {cc.EaseElasticOut} 774 */ 775 reverse:function () { 776 return cc.EaseElasticOut.create(this._inner.reverse(), this._period); 777 }, 778 779 clone:function(){ 780 var action = new cc.EaseElasticIn(); 781 action.initWithAction(this._inner.clone(), this._period); 782 return action; 783 } 784 }); 785 786 /** Creates the action with the inner action and the period in radians (default is 0.3) 787 * @param {cc.ActionInterval} action 788 * @param {Number} [period=0.3] 789 * @return {cc.EaseElasticIn} 790 * @example 791 * // example 792 * var moveEaseElasticIn = cc.EaseElasticIn.create(action, 3.0); 793 */ 794 cc.EaseElasticIn.create = function (action, period) { 795 return new cc.EaseElasticIn(action, period); 796 }; 797 798 //default ease elastic in object (period = 0.3) 799 cc._easeElasticInObj = { 800 easing:function(dt){ 801 if (dt === 0 || dt === 1) 802 return dt; 803 dt = dt - 1; 804 return -Math.pow(2, 10 * dt) * Math.sin((dt - (0.3 / 4)) * Math.PI * 2 / 0.3); 805 }, 806 reverse:function(){ 807 return cc._easeElasticOutObj; 808 } 809 }; 810 811 cc.easeElasticIn = function (period) { 812 if(period && period !== 0.3){ 813 return { 814 _period: period, 815 easing: function (dt) { 816 if (dt === 0 || dt === 1) 817 return dt; 818 dt = dt - 1; 819 return -Math.pow(2, 10 * dt) * Math.sin((dt - (this._period / 4)) * Math.PI * 2 / this._period); 820 }, 821 /** 822 * @return {cc.EaseElasticIn} 823 */ 824 reverse:function () { 825 return cc.easeElasticOut(this._period); 826 } 827 }; 828 } 829 return cc._easeElasticInObj; 830 }; 831 832 /** 833 * Ease Elastic Out action. 834 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 835 * @class 836 * @extends cc.EaseElastic 837 */ 838 cc.EaseElasticOut = cc.EaseElastic.extend(/** @lends cc.EaseElasticOut# */{ 839 /** 840 * @param {Number} time1 841 */ 842 update:function (time1) { 843 var newT = 0; 844 if (time1 === 0 || time1 == 1) { 845 newT = time1; 846 } else { 847 var s = this._period / 4; 848 newT = Math.pow(2, -10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / this._period) + 1; 849 } 850 851 this._inner.update(newT); 852 }, 853 854 /** 855 * @return {cc.EaseElasticIn} 856 */ 857 reverse:function () { 858 return cc.EaseElasticIn.create(this._inner.reverse(), this._period); 859 }, 860 861 clone:function(){ 862 var action = new cc.EaseElasticOut(); 863 action.initWithAction(this._inner.clone(), this._period); 864 return action; 865 } 866 }); 867 868 /** Creates the action with the inner action and the period in radians (default is 0.3) 869 * @param {cc.ActionInterval} action 870 * @param {Number} [period=0.3] 871 * @return {cc.EaseElasticOut} 872 * @example 873 * // example 874 * var moveEaseElasticOut = cc.EaseElasticOut.create(action, 3.0); 875 */ 876 cc.EaseElasticOut.create = function (action, period) { 877 return new cc.EaseElasticOut(action, period); 878 }; 879 880 //default ease elastic out object (period = 0.3) 881 cc._easeElasticOutObj = { 882 easing: function (dt) { 883 return (dt === 0 || dt === 1) ? dt : Math.pow(2, -10 * dt) * Math.sin((dt - (0.3 / 4)) * Math.PI * 2 / 0.3) + 1; 884 }, 885 reverse:function(){ 886 return cc._easeElasticInObj; 887 } 888 }; 889 890 cc.easeElasticOut = function (period) { 891 if(period && period !== 0.3){ 892 return { 893 _period: period, 894 easing: function (dt) { 895 return (dt === 0 || dt === 1) ? dt : Math.pow(2, -10 * dt) * Math.sin((dt - (this._period / 4)) * Math.PI * 2 / this._period) + 1; 896 }, 897 reverse:function(){ 898 return cc.easeElasticIn(this._period); 899 } 900 }; 901 } 902 return cc._easeElasticOutObj; 903 }; 904 905 /** 906 * Ease Elastic InOut action. 907 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 908 * @class 909 * @extends cc.EaseElastic 910 */ 911 cc.EaseElasticInOut = cc.EaseElastic.extend(/** @lends cc.EaseElasticInOut# */{ 912 /** 913 * @param {Number} time1 914 */ 915 update:function (time1) { 916 var newT = 0; 917 var locPeriod = this._period; 918 if (time1 === 0 || time1 == 1) { 919 newT = time1; 920 } else { 921 time1 = time1 * 2; 922 if (!locPeriod) 923 locPeriod = this._period = 0.3 * 1.5; 924 925 var s = locPeriod / 4; 926 time1 = time1 - 1; 927 if (time1 < 0) 928 newT = -0.5 * Math.pow(2, 10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / locPeriod); 929 else 930 newT = Math.pow(2, -10 * time1) * Math.sin((time1 - s) * Math.PI * 2 / locPeriod) * 0.5 + 1; 931 } 932 this._inner.update(newT); 933 }, 934 935 /** 936 * @return {cc.EaseElasticInOut} 937 */ 938 reverse:function () { 939 return cc.EaseElasticInOut.create(this._inner.reverse(), this._period); 940 }, 941 942 clone:function(){ 943 var action = new cc.EaseElasticInOut(); 944 action.initWithAction(this._inner.clone(), this._period); 945 return action; 946 } 947 }); 948 949 /** Creates the action with the inner action and the period in radians (default is 0.3) 950 * @param {cc.ActionInterval} action 951 * @param {Number} [period=0.3] 952 * @return {cc.EaseElasticInOut} 953 * @example 954 * // example 955 * var moveEaseElasticInOut = cc.EaseElasticInOut.create(action, 3.0); 956 */ 957 cc.EaseElasticInOut.create = function (action, period) { 958 return new cc.EaseElasticInOut(action, period); 959 }; 960 961 cc.easeElasticInOut = function (period) { 962 period = period || 0.3; 963 return { 964 _period: period, 965 easing: function (dt) { 966 var newT = 0; 967 var locPeriod = this._period; 968 if (dt === 0 || dt === 1) { 969 newT = dt; 970 } else { 971 dt = dt * 2; 972 if (!locPeriod) 973 locPeriod = this._period = 0.3 * 1.5; 974 var s = locPeriod / 4; 975 dt = dt - 1; 976 if (dt < 0) 977 newT = -0.5 * Math.pow(2, 10 * dt) * Math.sin((dt - s) * Math.PI * 2 / locPeriod); 978 else 979 newT = Math.pow(2, -10 * dt) * Math.sin((dt - s) * Math.PI * 2 / locPeriod) * 0.5 + 1; 980 } 981 return newT; 982 }, 983 reverse: function(){ 984 return cc.easeElasticInOut(this._period); 985 } 986 }; 987 }; 988 989 /** 990 * cc.EaseBounce abstract class. 991 * @class 992 * @extends cc.ActionEase 993 */ 994 cc.EaseBounce = cc.ActionEase.extend(/** @lends cc.EaseBounce# */{ 995 /** 996 * @param {Number} time1 997 * @return {Number} 998 */ 999 bounceTime:function (time1) { 1000 if (time1 < 1 / 2.75) { 1001 return 7.5625 * time1 * time1; 1002 } else if (time1 < 2 / 2.75) { 1003 time1 -= 1.5 / 2.75; 1004 return 7.5625 * time1 * time1 + 0.75; 1005 } else if (time1 < 2.5 / 2.75) { 1006 time1 -= 2.25 / 2.75; 1007 return 7.5625 * time1 * time1 + 0.9375; 1008 } 1009 1010 time1 -= 2.625 / 2.75; 1011 return 7.5625 * time1 * time1 + 0.984375; 1012 }, 1013 1014 clone:function(){ 1015 var action = new cc.EaseBounce(); 1016 action.initWithAction(this._inner.clone()); 1017 return action; 1018 }, 1019 1020 /** 1021 * @return {cc.EaseBounce} 1022 */ 1023 reverse:function () { 1024 return cc.EaseBounce.create(this._inner.reverse()); 1025 } 1026 }); 1027 1028 /** creates an ease bounce action 1029 * @param {cc.ActionInterval} action 1030 * @return {cc.EaseBounce} 1031 * @example 1032 * // example 1033 * var moveEaseBounce = cc.EaseBounce.create(action); 1034 */ 1035 cc.EaseBounce.create = function (action) { 1036 return new cc.EaseBounce(action); 1037 }; 1038 1039 /** 1040 * cc.EaseBounceIn action. 1041 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 1042 * @class 1043 * @extends cc.EaseBounce 1044 */ 1045 cc.EaseBounceIn = cc.EaseBounce.extend(/** @lends cc.EaseBounceIn# */{ 1046 /** 1047 * @param {Number} time1 1048 */ 1049 update:function (time1) { 1050 var newT = 1 - this.bounceTime(1 - time1); 1051 this._inner.update(newT); 1052 }, 1053 1054 /** 1055 * @return {cc.EaseBounceOut} 1056 */ 1057 reverse:function () { 1058 return cc.EaseBounceOut.create(this._inner.reverse()); 1059 }, 1060 1061 clone:function(){ 1062 var action = new cc.EaseBounceIn(); 1063 action.initWithAction(this._inner.clone()); 1064 return action; 1065 } 1066 }); 1067 1068 /** creates the action 1069 * @param {cc.ActionInterval} action 1070 * @return {cc.EaseBounceIn} 1071 * @example 1072 * // example 1073 * var moveEaseBounceIn = cc.EaseBounceIn.create(action); 1074 */ 1075 cc.EaseBounceIn.create = function (action) { 1076 return new cc.EaseBounceIn(action); 1077 }; 1078 1079 cc._bounceTime = function (time1) { 1080 if (time1 < 1 / 2.75) { 1081 return 7.5625 * time1 * time1; 1082 } else if (time1 < 2 / 2.75) { 1083 time1 -= 1.5 / 2.75; 1084 return 7.5625 * time1 * time1 + 0.75; 1085 } else if (time1 < 2.5 / 2.75) { 1086 time1 -= 2.25 / 2.75; 1087 return 7.5625 * time1 * time1 + 0.9375; 1088 } 1089 1090 time1 -= 2.625 / 2.75; 1091 return 7.5625 * time1 * time1 + 0.984375; 1092 }; 1093 1094 cc._easeBounceInObj = { 1095 easing: function(dt){ 1096 return 1 - cc._bounceTime(1 - dt); 1097 }, 1098 reverse: function(){ 1099 return cc._easeBounceOutObj; 1100 } 1101 }; 1102 cc.easeBounceIn = function(){ 1103 return cc._easeBounceInObj; 1104 }; 1105 1106 /** 1107 * cc.EaseBounceOut action. 1108 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 1109 * @class 1110 * @extends cc.EaseBounce 1111 */ 1112 cc.EaseBounceOut = cc.EaseBounce.extend(/** @lends cc.EaseBounceOut# */{ 1113 /** 1114 * @param {Number} time1 1115 */ 1116 update:function (time1) { 1117 var newT = this.bounceTime(time1); 1118 this._inner.update(newT); 1119 }, 1120 1121 /** 1122 * @return {cc.ActionInterval} 1123 */ 1124 reverse:function () { 1125 return cc.EaseBounceIn.create(this._inner.reverse()); 1126 }, 1127 1128 clone:function(){ 1129 var action = new cc.EaseBounceOut(); 1130 action.initWithAction(this._inner.clone()); 1131 return action; 1132 } 1133 }); 1134 1135 /** creates the action 1136 * @param {cc.ActionInterval} action 1137 * @return {cc.EaseBounceOut} 1138 * @example 1139 * // example 1140 * var moveEaseBounceOut = cc.EaseBounceOut.create(action); 1141 */ 1142 cc.EaseBounceOut.create = function (action) { 1143 return new cc.EaseBounceOut(action); 1144 }; 1145 1146 cc._easeBounceOutObj = { 1147 easing: function(dt){ 1148 return cc._bounceTime(dt); 1149 }, 1150 reverse:function () { 1151 return cc._easeBounceInObj; 1152 } 1153 }; 1154 cc.easeBounceOut = function(){ 1155 return cc._easeBounceOutObj; 1156 }; 1157 1158 /** 1159 * cc.EaseBounceInOut action. 1160 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 1161 * @class 1162 * @extends cc.EaseBounce 1163 */ 1164 cc.EaseBounceInOut = cc.EaseBounce.extend(/** @lends cc.EaseBounceInOut# */{ 1165 /** 1166 * @param {Number} time1 1167 */ 1168 update:function (time1) { 1169 var newT = 0; 1170 if (time1 < 0.5) { 1171 time1 = time1 * 2; 1172 newT = (1 - this.bounceTime(1 - time1)) * 0.5; 1173 } else { 1174 newT = this.bounceTime(time1 * 2 - 1) * 0.5 + 0.5; 1175 } 1176 this._inner.update(newT); 1177 }, 1178 1179 clone:function(){ 1180 var action = new cc.EaseBounceInOut(); 1181 action.initWithAction(this._inner.clone()); 1182 return action; 1183 }, 1184 1185 /** 1186 * @return {cc.EaseBounceInOut} 1187 */ 1188 reverse:function () { 1189 return cc.EaseBounceInOut.create(this._inner.reverse()); 1190 } 1191 }); 1192 1193 /** creates the action 1194 * @param {cc.ActionInterval} action 1195 * @return {cc.EaseBounceInOut} 1196 * @example 1197 * // example 1198 * var moveEaseBounceInOut = cc.EaseBounceInOut.create(action); 1199 */ 1200 cc.EaseBounceInOut.create = function (action) { 1201 return new cc.EaseBounceInOut(action); 1202 }; 1203 1204 cc._easeBounceInOutObj = { 1205 easing: function (time1) { 1206 var newT; 1207 if (time1 < 0.5) { 1208 time1 = time1 * 2; 1209 newT = (1 - cc._bounceTime(1 - time1)) * 0.5; 1210 } else { 1211 newT = cc._bounceTime(time1 * 2 - 1) * 0.5 + 0.5; 1212 } 1213 return newT; 1214 }, 1215 reverse: function(){ 1216 return cc._easeBounceInOutObj; 1217 } 1218 }; 1219 1220 cc.easeBounceInOut = function(){ 1221 return cc._easeBounceInOutObj; 1222 }; 1223 1224 /** 1225 * cc.EaseBackIn action. 1226 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 1227 * @class 1228 * @extends cc.ActionEase 1229 */ 1230 cc.EaseBackIn = cc.ActionEase.extend(/** @lends cc.EaseBackIn# */{ 1231 /** 1232 * @param {Number} time1 1233 */ 1234 update:function (time1) { 1235 var overshoot = 1.70158; 1236 time1 = time1===0 || time1==1 ? time1 : time1 * time1 * ((overshoot + 1) * time1 - overshoot); 1237 this._inner.update(time1); 1238 }, 1239 1240 /** 1241 * @return {cc.EaseBackOut} 1242 */ 1243 reverse:function () { 1244 return cc.EaseBackOut.create(this._inner.reverse()); 1245 }, 1246 1247 clone:function(){ 1248 var action = new cc.EaseBackIn(); 1249 action.initWithAction(this._inner.clone()); 1250 return action; 1251 } 1252 }); 1253 1254 1255 /** creates the action 1256 * @param {cc.ActionInterval} action 1257 * @return {cc.EaseBackIn} 1258 * @example 1259 * // example 1260 * var moveEaseBackIn = cc.EaseBackIn.create(action); 1261 */ 1262 cc.EaseBackIn.create = function (action) { 1263 return new cc.EaseBackIn(action); 1264 }; 1265 1266 cc._easeBackInObj = { 1267 easing: function (time1) { 1268 var overshoot = 1.70158; 1269 return (time1===0 || time1===1) ? time1 : time1 * time1 * ((overshoot + 1) * time1 - overshoot); 1270 }, 1271 reverse: function(){ 1272 return cc._easeBackOutObj; 1273 } 1274 }; 1275 1276 cc.easeBackIn = function(){ 1277 return cc._easeBackInObj; 1278 }; 1279 1280 /** 1281 * cc.EaseBackOut action. 1282 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 1283 * @class 1284 * @extends cc.ActionEase 1285 */ 1286 cc.EaseBackOut = cc.ActionEase.extend(/** @lends cc.EaseBackOut# */{ 1287 /** 1288 * @param {Number} time1 1289 */ 1290 update:function (time1) { 1291 var overshoot = 1.70158; 1292 time1 = time1 - 1; 1293 this._inner.update(time1 * time1 * ((overshoot + 1) * time1 + overshoot) + 1); 1294 }, 1295 1296 /** 1297 * @return {cc.EaseBackIn} 1298 */ 1299 reverse:function () { 1300 return cc.EaseBackIn.create(this._inner.reverse()); 1301 }, 1302 1303 clone:function(){ 1304 var action = new cc.EaseBackOut(); 1305 action.initWithAction(this._inner.clone()); 1306 return action; 1307 } 1308 }); 1309 1310 /** creates the action 1311 * @param {cc.ActionInterval} action 1312 * @return {cc.EaseBackOut} 1313 * @example 1314 * // example 1315 * var moveEaseBackOut = cc.EaseBackOut.create(action); 1316 */ 1317 cc.EaseBackOut.create = function (action) { 1318 return new cc.EaseBackOut(action); 1319 }; 1320 1321 cc._easeBackOutObj = { 1322 easing: function (time1) { 1323 var overshoot = 1.70158; 1324 time1 = time1 - 1; 1325 return time1 * time1 * ((overshoot + 1) * time1 + overshoot) + 1; 1326 }, 1327 reverse: function(){ 1328 return cc._easeBackInObj; 1329 } 1330 }; 1331 1332 cc.easeBackOut = function(){ 1333 return cc._easeBackOutObj; 1334 }; 1335 1336 /** 1337 * cc.EaseBackInOut action. 1338 * @warning This action doesn't use a bijective function. Actions like Sequence might have an unexpected result when used with this action. 1339 * @class 1340 * @extends cc.ActionEase 1341 */ 1342 cc.EaseBackInOut = cc.ActionEase.extend(/** @lends cc.EaseBackInOut# */{ 1343 /** 1344 * @param {Number} time1 1345 */ 1346 update:function (time1) { 1347 var overshoot = 1.70158 * 1.525; 1348 time1 = time1 * 2; 1349 if (time1 < 1) { 1350 this._inner.update((time1 * time1 * ((overshoot + 1) * time1 - overshoot)) / 2); 1351 } else { 1352 time1 = time1 - 2; 1353 this._inner.update((time1 * time1 * ((overshoot + 1) * time1 + overshoot)) / 2 + 1); 1354 } 1355 }, 1356 1357 clone:function(){ 1358 var action = new cc.EaseBackInOut(); 1359 action.initWithAction(this._inner.clone()); 1360 return action; 1361 }, 1362 1363 /** 1364 * @return {cc.EaseBackInOut} 1365 */ 1366 reverse:function () { 1367 return cc.EaseBackInOut.create(this._inner.reverse()); 1368 } 1369 }); 1370 1371 1372 /** creates the action 1373 * @param {cc.ActionInterval} action 1374 * @return {cc.EaseBackInOut} 1375 * @example 1376 * // example 1377 * var moveEaseBackInOut = cc.EaseBackInOut.create(action); 1378 */ 1379 cc.EaseBackInOut.create = function (action) { 1380 return new cc.EaseBackInOut(action); 1381 }; 1382 1383 cc._easeBackInOutObj = { 1384 easing: function (time1) { 1385 var overshoot = 1.70158 * 1.525; 1386 time1 = time1 * 2; 1387 if (time1 < 1) { 1388 return (time1 * time1 * ((overshoot + 1) * time1 - overshoot)) / 2; 1389 } else { 1390 time1 = time1 - 2; 1391 return (time1 * time1 * ((overshoot + 1) * time1 + overshoot)) / 2 + 1; 1392 } 1393 }, 1394 reverse: function(){ 1395 return cc._easeBackInOutObj; 1396 } 1397 }; 1398 1399 cc.easeBackInOut = function(){ 1400 return cc._easeBackInOutObj; 1401 }; 1402 1403