iphone - 在cocos2d中添加动画到图层路径

标签 iphone ios cocos2d-iphone core-animation cgpath

所以我在cocos2d上,但在我使用普通的ios应用程序之前,我有这个代码:

-(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"];
}

但是我没有找到在cocos2d上做从boxpathroundpath的动画的方法,我不知道CCAction使用什么。有谁能够帮助我 ?抱歉我的英语不好,我是法国人:/

最佳答案

据我所知,Cocos2d 中没有直接接口(interface)来使用 Apple 的 CoreAnimation 库中的 CGPaths。


编辑1:抱歉,我误解了你的问题! Cocos2d 没有提供从由路径定义的一个形状到由不同路径定义的另一个形状的动画/变形工具。您需要创建自己的继承自 CCNode 的自定义类,或者使用 Cocos2d 以外的其他库。如果您确实创建了自己的 CCNode 子类,那么现有的 Cocos2d Action /动画工具可能会有用...


编辑2:如果您了解如何子类化,那么这里有一个链接,其中包含您想要在 CCNode 子类中重写哪些方法的基本知识:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update

此外,cocos2d 编程指南总体上可能会有所帮助:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

如果您不了解如何子类化(例如,从现有的 class 继承新的 class ),那么我建议您使用更简单的问题来学习面向对象编程的这一部分!


<罢工> 我相信您最好的选择是查看 CCSequence (这是 CCAction 的类型)并用一系列 CCMoveTo 填充它或CCMoveBy行动。

<罢工>

教程链接:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_composition

关于iphone - 在cocos2d中添加动画到图层路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10093470/

相关文章:

ios - Swift 中如何计算坐标

ios - SWRevealViewcontroller 不适用于 iOS 7.1

iphone - Xcode 将 PDF 保存为图像?

iphone - 如何订阅设备方向(非界面方向)事件?

ios - CCLabelTTF错误:无法识别的选择器已发送给类(class)

iphone - 程序收到信号 EXC_BAD_ACCESS 访问数组

ios - 如何在 Swift 中制作随机 Sprite 位置生成器

ios - 核心数据迁移后主 TableView 崩溃 Swift

iOS;当应用程序未运行或在后台时,推送通知不起作用

iphone - cocos2d-iOS - 手势识别器