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 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * Parallax Object. <br /> 29 * Parallax required attributes are stored. 30 * @class 31 * @extends cc.Class 32 */ 33 cc.PointObject = cc.Class.extend(/** @lends cc.PointObject# */{ 34 _ratio:null, 35 _offset:null, 36 _child:null, 37 38 ctor: function(ratio, offset){ 39 this.initWithCCPoint(ratio, offset); 40 }, 41 42 /** 43 * Gets the ratio. 44 * @return {cc.Point} Not point, this is ratio. 45 */ 46 getRatio:function () { 47 return this._ratio; 48 }, 49 50 /** 51 * Set the ratio. 52 * @param {cc.Point} value 53 */ 54 setRatio:function (value) { 55 this._ratio = value; 56 }, 57 58 /** 59 * Gets the offset. 60 * @return {cc.Point} 61 */ 62 getOffset:function () { 63 return this._offset; 64 }, 65 66 /** 67 * Set the offset. 68 * @param {cc.Point} value 69 */ 70 setOffset:function (value) { 71 this._offset = value; 72 }, 73 74 /** 75 * Gets the child. 76 * @return {cc.Node} 77 */ 78 getChild:function () { 79 return this._child; 80 }, 81 82 /** 83 * Set the child. 84 * @param {cc.Node} value 85 */ 86 setChild:function (value) { 87 this._child = value; 88 }, 89 90 /** 91 * initializes cc.PointObject 92 * @param {cc.Point} ratio Not point, this is a ratio. 93 * @param {cc.Point} offset 94 * @return {Boolean} 95 */ 96 initWithCCPoint:function (ratio, offset) { 97 this._ratio = ratio; 98 this._offset = offset; 99 this._child = null; 100 return true; 101 } 102 }); 103 104 /** 105 * Create a object to stored parallax data. 106 * @param {cc.Point} ratio 107 * @param {cc.Point} offset 108 * @return {cc.PointObject} 109 * @deprecated since v3.0 please use new cc.PointObject() instead. 110 */ 111 cc.PointObject.create = function (ratio, offset) { 112 return new cc.PointObject(ratio, offset); 113 }; 114 115 /** 116 * <p>cc.ParallaxNode: A node that simulates a parallax scroller<br /> 117 * The children will be moved faster / slower than the parent according the the parallax ratio. </p> 118 * @class 119 * @extends cc.Node 120 * 121 * @property {Array} parallaxArray - Parallax nodes array 122 */ 123 cc.ParallaxNode = cc.Node.extend(/** @lends cc.ParallaxNode# */{ 124 parallaxArray:null, 125 126 _lastPosition:null, 127 _className:"ParallaxNode", 128 129 /** 130 * Gets the parallax array. 131 * @return {Array} 132 */ 133 getParallaxArray:function () { 134 return this.parallaxArray; 135 }, 136 137 /** 138 * Set parallax array. 139 * @param {Array} value 140 */ 141 setParallaxArray:function (value) { 142 this.parallaxArray = value; 143 }, 144 145 /** 146 * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. 147 */ 148 ctor:function () { 149 cc.Node.prototype.ctor.call(this); 150 this.parallaxArray = []; 151 this._lastPosition = cc.p(-100, -100); 152 }, 153 154 /** 155 * Adds a child to the container with a z-order, a parallax ratio and a position offset 156 * It returns self, so you can chain several addChilds. 157 * @param {cc.Node} child 158 * @param {Number} z 159 * @param {cc.Point} ratio 160 * @param {cc.Point} offset 161 * @example 162 * //example 163 * voidNode.addChild(background, -1, cc.p(0.4, 0.5), cc.p(0,0)); 164 */ 165 addChild:function (child, z, ratio, offset) { 166 if (arguments.length === 3) { 167 cc.log("ParallaxNode: use addChild(child, z, ratio, offset) instead"); 168 return; 169 } 170 if(!child) 171 throw new Error("cc.ParallaxNode.addChild(): child should be non-null"); 172 var obj = new cc.PointObject(ratio, offset); 173 obj.setChild(child); 174 this.parallaxArray.push(obj); 175 176 child.setPosition(this._position.x * ratio.x + offset.x, this._position.y * ratio.y + offset.y); 177 178 cc.Node.prototype.addChild.call(this, child, z, child.tag); 179 }, 180 181 /** 182 * Remove Child 183 * @param {cc.Node} child 184 * @param {Boolean} cleanup 185 * @example 186 * //example 187 * voidNode.removeChild(background,true); 188 */ 189 removeChild:function (child, cleanup) { 190 var locParallaxArray = this.parallaxArray; 191 for (var i = 0; i < locParallaxArray.length; i++) { 192 var point = locParallaxArray[i]; 193 if (point.getChild() === child) { 194 locParallaxArray.splice(i, 1); 195 break; 196 } 197 } 198 cc.Node.prototype.removeChild.call(this, child, cleanup); 199 }, 200 201 /** 202 * Remove all children with cleanup 203 * @param {Boolean} [cleanup=true] 204 */ 205 removeAllChildren:function (cleanup) { 206 this.parallaxArray.length = 0; 207 cc.Node.prototype.removeAllChildren.call(this, cleanup); 208 }, 209 210 _updateParallaxPosition: function(){ 211 var pos = this._absolutePosition(); 212 if (!cc.pointEqualToPoint(pos, this._lastPosition)) { 213 var locParallaxArray = this.parallaxArray; 214 for (var i = 0, len = locParallaxArray.length; i < len; i++) { 215 var point = locParallaxArray[i]; 216 var child = point.getChild(); 217 child.setPosition(-pos.x + pos.x * point.getRatio().x + point.getOffset().x, 218 -pos.y + pos.y * point.getRatio().y + point.getOffset().y); 219 } 220 this._lastPosition = pos; 221 } 222 }, 223 224 _absolutePosition:function () { 225 var ret = this._position; 226 var cn = this; 227 while (cn.parent !== null) { 228 cn = cn.parent; 229 ret = cc.pAdd(ret, cn.getPosition()); 230 } 231 return ret; 232 }, 233 234 _createRenderCmd: function(){ 235 if(cc._renderType === cc.game.RENDER_TYPE_CANVAS) 236 return new cc.ParallaxNode.CanvasRenderCmd(this); 237 else 238 return new cc.ParallaxNode.WebGLRenderCmd(this); 239 } 240 }); 241 242 /** 243 * Create new parallax node. 244 * @deprecated since v3.0 please use new cc.ParallaxNode() instead. 245 * @return {cc.ParallaxNode} 246 * @example 247 * //example 248 * var voidNode = new cc.ParallaxNode(); 249 */ 250 cc.ParallaxNode.create = function () { 251 return new cc.ParallaxNode(); 252 }; 253