Follow Action cocos2d-x v3.2?

I tried to use this action,
I faced 2 issues:

  1. Suppose my sprite is positioned at 2/3 height of screen’s height. But when I use layer->runAction(Follow::create(mySprite)), then the layer follows this sprite but it keeps it in the center. But I want my sprite to be always at the position it was earlier, which is 2/3 height of the screen’s height.

  2. To resolve this issues, I tied to add a dummy sprite(dummy is just a name and nothing much, I tried to make dummy sprite as Node, Sprite with some file and even empty sprite but nothing worked) as a child to this sprite mentioned above. I positioned this dummy sprite so that it is actually at position 1/2 of screen’s height when my above sprite(which parent) is at 2/3 of screen’s height.
    Now, I layer to to follow this dummy sprite… But the problem here is that layer is not following my dummy sprite.

I need to use this follow action, can someone help me?

Thanks

Anybody any idea !!
Hi @iQD , sorry for bothering personally, but can you suggest something?

The follow action always centers on the sprite, as it does not know an offset you can give it.
Either adapt the code to take an offset param or fix it like you said.

Create a Node and a the child sprite to it. Set the follow action to the node and it will follow it and your child will move along with it.
The follow action will always follow a node, regardless if it’s empty or whatever specific node it is.

Post your code.

@iQD

Hi… thanks for replying…

Ammm…
I re read my post… I think I made my 2nd point a little absurd and it was the most important point of my post… :smile:
Oh… Anyways, here is what I want to say again…

Here is what I actually did.
I made a sprite/node and added a child to it which was relatively just half the screen down towards it…
Now, I made my layer to follow this child (and not the parent sprite/node) .
I was expecting that layer would follow child node keeping it at the center, so my parent node will automatically be seen as it is 2/3 the height of the screen.

BUT something weird happened and layer followed nothing. But it was fixed at some random position, and no following…

Here is my code…

  Sprite *sp= Sprite::create("1.png");
  sp->setScale(0.3);
  // Below statement positions my sp at 2/3 height of the screen with x coordinate in the middle of screenWidth
  sp->setPosition(Vec2(...)); 
 Sprite *sp2= Sprite::create("2.png"); // sp2 is my dummy sprite, it can be sprite with no texture also
 // Below statement positions my sp2 relative to sp and hence it makes sp2 appear at the center of the screen
 sp2->setPosition(Vec2(0.6*sp->getContentSize().width,-visibleSize.height/(4*0.3)));

float playfield_width = size.width * 2.0; // make the x-boundry 2 times the screen width
float playfield_height = size.height * 2.0; // make the y-boundry 2 times the screen height

// Here **this** is my layer
this->runAction(Follow::create(sp2, Rect( center.x, center.y - playfield_height/4 , 0,playfield_height/2)));

Note: My sprites have physics body so they fall vertically by themselves under gravity…

Now, layer doesnot follow anything but it is making some random point as the center of the screen and also it is not following it…
I was expecting that layer would follow my sp2(dummy sprite) so that sp2 would always be in the center of the screen, which will automatically make my parent sprite (sp) to be positioned at the 2/3 height of the screen…

Note:
If I change, in below code , from sp2 to sp2, this code works fine but it makes sp as the center which I don’t want.

this->runAction(Follow::create(sp2, Rect( center.x, center.y - playfield_height/4 , 0,playfield_height/2)));

Thanks… :slight_smile:

@catch_up @iQD Can anyone help out with adapting the existing Follow Action code to take an offset so that the followed sprite is not centered followed but is followed relative to it’s position on screen?

Thanks!!

I found a solution for this.If anyone is encountering the same problem,I’ve explained my approach to this problem at Follow action cocos2d-x v3.2. Why always centered followed?.