CCParallaxNode.js 7.1 KB

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