Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d move animation on a path

Is it possible in Cocos2d to create a move animation that would go on a specific path?

For example, how should I do if I need an object to move on an arc or full circle?

Regards!

like image 842
mxg Avatar asked Jan 19 '12 10:01

mxg


1 Answers

Sure you can do this using :

    ccBezierConfig bezier;
    bezier.controlPoint_1 = ccp(320,0); // control point 1 
    bezier.controlPoint_2 =ccp(0,0); // control point 2
    bezier.endPosition = ccp(endPoint.x,endPoint.y) ;
    id bezierForward = [CCBezierTo actionWithDuration:3 bezier:bezier];
    [ball runAction:bezierForward];

you can use ccBezier to move any node in curves :

Now Animation Part :

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"eggAnimation.plist"];        
    spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"eggAnimation.png"];
    [gameBackgroundLayer addChild:spriteSheet];
    eggAnimFrames = [NSMutableArray array];
    for ( int i = 1; i <= 10; i++ )  
    {
        [eggAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%d.png", i]]];
    }
    rotateAnim = [CCAnimation animationWithFrames:eggAnimFrames delay:0.05f];
    ball = [CCSprite spriteWithSpriteFrameName:@"1.png"];
    ball.position=ccp(160,80);
    rotateAction =[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:rotateAnim restoreOriginalFrame:YES]];
    [spriteSheet addChild:ball];

Reference Links : http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d

http://www.math.ubc.ca/~cass/gfx/bezier.html

like image 76
Haroon Avatar answered Nov 03 '22 02:11

Haroon