Troy R
Troy R

Reputation: 426

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;
}

Upvotes: 1

Views: 224

Answers (2)

Troy R
Troy R

Reputation: 426

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

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

Thanks LearnCocos2D, once again.

Upvotes: 1

Rich Fox
Rich Fox

Reputation: 2194

I am not sure if I understand what you are trying to do clearly, but I think maybe you want the map to move behind the player?
If this is the case you could try moving the map instead of self. I have a very similar function in my project, with basically one line different, and that is the last line.

self.position = viewPoint;

This is how it appears in my code:

map.position = viewPoint;

Hope that helps!

Upvotes: 0

Related Questions