objective-ccocos2d-iphonekobold2d

How to I get a scene to follow my sprite?


I've been trying to get my scene to follow my Player sprite but for some reason its not following. Can anyone explain why? I've tried following tutorials but no luck. This is my current code:

[self setViewpointCenter:Player.position];


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

    int x = MAX(position.x, winSize.width / 2);
    int y = MAX(position.y, winSize.height / 2);
    x = MIN(x, (theMap.mapSize.width * theMap.tileSize.width) - winSize.width / 2);
    y = MIN(y, (theMap.mapSize.height * theMap.tileSize.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;
}

Solution

  • Scrapped my original code and replaced it with a clean code recommended by LearnCocos2D

    [self runAction: [CCFollow actionWithTarget:Player]];
    

    Thanks LearnCocos2D, once again.