1
votes

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.

EDIT: 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.

2

2 Answers

0
votes

Sounds like self.parent isn't actually the layer. Check that.

0
votes

Every CCNode has an independent scale, but the scale from parent down (nested) is cumulative.

Eg. A parents scale will effect all objects that are its decedent, but a child will not effect its parent.

For example if a parent layer is scaled at 50%, then all the children will have the 50% factored in when they display, even if the scaling for the individual object is 100%.

Math being, 0.50 * 1.00 = 0.50