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))
{
…
}
CC_SAFE_DELETE(follow);
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;
_halfScreenSize.x+=offsetX;
_halfScreenSize.y+=offsetY;
…
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?