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...