I have a Layer with a (player-controlled) Node that it follows via running
//follow the player
runAction(Follow::create(player));
which works great, the player node is centered in the screen and the layer scrolled accordingly when it gets moved around.
However when I scale the layer via
//scale the layer
runAction(ScaleBy::create(3.0f, 3.0f));
it breaks terribly. The player can now move out of the screen and the movement of the screen is really strange. Has anyone had similar issues or a solution and can point me in the right direction? Thanks a lot!
The anchor point of a Layer is always (0,0) ( Bottom-Left) and you cannot change it. So when you scale up the layer, everything inside the layer will be pushed toward Top-Right.
I have searched for that already and tried tinkering with it. I can not get the sprite that the layer is supposed to follow centered on screen anymore. This somehow breaks the calculation of movement/positioning with the Follow action.
Thanks a lot to both of you for pointing that out, at least now I know what the problem is. I can’t believe noone has stumbled upon this yet, seems so very common to do what I did there, or is there some other default way of achieving the behavior I am looking for (i.e. player in the center of the screen, layer following the player, being able to zoom in and out depending on what happens on screen)?
About the ignoreAnchorPointForPosition(), I didn’t know it exist. haha
I think one way to do it is to have a node (camera ) as parent of the world node and the world node contains the character node.
In the update loop, you always update the world node position to cancel out the new position of the character.
e.g. Character at (30,50), World at (-30,-50)
So when you scale the camera node. it always have the character position be the scale center.
Don’t forget to put the camera node to the center of the screen or everything will be at the bottom left.
In case anyone stumbles upon this, the way I ended up implementing this behaviour is by adding another container layer in between that takes care of scaling.
@digitalhoax As you’ve mentioned above that you scale your layer using runAction(ScaleBy::create(3.0f, 3.0f)); while following the followed node,have you changed the co-ordinates of your followed node after scaling the layer?
I am not sure I understand correctly, but the followed nodes position changes all the time, it has a constant forward motion while directions are controlled by the player.
Well,what happens after scaling the layer,is that,the position of any child node added in the layer changes according to scale factor applied to the layer,so what is passed on to the follow action is the scaled position of the followed node which is used for calculations by the Follow action.If your game is following a specific co-ordinate system to position the child nodes in the layer,then scaling the layer will change the values of the position of the child nodes which in turn affects the Follow action.I happened to face the same issue which you faced and I was able to resolve it without adding a container layer to take care of the scaling.