Here's what I've just recently used. Just keep in mind that I'm still new to cocos2d, so I doubt this is the best method, but it works for me so far and I'm sticking with it at the moment. Keep in mind that I'm also using Box2D, so make adjustments as needed if you're not using it. I had just finished implementing the camera movement last night. I'm currently only updating the camera once the player reaches the center of the screen along the X coord, after that, I move the camera with the player so that the player remains centered on the screen.
-(void) tick: (ccTime) dt
{
CGSize winSize = [CCDirector sharedDirector].winSize;
int32 velocityIterations = 8;
int32 positionIterations = 1;
_world->Step(dt, velocityIterations, positionIterations);
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if(sprite.tag == 1) {
// Move the camera along with the player
float moveX = 0.0f, x1, x2, y1, y2, z1, z2;
// Calculate camera X position only if the player has reached the center of the screen
if(sprite.position.x > winSize.width / 2)
moveX = sprite.position.x - (winSize.width / 2);
// Get current position
[self.camera centerX:&x1 centerY:&y1 centerZ:&z1];
[self.camera eyeX:&x2 eyeY:&y2 eyeZ:&z2];
// Perform camera update
[self.camera setCenterX:moveX centerY:y1 centerZ:z1];
[self.camera setEyeX:moveX eyeY:y2 eyeZ:z2];
}
sprite.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
For some reason, I was unable to use the same x, y and z variables that I've seen others use in their sample code for both the setCenterX/Y/Z and setEyeX/Y/Z. I'm not quite sure what's going on, but my player suddenly disappears if I use the same x, y and z variables. For now, I'm using separate ones which works. Once I actually add a background (instead of using just the default black background that you get with the cocos2D/Box2D template) then I may figure out what's going on. But for now, it works for me and so I'm moving on to something else.