CCActionTween.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. * @class
  24. * @extends cc.Class
  25. */
  26. cc.ActionTweenDelegate = cc.Class.extend(/** @lends cc.ActionTweenDelegate */{
  27. updateTweenAction:function(value, key){
  28. }
  29. });
  30. /**
  31. * cc.ActionTween
  32. * cc.ActionTween is an action that lets you update any property of an object.
  33. *
  34. * @class
  35. * @extends cc.ActionInterval
  36. * @example
  37. * //For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:
  38. * var modifyWidth = cc.ActionTween.create(2,"width",200,300)
  39. * target.runAction(modifyWidth);
  40. *
  41. * //Another example: cc.ScaleTo action could be rewriten using cc.PropertyAction:
  42. * // scaleA and scaleB are equivalents
  43. * var scaleA = cc.ScaleTo.create(2,3);
  44. * var scaleB = cc.ActionTween.create(2,"scale",1,3);
  45. */
  46. cc.ActionTween = cc.ActionInterval.extend(/** @lends cc.ActionTween */{
  47. key:"",
  48. from:0,
  49. to:0,
  50. delta:0,
  51. ctor:function(){
  52. cc.ActionInterval.prototype.ctor.call(this);
  53. this.key = "";
  54. this.from = 0;
  55. this.to = 0;
  56. this.delta = 0;
  57. },
  58. /**
  59. * initializes the action with the property name (key), and the from and to parameters.
  60. * @param {Number} duration
  61. * @param {String} key
  62. * @param {Number} from
  63. * @param {Number} to
  64. * @return {Boolean}
  65. */
  66. initWithDuration:function (duration, key, from, to) {
  67. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  68. this.key = key;
  69. this.to = to;
  70. this.from = from;
  71. return true;
  72. }
  73. return false;
  74. },
  75. /**
  76. * @param {cc.Node} target
  77. */
  78. startWithTarget:function (target) {
  79. if(!target || !target.updateTweenAction)
  80. throw "cc.ActionTween.startWithTarget(): target must be non-null, and target must implement updateTweenAction function";
  81. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  82. this.delta = this.to - this.from;
  83. },
  84. /**
  85. * @param {Number} dt
  86. */
  87. update:function (dt) {
  88. this._target.updateTweenAction(this.to - this.delta * (1 - dt), this.key);
  89. },
  90. /**
  91. * @return {cc.ActionTween}
  92. */
  93. reverse:function () {
  94. return cc.ActionTween.create(this.duration, this.key, this.to, this.from);
  95. },
  96. clone:function(){
  97. var action = new cc.ActionTween();
  98. action.initWithDuration(this._duration, this.key, this.from, this.to);
  99. return action;
  100. }
  101. });
  102. /**
  103. * Creates an initializes the action with the property name (key), and the from and to parameters.
  104. * @param {Number} duration
  105. * @param {String} key
  106. * @param {Number} from
  107. * @param {Number} to
  108. * @return {cc.ActionTween}
  109. */
  110. cc.ActionTween.create = function (duration, key, from, to) {
  111. var ret = new cc.ActionTween();
  112. if (ret.initWithDuration(duration, key, from, to))
  113. return ret;
  114. return null;
  115. };