diff --git a/cocos2d/base_nodes/CCNode.js b/cocos2d/base_nodes/CCNode.js index 48528aa5d8..07c41c40df 100644 --- a/cocos2d/base_nodes/CCNode.js +++ b/cocos2d/base_nodes/CCNode.js @@ -174,6 +174,8 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{ _scheduler:null, _initializedNode:false, + _additionalTransformDirty:false, + _additionalTransform:null, /** * Constructor @@ -201,6 +203,8 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{ return this._scheduler; }; this._initializedNode = true; + this._additionalTransform = cc.AffineTransformMakeIdentity(); + this._additionalTransformDirty = false; }, init:function () { @@ -1696,11 +1700,67 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{ t.tx += this._anchorPointInPoints.x; t.ty += this._anchorPointInPoints.y; } + if (this._additionalTransformDirty) { + this._transform = cc.AffineTransformConcat(this._transform, this._additionalTransform); + this._additionalTransformDirty = false; + } this._transformDirty = false; } return this._transform; }, + /** + *

Sets the additional transform.
+ * The additional transform will be concatenated at the end of nodeToParentTransform.
+ * It could be used to simulate `parent-child` relationship between two nodes (e.g. one is in BatchNode, another isn't).
+ * // create a batchNode
+ * var batch= cc.SpriteBatchNode.create("Icon-114.png");
+ * this.addChild(batch);
+ *
+ * // create two sprites, spriteA will be added to batchNode, they are using different textures.
+ * var spriteA = cc.Sprite.createWithTexture(batch->getTexture());
+ * var spriteB = cc.Sprite.create("Icon-72.png");
+ *
+ * batch.addChild(spriteA);
+ *
+ * // We can't make spriteB as spriteA's child since they use different textures. So just add it to layer.
+ * // But we want to simulate `parent-child` relationship for these two node.
+ * this.addChild(spriteB);
+ *
+ * //position
+ * spriteA.setPosition(ccp(200, 200));
+ *
+ * // Gets the spriteA's transform.
+ * var t = spriteA.nodeToParentTransform();
+ *
+ * // Sets the additional transform to spriteB, spriteB's postion will based on its pseudo parent i.e. spriteA.
+ * spriteB.setAdditionalTransform(t);
+ *
+ * //scale
+ * spriteA.setScale(2);
+ *
+ // Gets the spriteA's transform.
+ * * t = spriteA.nodeToParentTransform();
+ *
+ * // Sets the additional transform to spriteB, spriteB's scale will based on its pseudo parent i.e. spriteA.
+ * spriteB.setAdditionalTransform(t);
+ *
+ * //rotation
+ * spriteA.setRotation(20);
+ *
+ * // Gets the spriteA's transform.
+ * t = spriteA.nodeToParentTransform();
+ *
+ * // Sets the additional transform to spriteB, spriteB's rotation will based on its pseudo parent i.e. spriteA.
+ * spriteB.setAdditionalTransform(t);
+

+ */ + setAdditionalTransform:function (additionalTransform) { + this._additionalTransform = additionalTransform; + this._transformDirty = true; + this._additionalTransformDirty = true; + }, + /** * Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates.
* The matrix is in Pixels.