CCActionFollow Class Reference

Inherits from CCAction : NSObject
Conforms to NSCopying
Declared in CCAction.h

Overview

Creates an action which follows a node. The followed node can be moved by any means available (ie move action, physics velocity, changing position property). The target node will be moved in the opposite direction to keep it centered on the followed node.

A boundary can be specified to prevent the target’s position to leave a certain area. Note that this area must be smaller to account for the fact that the followed node is at the center but you will probably want the following to stop when the border of the target node reaches the boundary (ie typicall the screen border).

Smoothly following a node or keeping it off-center is not supported. Look at the CCActionFollow code to create a custom action with those features. There’s also an explanation Centering the Scene on a Node in the Sprite Kit documentation - the same principle can be applied to Cocos2D and is used by CCActionFollow.

Usage example:

id follow = [CCFollow actionWithTarget:playerNode];
[gameLayerNode runAction:follow];

Whenever playerNode changes its position, the position of gameLayerNode will be updated (moved in the opposite direction) to keep playerNode centered.

Creating a Follow Action

+ actionWithTarget:

Creates a follow action with no boundaries.

+ (id)actionWithTarget:(CCNode *)followedNode

Parameters

followedNode

Node to follow.

Return Value

The follow action object.

Declared In

CCAction.h

+ actionWithTarget:worldBoundary:

Creates a follow action with boundaries.

+ (id)actionWithTarget:(CCNode *)followedNode worldBoundary:(CGRect)rect

Parameters

followedNode

Node to follow.

rect

Boundary rect.

Return Value

The follow action object.

Declared In

CCAction.h

– initWithTarget:

Initalizes a follow action with no boundaries.

- (id)initWithTarget:(CCNode *)followedNode

Parameters

followedNode

Node to follow.

Return Value

An initialized follow action object.

Declared In

CCAction.h

– initWithTarget:worldBoundary:

Initalizes a follow action with boundaries.

- (id)initWithTarget:(CCNode *)followedNode worldBoundary:(CGRect)rect

Parameters

followedNode

Node to follow.

rect

Boundary rect.

Return Value

The initalized follow action object.

Declared In

CCAction.h