How to scale while retaining position

Hi,
I have a layer that I have scaled down:
layer->setScale(.4);

the layer does scale down, but the positioning is off.
For example, if it starts off at the center, after the scaling it is not on the center anymore….

Does anyone know how to go about this?
Thank you

CCLayer s have their Anchor Point on ( 0, 0 ), the bottom left corner. When you scale an object, it will scale relative to the anchor point.
Anyway, I have tried changing it’s anchor point to the center but it doesn’t work for me so I just did a bit of math to figure out the position of the layer.

// cocos2dx-2.0.4
/* Scale a layer and set it's position to the center */
// Get screen size.
CCSize windowSize = CCDirector::sharedDirector() -> getVisibleSize();

// Scale layer down to 40%
mBaseLayer -> setScale( 0.40f );

// Calculate coordinates
// xPos is the center of the width of the screen ( width * 0.5 ) minus half the width of the layer
float xPos = ( windowSize.width * 0.50f ) - ( mBaseLayer -> getContentSize().width * 0.50f );

// yPos is the center of the height of the screen ( height * 0.5 ) minus half the height of the layer
float yPos = ( windowSize.height * 0.50f ) - ( mBaseLayer -> getContentSize().height * 0.50f );

// Place the bottom left corner of the layer to xPos and yPos.
mBaseLayer -> setPosition( CCPointMake( xPos, yPos ) );

Hi,

Your code is not working for me….
Is there another way?

Thanks…
:slight_smile: