所以我在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上从
boxpath
转换为roundpath
的动画的方法,我不知道CCAction使用了什么。有谁能够帮我 ?对不起,我的英语我是法语:/ 最佳答案
据我所知,Cocos2d中没有直接接口可以使用Apple CoreAnimation库中的CGPaths。
编辑1:抱歉,我误解了您的问题! Cocos2d没有提供任何工具来使路径定义的一种形状动画化或变形为另一路径定义的另一种形状。您将需要创建自己的从CCNode继承的自定义类,或者使用Cocos2d以外的其他库。如果您确实创建了自己的CCNode子类,那么现有的Cocos2d动作/动画工具可能会有用...
编辑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/