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