Cocos2d rotating sprite while moving with CCBezierBy

Posted by marcg11 on Game Development See other posts from Game Development or by marcg11
Published on 2011-12-03T11:43:37Z Indexed on 2012/06/10 16:48 UTC
Read the original article Hit count: 259

Filed under:
|
|

I've done my moving actions which consists of sequences of CCBezierBy. However I would like the sprite to rotate by following the direction of the movement (like an airplane). How sould I do this with cocos2d?

I've done the following to test this out.

    CCSprite *green = [CCSprite spriteWithFile:@"enemy_green.png"];
    [green setPosition:ccp(50, 160)];
    [self addChild:green];

    ccBezierConfig bezier;
    bezier.controlPoint_1 = ccp(100, 200);
    bezier.controlPoint_2 = ccp(400, 200);
    bezier.endPosition = ccp(300,160);

    [green runAction:[CCAutoBezier actionWithDuration:4.0 bezier:bezier]];

In my subclass:

@interface CCAutoBezier : CCBezierBy

@end

@implementation CCAutoBezier

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

-(void) update:(ccTime) t
{
    CGPoint oldpos=[self.target position];
    [super update:t];
    CGPoint newpos=[self.target position];

    float angle = atan2(newpos.y - oldpos.y, newpos.x - oldpos.x);
    [self.target setRotation: angle];
}

@end

However it rotating, but not following the path...

© Game Development or respective owner

Related posts about sprites

Related posts about cocos2d