CCActionProgressTimer.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. Copyright (C) 2010 Lam Pham
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. /**
  24. * Progress to percentage
  25. * @class
  26. * @extends cc.ActionInterval
  27. * @param {Number} duration duration in seconds
  28. * @param {Number} percent
  29. * @example
  30. * var to = new cc.ProgressTo(2, 100);
  31. */
  32. cc.ProgressTo = cc.ActionInterval.extend(/** @lends cc.ProgressTo# */{
  33. _to:0,
  34. _from:0,
  35. /**
  36. * Creates a ProgressTo action with a duration and a percent
  37. * Constructor of cc.ProgressTo
  38. * @param {Number} duration duration in seconds
  39. * @param {Number} percent
  40. */
  41. ctor: function(duration, percent){
  42. cc.ActionInterval.prototype.ctor.call(this);
  43. this._to = 0;
  44. this._from = 0;
  45. percent !== undefined && this.initWithDuration(duration, percent);
  46. },
  47. /** Initializes with a duration and a percent
  48. * @param {Number} duration duration in seconds
  49. * @param {Number} percent
  50. * @return {Boolean}
  51. */
  52. initWithDuration:function (duration, percent) {
  53. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  54. this._to = percent;
  55. return true;
  56. }
  57. return false;
  58. },
  59. /**
  60. * return a new cc.ProgressTo, all the configuration is the same as the original
  61. * @returns {cc.ProgressTo}
  62. */
  63. clone:function(){
  64. var action = new cc.ProgressTo();
  65. action.initWithDuration(this._duration, this._to);
  66. return action;
  67. },
  68. /**
  69. * reverse hasn't been supported
  70. * @returns {null}
  71. */
  72. reverse: function(){
  73. cc.log("cc.ProgressTo.reverse(): reverse hasn't been supported.");
  74. return null;
  75. },
  76. /**
  77. * start with a target
  78. * @param {cc.Node} target
  79. */
  80. startWithTarget:function (target) {
  81. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  82. this._from = target.percentage;
  83. // XXX: Is this correct ?
  84. // Adding it to support CCRepeat
  85. if (this._from == 100)
  86. this._from = 0;
  87. },
  88. /**
  89. * custom update
  90. * @param {Number} time time in seconds
  91. */
  92. update:function (time) {
  93. if (this.target instanceof cc.ProgressTimer)
  94. this.target.percentage = this._from + (this._to - this._from) * time;
  95. }
  96. });
  97. /**
  98. * Creates and initializes with a duration and a percent
  99. * @function
  100. * @param {Number} duration duration in seconds
  101. * @param {Number} percent
  102. * @return {cc.ProgressTo}
  103. * @example
  104. * // example
  105. * var to = cc.progressTo(2, 100);
  106. */
  107. cc.progressTo = function (duration, percent) {
  108. return new cc.ProgressTo(duration, percent);
  109. };
  110. /**
  111. * Please use cc.progressTo instead
  112. * Creates and initializes with a duration and a percent
  113. * @static
  114. * @deprecated since v3.0,please use cc.progressTo instead.
  115. * @param {Number} duration duration in seconds
  116. * @param {Number} percent
  117. * @return {cc.ProgressTo}
  118. * @example
  119. * //example
  120. * var progress = cc.ProgressTo.create(duration,percent);
  121. */
  122. cc.ProgressTo.create = cc.progressTo;
  123. /**
  124. * Progress from a percentage to another percentage
  125. * @class
  126. * @extends cc.ActionInterval
  127. * @param {Number} duration duration in seconds
  128. * @param {Number} fromPercentage
  129. * @param {Number} toPercentage
  130. * @example
  131. * var fromTo = new cc.ProgressFromTo(2, 100.0, 0.0);
  132. */
  133. cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{
  134. _to:0,
  135. _from:0,
  136. /**
  137. * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
  138. * Constructor of cc.ProgressFromTo
  139. * @param {Number} duration duration in seconds
  140. * @param {Number} fromPercentage
  141. * @param {Number} toPercentage
  142. */
  143. ctor:function(duration, fromPercentage, toPercentage){
  144. cc.ActionInterval.prototype.ctor.call(this);
  145. this._to = 0;
  146. this._from = 0;
  147. toPercentage !== undefined && this.initWithDuration(duration, fromPercentage, toPercentage);
  148. },
  149. /** Initializes the action with a duration, a "from" percentage and a "to" percentage
  150. * @param {Number} duration duration in seconds
  151. * @param {Number} fromPercentage
  152. * @param {Number} toPercentage
  153. * @return {Boolean}
  154. */
  155. initWithDuration:function (duration, fromPercentage, toPercentage) {
  156. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  157. this._to = toPercentage;
  158. this._from = fromPercentage;
  159. return true;
  160. }
  161. return false;
  162. },
  163. /**
  164. * return a new cc.ProgressTo, all the configuration is the same as the original
  165. * @returns {cc.ProgressFromTo}
  166. */
  167. clone:function(){
  168. var action = new cc.ProgressFromTo();
  169. action.initWithDuration(this._duration, this._from, this._to);
  170. return action;
  171. },
  172. /**
  173. * @return {cc.ActionInterval}
  174. */
  175. reverse:function () {
  176. return cc.progressFromTo(this._duration, this._to, this._from);
  177. },
  178. /**
  179. * start with a target
  180. * @param {cc.Node} target
  181. */
  182. startWithTarget:function (target) {
  183. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  184. },
  185. /**
  186. * @param {Number} time time in seconds
  187. */
  188. update:function (time) {
  189. if (this.target instanceof cc.ProgressTimer)
  190. this.target.percentage = this._from + (this._to - this._from) * time;
  191. }
  192. });
  193. /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
  194. * @function
  195. * @param {Number} duration duration in seconds
  196. * @param {Number} fromPercentage
  197. * @param {Number} toPercentage
  198. * @return {cc.ProgressFromTo}
  199. * @example
  200. * // example
  201. * var fromTo = cc.progressFromTo(2, 100.0, 0.0);
  202. */
  203. cc.progressFromTo = function (duration, fromPercentage, toPercentage) {
  204. return new cc.ProgressFromTo(duration, fromPercentage, toPercentage);
  205. };
  206. /**
  207. * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
  208. * @static
  209. * @deprecated since v3.0,please use cc.ProgressFromTo(duration, fromPercentage, toPercentage) instead.
  210. * @param {Number} duration duration in seconds
  211. * @param {Number} fromPercentage
  212. * @param {Number} toPercentage
  213. * @return {cc.ProgressFromTo}
  214. * @example
  215. * //example
  216. * var progress = cc.ProgressFromTo.create(duration, fromPercentage, toPercentage);
  217. */
  218. cc.ProgressFromTo.create = cc.progressFromTo;