CCActionProgressTimer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (C) 2010 Lam Pham
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * Progress to percentage
  23. * @class
  24. * @extends cc.ActionInterval
  25. */
  26. cc.ProgressTo = cc.ActionInterval.extend(/** @lends cc.ProgressTo# */{
  27. _to:0,
  28. _from:0,
  29. ctor: function(){
  30. cc.ActionInterval.prototype.ctor.call(this);
  31. this._to = 0;
  32. this._from = 0;
  33. },
  34. /** Initializes with a duration and a percent
  35. * @param {Number} duration duration in seconds
  36. * @param {Number} percent
  37. * @return {Boolean}
  38. */
  39. initWithDuration:function (duration, percent) {
  40. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  41. this._to = percent;
  42. return true;
  43. }
  44. return false;
  45. },
  46. clone:function(){
  47. var action = new cc.ProgressTo();
  48. action.initWithDuration(this._duration, this._to);
  49. return action;
  50. },
  51. reverse: function(){
  52. cc.log("cc.ProgressTo.reverse(): reverse hasn't been supported.");
  53. return null;
  54. },
  55. /**
  56. * @param {cc.Node} target
  57. */
  58. startWithTarget:function (target) {
  59. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  60. this._from = target.getPercentage();
  61. // XXX: Is this correct ?
  62. // Adding it to support CCRepeat
  63. if (this._from == 100)
  64. this._from = 0;
  65. },
  66. /**
  67. * @param {Number} time time in seconds
  68. */
  69. update:function (time) {
  70. if (this._target instanceof cc.ProgressTimer)
  71. this._target.setPercentage(this._from + (this._to - this._from) * time);
  72. }
  73. });
  74. /** Creates and initializes with a duration and a percent
  75. * @param {Number} duration duration in seconds
  76. * @param {Number} percent
  77. * @return {cc.ProgressTo}
  78. * @example
  79. * // example
  80. * var to = cc.ProgressTo.create(2, 100);
  81. *
  82. */
  83. cc.ProgressTo.create = function (duration, percent) {
  84. var progressTo = new cc.ProgressTo();
  85. progressTo.initWithDuration(duration, percent);
  86. return progressTo;
  87. };
  88. /**
  89. * Progress from a percentage to another percentage
  90. * @class
  91. * @extends cc.ActionInterval
  92. */
  93. cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{
  94. _to:0,
  95. _from:0,
  96. ctor:function(){
  97. cc.ActionInterval.prototype.ctor.call(this);
  98. this._to = 0;
  99. this._from = 0;
  100. },
  101. /** Initializes the action with a duration, a "from" percentage and a "to" percentage
  102. * @param {Number} duration duration in seconds
  103. * @param {Number} fromPercentage
  104. * @param {Number} toPercentage
  105. * @return {Boolean}
  106. */
  107. initWithDuration:function (duration, fromPercentage, toPercentage) {
  108. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  109. this._to = toPercentage;
  110. this._from = fromPercentage;
  111. return true;
  112. }
  113. return false;
  114. },
  115. clone:function(){
  116. var action = new cc.ProgressFromTo();
  117. action.initWithDuration(this._duration, this._from, this._to);
  118. return action;
  119. },
  120. /**
  121. * @return {cc.ActionInterval}
  122. */
  123. reverse:function () {
  124. return cc.ProgressFromTo.create(this._duration, this._to, this._from);
  125. },
  126. /**
  127. * @param {cc.Node} target
  128. */
  129. startWithTarget:function (target) {
  130. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  131. },
  132. /**
  133. * @param {Number} time time in seconds
  134. */
  135. update:function (time) {
  136. if (this._target instanceof cc.ProgressTimer)
  137. this._target.setPercentage(this._from + (this._to - this._from) * time);
  138. }
  139. });
  140. /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
  141. * @param {Number} duration duration in seconds
  142. * @param {Number} fromPercentage
  143. * @param {Number} toPercentage
  144. * @return {cc.ProgressFromTo}
  145. * @example
  146. * // example
  147. * var fromTO = cc.ProgressFromTo.create(2, 100.0, 0.0);
  148. */
  149. cc.ProgressFromTo.create = function (duration, fromPercentage, toPercentage) {
  150. var progressFromTo = new cc.ProgressFromTo();
  151. progressFromTo.initWithDuration(duration, fromPercentage, toPercentage);
  152. return progressFromTo;
  153. };