1

I am using the following code to make the camera follow my character as he falls down the screen:

[self runAction:[CCFollow actionWithTarget:charSprite worldBoundary:[levelLoader gameWorldSize]/*CGRectMake(0,0,320,3000)*/]];

What happens is this: the code runs as you would expect, and follows the character as he falls from the top of the screen. However: when he reaches the middle of the screen the camera stops following him.

I have tried both the code above as well as replacing the [levelLoader gameWorldSize] with the commented out CGRectMake(0,0,320,3000) - both yield the same results. As a last resort I tried to use some code I found on www.raywenderlich.com that has worked for me before (see below) (it worked on the x-axis in another game I wrote), but the same problem occurred.

-(void)setViewpointCenter:(CGPoint) position
{
 CGSize winSize = [[CCDirector sharedDirector] winSize];
 CGRect worldRect = [levelLoader gameWorldSize];

 int x = MAX(position.x, worldRect.origin.x + winSize.width / 2);
 int y = MAX(position.y, worldRect.origin.y + winSize.height / 2);
 x = MIN(x, (worldRect.origin.x + worldRect.size.width) - winSize.width / 2);
 y = MIN(y, (worldRect.origin.y + worldRect.size.height) - winSize.height/2);
 CGPoint actualPosition = ccp(x, y);

 CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
 CGPoint viewPoint = ccpSub(centerOfView, actualPosition);

 self.position = viewPoint;
}

My world is created in Portrait mode using LevelHelper.

Any help would be greatly appreciated!

Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59

1 Answers1

1

CCFollow follows the node within the world boundary, minus half the screen width/height. If your sprite starts falling at y = 320 then CCFollow will follow it until y = 160.

You can either position the sprite to your world boundary's top (y = 3000) or make the world boundary negative (y = -3000). Either way, when the sprite reaches the world boundary, scrolling will stop half a screen width/height before the world boundary.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • OK, thanks for the info! Unfortunately putting `CGRectMake(0,0,320,-3000)` or `CGRectMake(0,3000,320,-3000)` into the `worldBoundary:` does not fix the issue for me - is that what you meant? – Max Chuquimia Jan 31 '13 at 05:35