CCActionTween.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. *
  24. * @class
  25. * @extends cc.Class
  26. */
  27. cc.ActionTweenDelegate = cc.Class.extend(/** @lends cc.ActionTweenDelegate */{
  28. /**
  29. * Update Tween Action.
  30. * @param value
  31. * @param key
  32. */
  33. updateTweenAction:function(value, key){}
  34. });
  35. /**
  36. * cc.ActionTween
  37. * cc.ActionTween is an action that lets you update any property of an object.
  38. *
  39. * @class
  40. * @extends cc.ActionInterval
  41. * @example
  42. * //For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:
  43. * var modifyWidth = cc.actionTween(2,"width",200,300)
  44. * target.runAction(modifyWidth);
  45. *
  46. * //Another example: cc.ScaleTo action could be rewriten using cc.PropertyAction:
  47. * // scaleA and scaleB are equivalents
  48. * var scaleA = cc.scaleTo(2,3);
  49. * var scaleB = cc.actionTween(2,"scale",1,3);
  50. * @param {Number} duration
  51. * @param {String} key
  52. * @param {Number} from
  53. * @param {Number} to
  54. */
  55. cc.ActionTween = cc.ActionInterval.extend(/** @lends cc.ActionTween */{
  56. key:"",
  57. from:0,
  58. to:0,
  59. delta:0,
  60. /**
  61. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  62. * Creates an initializes the action with the property name (key), and the from and to parameters.
  63. * @param {Number} duration
  64. * @param {String} key
  65. * @param {Number} from
  66. * @param {Number} to
  67. */
  68. ctor:function(duration, key, from, to){
  69. cc.ActionInterval.prototype.ctor.call(this);
  70. this.key = "";
  71. to !== undefined && this.initWithDuration(duration, key, from, to);
  72. },
  73. /**
  74. * initializes the action with the property name (key), and the from and to parameters.
  75. * @param {Number} duration
  76. * @param {String} key
  77. * @param {Number} from
  78. * @param {Number} to
  79. * @return {Boolean}
  80. */
  81. initWithDuration:function (duration, key, from, to) {
  82. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  83. this.key = key;
  84. this.to = to;
  85. this.from = from;
  86. return true;
  87. }
  88. return false;
  89. },
  90. /**
  91. * Start this tween with target.
  92. * @param {cc.ActionTweenDelegate} target
  93. */
  94. startWithTarget:function (target) {
  95. if(!target || !target.updateTweenAction)
  96. throw "cc.ActionTween.startWithTarget(): target must be non-null, and target must implement updateTweenAction function";
  97. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  98. this.delta = this.to - this.from;
  99. },
  100. /**
  101. * Called once per frame. Time is the number of seconds of a frame interval.
  102. *
  103. * @param {Number} dt
  104. */
  105. update:function (dt) {
  106. this.target.updateTweenAction(this.to - this.delta * (1 - dt), this.key);
  107. },
  108. /**
  109. * returns a reversed action.
  110. * @return {cc.ActionTween}
  111. */
  112. reverse:function () {
  113. return new cc.ActionTween(this.duration, this.key, this.to, this.from);
  114. },
  115. /**
  116. * to copy object with deep copy.
  117. * returns a clone of action.
  118. *
  119. * @return {cc.ActionTween}
  120. */
  121. clone:function(){
  122. var action = new cc.ActionTween();
  123. action.initWithDuration(this._duration, this.key, this.from, this.to);
  124. return action;
  125. }
  126. });
  127. /**
  128. * Creates an initializes the action with the property name (key), and the from and to parameters.
  129. * @function
  130. * @param {Number} duration
  131. * @param {String} key
  132. * @param {Number} from
  133. * @param {Number} to
  134. * @return {cc.ActionTween}
  135. */
  136. cc.actionTween = function (duration, key, from, to) {
  137. return new cc.ActionTween(duration, key, from, to);
  138. };
  139. /**
  140. * Please use cc.actionTween instead.
  141. * Creates an initializes the action with the property name (key), and the from and to parameters.
  142. * @static
  143. * @deprecated since v3.0 <br /> Please use cc.actionTween instead.
  144. * @param {Number} duration
  145. * @param {String} key
  146. * @param {Number} from
  147. * @param {Number} to
  148. * @return {cc.ActionTween}
  149. */
  150. cc.ActionTween.create = cc.actionTween;