I am encountering issues with determining a CCSprite's parent node's scale value.

In my game I have a class that extends CCLayer and scales itself based on game triggers. Certain child sprites of this CCLayer have mathematical calculations that become inaccurate once I scale the parent CCLayer. For instance, I have a tank sprite that needs to determine its firing point within the parent node.

Whenever I scale the layer and ask the layer for its scale values, they are accurate. However, when I poll the sprites contained within the layer for their parent's scale values, they always appear as one.

// From within the sprite
CCLOG(@"ChildSprite-> Parent's scale values are scaleX: %f, scaleY: %f",
      self.parent.scaleX, self.parent.scaleY); // Outputs 1.0,1.0

// From within the layer
CCLOG(@"Layer-> ScaleX : %f, ScaleY: %f , SCALE: %f",
      self.scaleX, self.scaleY,     self.scale); // Output is 0.80,0.80

Could anyone explain to me why this is the case? I don't understand why these values are different. Maybe I don't understand the inner design of Cocos2d fully. Any help is appreciated.


2 Answers 2


I believe this is related to the scene graph and parent/child relations. As the child's transformation matrices are computed against the parent, the parent is the world origin of transformation.

The child is perceiving up the scene graph tree as an individual node, they see the parent as a transform reference and if the parent is always uniformly scaled, as this is their world transform.

Try experimenting with non-uniform scale values of the parent and see how this is reflected from the child's perspective. From this you can create a scaling percentage offset that the child can use in it's own calculations(You can't extract the exact location, but you can extract the offset from the world origin of the parent).

  • \$\begingroup\$ Turns out it was because self.parent references the spriteBatchNode the sprite is contained within \$\endgroup\$ Jul 1, 2013 at 0:07

Going to answer my own question... This is the code I am using to insert a sprite into the layer

EarthTank * earthTank = [[EarthTank alloc] initWithWorld:self->world  
atLocation:spawnPointRight withID:pid];
earthTank.tankShaft = shaft; 
[earthTank createInitialArsenal:tempPlayer];
[earthTank addAndAlignTankShaft];
[spriteBatchNode addChild:earthTank z:2];

So the problem is self.parent (called within sprite) references the SpriteBatchNode and not the layer itself. If you change the call to self.parent.parent you can find the Layer's true scale values.

Moral of the story: If you use a spriteBatchNode the parent of a sprite is not the layer, but the spriteBatchNode.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .