如何在cocos2d box2d中移动相机

时间:2013-04-06 11:07:19

标签: cocos2d-iphone box2d-iphone

-(void)moveCamera:(CGpoint)resultVector {
float centerX, centerY, centerZ;
float eyeX, eyeY, eyeZ;

[self.camera centerX:&centerX centerY=&centerY centerZ=&centerZ];
[self.camera eyeX:&eyeX eyeY:&eyeY eyeZ:&eyeZ];

float newX = (resultVector.x - actorOriginalPosition.x) + (actorOriginalPosition.x - screenSize.width/2);
float newY = (resultVector.y - actorOriginalPosition.y) + (actorOriginalPosition.y - screenSize.height/2);

[self.camera setCenterX:newX centerY=newY centerZ:-150.0f];
[self.camera setEyeX:newX eyeY=newY eyeZ:eyeZ];

}

这里resultVector是actor currentPosition

1 个答案:

答案 0 :(得分:0)

更新功能中的

CGPoint direction = [self getPoint:myBody->GetPosition()];
[self setPosition:direction];


- (CGPoint)getPoint:(b2Vec2)vec
{
    CGSize screen = [[CCDirector sharedDirector] winSize];

    float x = vec.x * PTM_RATIO;
    float y = vec.y * PTM_RATIO;

    x = MAX(x, screen.width/2);
    y = MAX(y, screen.height/2);

    float _x = area.width  -  (screen.width/2);
    float _y = area.height - (screen.height/2);

    x = MIN(x, _x);
    y = MIN(y, _y);

    CGPoint goodPoint = ccp(x,y);

    CGPoint centerOfScreen = ccp(screen.width/2, screen.height/2);
    CGPoint difference = ccpSub(centerOfScreen, goodPoint);

    return difference;
}