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