Search code examples
iphoneioscocos2d-iphonecore-animationcgpath

add animation to layer's path in cocos2d


so i'm on cocos2d but before I was on a normal ios app and I had this code :

-(void)viewDidLoad
{
    rootLayer = [[CALayer alloc] init];
    [imageView.layer addSublayer:rootLayer];
    roundPath = CGPathCreateMutable();

    CGPathMoveToPoint(roundPath, nil, center.x , center.y - 35);
    CGPathAddArcToPoint(roundPath, nil, center.x + 35, center.y - 35, center.x + 35, center.y + 35, 35);
    CGPathAddArcToPoint(roundPath, nil, center.x + 35, center.y + 35, center.x - 35, center.y + 35, 35);
    CGPathAddArcToPoint(roundPath, nil, center.x - 35, center.y + 35, center.x - 35, center.y, 35);
    CGPathAddArcToPoint(roundPath, nil, center.x - 35, center.y - 35, center.x, center.y - 35, 35);

    CGPathCloseSubpath(roundPath);

    //Box Path

    boxPath = CGPathCreateMutable();

    CGPathMoveToPoint(boxPath, nil, center.x , center.y - 35);
    CGPathAddArcToPoint(boxPath, nil, center.x + 35, center.y - 35, center.x + 35, center.y + 35, 4.7);
    CGPathAddArcToPoint(boxPath, nil, center.x + 35, center.y + 35, center.x - 35, center.y + 35, 4.7);
    CGPathAddArcToPoint(boxPath, nil, center.x - 35, center.y + 35, center.x - 35, center.y, 4.7);
    CGPathAddArcToPoint(boxPath, nil, center.x - 35, center.y - 35, center.x, center.y - 35, 4.7);

    CGPathCloseSubpath(boxPath);
    shapeLayer = [CAShapeLayer layer];

    shapeLayer.path = boxPath;

    [rootLayer addSublayer:shapeLayer];
}

-(void)startAnimation
{   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];

    animation.duration = 2.0;

    animation.repeatCount = HUGE_VALF;

    animation.autoreverses = YES;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    animation.fromValue = (id)boxPath;

    animation.toValue = (id)roundPath;

    [shapeLayer addAnimation:animation forKey:@"animatePath"];
}

But I didn't found a way to do the animation fromboxpath toroundpath on cocos2d, I don't know what CCAction use . Can anybody help me ? sorry for my english I'm french :/


Solution

  • To my knowledge, there is no direct interface in Cocos2d to use CGPaths from Apple's CoreAnimation library.


    EDIT1: Sorry, I misunderstood your question! Cocos2d provides no tools to animate/morph from one shape defined by a path to another shape defined by a different path. You'll either need to create your own custom class that inherits from CCNode, or use some library other than Cocos2d. If you do create your own CCNode subclass, then the existing Cocos2d action/animation tools may be of use...


    EDIT2: If you understand how to subclass, then here is a link with the very basics of what methods you want to override in a subclass of CCNode: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update

    Also, the cocos2d programming guide may be helpful in general: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

    If you don't understand how to subclass (e.g. have a new class inherit from an existing class), then I would recommend you learn about this part of Object Oriented Programming using a simpler problem!


    I believe your best option is to look into CCSequence (this is a type of CCAction) and stuff it with a series of CCMoveTo or CCMoveBy actions.

    Tutorial Link: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_composition