Follow action cocos2d-x v3.2. Why always centered followed?

Follow action cocos2d-x v3.2. Why always centered followed?

I am using coco2d-x v3.2

When I do layer to follow my player. Player is followed. Good Enough!!.

But initally I’ve set the position of my player to be at the 2/3 of the height of the layer so why does it follows it from the 1/2 of the height.
I mean why it follows it by keeping the player at the center of the screen.

According to me Follow action must let the player be at its position relative to screen and then it can be followed
by my layer by keeping relative position of the player always the same as intial.

Is there any other way.

Also, can someone explain me the meaning of Rect given as the 2nd argument inside this Follow::create()

APIs available is not updated so it shows old implementation of Follow action which is now different in v3.2.

Thanks :slight_smile:

i am not sure what your doubt is, but you might wanna look at this

Just a explain a bit more.
Suppose, I am setting a sprite’s position at the the winSize().width/2 , 2winSize.height/3 and add this to a layer
Now, when I put layer to follow this sprite, then it follows it but it keeps player at the center of the screen.
But instead, i was asking sprite to be followed by the layer but this sprite always at the same
winSize().width/2 , 2
winSize.height/3 with respective to the game’s screen

Even i need solution for this.
there should be some method like see ref image, when MC touch red rect than it should follow the layer right side.

Hey, some implementation has been done in test samples for this Follow Action, but I have not get how to implement it… Lets see and wait for more replies.

Any updates on this. I would appreciate a proper implementation of a Follow action with an (initial) offset. The use case is to have some complex structure to suddenly follow a node but without it jumping to the center of the followed node. A suitable workaround might be to just add in another (container) layer that does not move inititally but starts moving when following. downside to this is that parallax inside the container will not work.

I didn’t get it work so used different strategy of moving nodes across scene and removing once out of the screen. There is still camera thing that can be explored. Check if something works for you and may be you would like to post the solution if follow or camera api works. Thanks :smile:

I’ve got a work around for follow action with an initial offset.Offset can be horizontal(x) offset or vertical(y) offset.By providing an offset,the follow action will not jump to the centre of the followed node when following the node but it will follow the node from the provided offset.The implementation is as follows-

In CCAction.h modify the create method as-

Follow* create(Node *followedNode,const Rect& rect=Rect::ZERO,float offsetX=0.0,float offsetY=0.0);

bool initWithTarget(Node *followedNode, const Rect& rect = Rect::ZERO,float offsetX=0.0,float offsetY=0.0);

In CCAction.cpp modify the implementation of above two methods as-

Follow* Follow::create(Node followedNode, const Rect& rect/ = Rect::ZERO*/,float offsetX,float offsetY)
Follow *follow = new (std::nothrow) Follow();
if (follow && follow->initWithTarget(followedNode, rect,offsetX,offsetY))

return nullptr;

bool Follow::initWithTarget(Node followedNode, const Rect& rect/ = Rect::ZERO*/,float offsetX,float offsetY)

Size winSize = Director::getInstance()->getWinSize();
_fullScreenSize.set(winSize.width, winSize.height);
_halfScreenSize = _fullScreenSize * 0.5f;

return true;

I’ve indicated the lines to be added or changed to the existing follow action code in the above implementations in bold.I’ve not included the complete implementation for simplicity.
I feel this feature can be added in future cocos 2dx releases.
Cocos 2dx developers what do you think about this approach?

1 Like

Aaha, nice. Yes, Modifying the framework was the only way to make it work as per our requirement. Good that it’s open source. Also, may be you can send pull request the project on GIT. May be they can add your thing.


Yes I’m already working on submitting a pull request for this feature.Well,we can achieve the above even without modifying the framework by writing a custom class which inherits from the framework’s Follow action class.

Hmm. Yes you are right. But I think, it can be good if originally supported since it’s an assumption to follow player positioned at center of screen.

Also, if we were to inherit from the follow action and make our custom action, can you please write the constructor function of this new class?
1. I just want to see how the call to initWithTarget of the parent class(Follow Action) is made with those offsetX and offsett, AND

2. how exactly those halfScreenSize.x and ```_halfScreenSize.y`` will be modified from this constructor!!

Thanks :smile:

Yes this should be supported originally for which I’m submitting a pull request.Hopefully this feature will be added in the future releases of cocos 2dx!!
Till such time we can implement our own custom Follow action class.
For implementing this we’ll have to follow basic C++ inheritance rules.So the implementation will be as follows-
CustomFollow::CustomFollow(float xOffset,float yOffset){


CustomFollow::CustomFollow* create(Node *followedNode,Rect &rect,float offsetX,float offsetY){

CustomFollow *follow = new (std::nothrow) CustomFollow(offsetX,offsetY);
if (follow && follow->initWithTarget(followedNode, rect))
return follow;
return nullptr;


Also override the step method.
Voila we have our custom follow action class with offset ready!

1 Like

Pull request for this feature submitted at

1 Like

Hi @zhangxm

I can see that it’s merged but it’s not present in cocos2d-x v3.11.1!

Can you please see to it. Thanks :slight_smile:

@catch_up v3.11 only includes IPv6-only based on v3.11. The PR is merged into v3.12 as you can see there is a label named v3.12. We plan to release v3.12 at the end of June.

oh ya…this…