# Scale layer relative to a certain point

Scale layer relative to a certain point
0.0 0

#1

I’m currently making a large space map, filled with planets.
My objective is to scale the layer down (zoom out) when touching one of the planets (in order to be able to see what’s around them) but, as soon as I touch it, the planet moves from under my finger (due to the layer being scaled relative to its anchor point, I guess).
What I’d want it to do is to scale down, while the planet remains at exactly the same position.

Any thoughts on how to achieve this?

Thank you!

#2

I think I got it…

What I did was get the Planet’s position, divide its X coordinate by the layer’s width and its Y coordinate by the layer’s height.
I then set the layer’s anchor point to the resulting X and Y coordinates (the position of the planet) and scaled it relative to that.

``````if (_planetTouched) {

for (int i = 0; i < _planets->count(); i++) {

Planet *p = (Planet *)_planets->objectAtIndex(i);

if (p->_isSelected) {

CCPoint newPos = p->getPosition();

float newX = newPos.x / this->getContentSize().width;
float newY = newPos.y / this->getContentSize().height;

this->setAnchorPoint(ccp(newX, newY));

this->setScale(0.8f);

}

}

}
``````

It seems to work, but I’d like to know if there’s a better way to do this.

Thank you very much!

#3

You could also not change the anchor point, scale, and shift the layer so that the screen is centered on the appropriate spot (like moving the camera).

#4

I was just about to suggest what you did.
I’m planning to do something similar and was thinking of doing exactly that. Glad to know it works! Thanks!

C.J. Kimberlin wrote:

You could also not change the anchor point, scale, and shift the layer so that the screen is centered on the appropriate spot (like moving the camera).

Wouldn’t it be the same, though? I mean, is there any benefit to scaling and moving vs. resetting the anchor point and scaling the layer?

Thanks!

#5

My only thought would be if you do any other transforms (like rotation) it can turn into a headache (or you’d have to shift the layer anyways). In this case I suppose it’s whatever works.

#6

C.J. Kimberlin wrote:

My only thought would be if you do any other transforms (like rotation) it can turn into a headache (or you’d have to shift the layer anyways). In this case I suppose it’s whatever works.

It’s true… If I ever need to transform the layer, it’ll be a complete mess.
I may be spewing nonsense, but I’d like to know if it’d be possible to create a base node, add the layer as a child of that node and apply the scale and anchor point transforms to the node, leaving the layer unchanged?

#7

Henrique Sousa,

You are not the first person to need this. If you are able, this would be a fantastic addition/extension to the CCScaleBy or CCScaleTo actions:

CCScaleTo::Create(1.5, 45, ccp(45,45));

The default behavior is to use the anchor point, but being able to override this in an action is immensely useful.

Please fork and PR back on github if you can. I am sure there are a hundred people out there who would use this the minute it hit the framework.

Thanks!