Many games require two objects to follow each other, or one object moving to a moving target. Until now you had to implement this mechanism manually. Today we're releasing a new CCAction for Cocos2d 3.0 that makes this a lot simpler, CCActionMoveToNode:


Move to a Node

CCActionMoveToNode *moveTo = [CCActionMoveToNode actionWithSpeed:100.f targetNode:_spriteToFollow];
[_followingSprite runAction:moveTo];

Follow a Node infinitely

CCActionMoveToNode *moveTo = [CCActionMoveToNode actionWithSpeed:100.f targetNode:_spriteToFollow followInfinite:YES];
[_followingSprite runAction:moveTo];

Get informed when target node is reached

moveTo.actionCompletedBlock = ^(void) {
    [self.contentNode addChild:completedLabel];

Provide the target position through a block

Assuming you want to follow a certain position within a target node you also have the possibility to provide the target position through a block:

CCActionMoveToNode *moveTo = [CCActionMoveToNode actionWithSpeed:100.f positionUpdateBlock:^CGPoint{
    return _spriteToFollow.position;
[_followingSprite runAction:moveTo];

A lot more options!

Checkout the code on GitHub. It contains many tests with all kinds of examples how this API can be used.

Have fun with it!


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations in San Francisco and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more

Cookies on Make School's website

We have placed cookies on your device to ensure that we give you the best experience on our website.

This site uses cookies to deliver our services. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Make School’s Products and Services is subject to these policies and terms.

Please note that Make School no longer supports Internet Explorer

We recommend upgrading to a modern web browser. Learn more