CCParallaxNode.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.PointObject = cc.Class.extend(/** @lends cc.PointObject# */{
  27. _ratio:null,
  28. _offset:null,
  29. _child:null,
  30. /**
  31. * @return {cc.Point}
  32. */
  33. getRatio:function () {
  34. return this._ratio;
  35. },
  36. /**
  37. * @param {cc.Point} value
  38. */
  39. setRatio:function (value) {
  40. this._ratio = value;
  41. },
  42. /**
  43. * @return {cc.Point}
  44. */
  45. getOffset:function () {
  46. return this._offset;
  47. },
  48. /**
  49. * @param {cc.Point} value
  50. */
  51. setOffset:function (value) {
  52. this._offset = value;
  53. },
  54. /**
  55. * @return {cc.Node}
  56. */
  57. getChild:function () {
  58. return this._child;
  59. },
  60. /**
  61. * @param {cc.Node} value
  62. */
  63. setChild:function (value) {
  64. this._child = value;
  65. },
  66. /**
  67. * @param {cc.Point} ratio
  68. * @param {cc.Point} offset
  69. * @return {Boolean}
  70. */
  71. initWithCCPoint:function (ratio, offset) {
  72. this._ratio = ratio;
  73. this._offset = offset;
  74. this._child = null;
  75. return true;
  76. }
  77. });
  78. /**
  79. * @param {cc.Point} ratio
  80. * @param {cc.Point} offset
  81. * @return {cc.PointObject}
  82. */
  83. cc.PointObject.create = function (ratio, offset) {
  84. var ret = new cc.PointObject();
  85. ret.initWithCCPoint(ratio, offset);
  86. return ret;
  87. };
  88. /**
  89. * <p>cc.ParallaxNode: A node that simulates a parallax scroller<br />
  90. * The children will be moved faster / slower than the parent according the the parallax ratio. </p>
  91. * @class
  92. * @extends cc.Node
  93. */
  94. cc.ParallaxNode = cc.NodeRGBA.extend(/** @lends cc.ParallaxNode# */{
  95. _lastPosition:null,
  96. _parallaxArray:null,
  97. /**
  98. * @return {Array}
  99. */
  100. getParallaxArray:function () {
  101. return this._parallaxArray;
  102. },
  103. /**
  104. * @param {Array} value
  105. */
  106. setParallaxArray:function (value) {
  107. this._parallaxArray = value;
  108. },
  109. /**
  110. * Constructor
  111. */
  112. ctor:function () {
  113. cc.NodeRGBA.prototype.ctor.call(this);
  114. this._parallaxArray = [];
  115. this._lastPosition = cc.p(-100, -100);
  116. },
  117. /**
  118. * Adds a child to the container with a z-order, a parallax ratio and a position offset
  119. * It returns self, so you can chain several addChilds.
  120. * @param {cc.Node} child
  121. * @param {Number} z
  122. * @param {cc.Point} ratio
  123. * @param {cc.Point} offset
  124. * @example
  125. * //example
  126. * voidNode.addChild(background, -1, cc.p(0.4, 0.5), cc.PointZero());
  127. */
  128. addChild:function (child, z, ratio, offset) {
  129. if (arguments.length === 3) {
  130. cc.log("ParallaxNode: use addChild(child, z, ratio, offset) instead")
  131. return;
  132. }
  133. if(!child)
  134. throw "cc.ParallaxNode.addChild(): child should be non-null";
  135. var obj = cc.PointObject.create(ratio, offset);
  136. obj.setChild(child);
  137. this._parallaxArray.push(obj);
  138. var pos = cc.p(this._position.x, this._position.y);
  139. pos.x = pos.x * ratio.x + offset.x;
  140. pos.y = pos.y * ratio.y + offset.y;
  141. child.setPosition(pos);
  142. cc.NodeRGBA.prototype.addChild.call(this, child, z, child.getTag());
  143. },
  144. /**
  145. * Remove Child
  146. * @param {cc.Node} child
  147. * @param {Boolean} cleanup
  148. * @example
  149. * //example
  150. * voidNode.removeChild(background,true);
  151. */
  152. removeChild:function (child, cleanup) {
  153. var locParallaxArray = this._parallaxArray;
  154. for (var i = 0; i < locParallaxArray.length; i++) {
  155. var point = locParallaxArray[i];
  156. if (point.getChild() == child) {
  157. locParallaxArray.splice(i, 1);
  158. break;
  159. }
  160. }
  161. cc.NodeRGBA.prototype.removeChild.call(this, child, cleanup);
  162. },
  163. /**
  164. * Remove all children with cleanup
  165. * @param {Boolean} cleanup
  166. */
  167. removeAllChildren:function (cleanup) {
  168. this._parallaxArray.length = 0;
  169. cc.NodeRGBA.prototype.removeAllChildren.call(this, cleanup);
  170. },
  171. /**
  172. * Visit
  173. */
  174. visit:function () {
  175. var pos = this._absolutePosition();
  176. if (!cc.pointEqualToPoint(pos, this._lastPosition)) {
  177. var locParallaxArray = this._parallaxArray;
  178. for (var i = 0, len = locParallaxArray.length; i < len; i++) {
  179. var point = locParallaxArray[i];
  180. var x = -pos.x + pos.x * point.getRatio().x + point.getOffset().x;
  181. var y = -pos.y + pos.y * point.getRatio().y + point.getOffset().y;
  182. point.getChild().setPosition(x, y);
  183. }
  184. this._lastPosition = pos;
  185. }
  186. cc.NodeRGBA.prototype.visit.call(this);
  187. },
  188. _absolutePosition:function () {
  189. var ret = this._position;
  190. var cn = this;
  191. while (cn.getParent() != null) {
  192. cn = cn.getParent();
  193. ret = cc.pAdd(ret, cn.getPosition());
  194. }
  195. return ret;
  196. }
  197. });
  198. /**
  199. * @return {cc.ParallaxNode}
  200. * @example
  201. * //example
  202. * var voidNode = cc.ParallaxNode.create();
  203. */
  204. cc.ParallaxNode.create = function () {
  205. return new cc.ParallaxNode();
  206. };