Understanding relative movement - how to scroll background when node reaches screen border

Hello

I’m trying to solve a task with relative movement in cocos2dx and I would be gratefull if You could provide a hint on how to do it.

I have a Scene to which I attach Background sprite. Then to CCLayer with this Background Sprite I attack hero, which can move inside the borders of the screen.

I wanted to make it so when hero reaches point that is outside of specified CCRect which is half of the size of the screen, the screen will scroll.

I do it right now like that:

bool playScene::init() { gameMap *map = new gameMap(); Hero *player = new Hero(); map->addChild(player); this->addChild(map); }

On CCTouchEnded i give my hero position to go to, and in update check if he’s inside CCRect on center of screen. If not, the gameMap CCLayer is being moved in opposite side to imitate as if camera is following hero.

But the problem is that some crazy things happen (screen scrolls all the way down etc.). Can You help me? I’ve tried CCFollow, but it doesn’t seem to work, or I don’t know how to attach it.

So how should I move position of player along with the map so the coordinates of center of him are not messed up?

Thank You for reply