2012-04-10 17 views
2

so bin ich auf cocos2d aber bevor ich auf einem normalen ios app war und ich hatte diesen Code:hinzufügen Animation Schicht Pfad in cocos2d

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

Aber ich fand nicht einen Weg, um die Animation zu tun von boxpath bis roundpath auf cocos2d, ich weiß nicht, was CCAction verwenden. Kann mir jemand helfen ? Entschuldigung für mein Englisch Ich bin Französisch:/

Antwort

2

Meines Wissens gibt es keine direkte Schnittstelle in Cocos2d, um CGPaths von Apples CoreAnimation-Bibliothek zu verwenden.


EDIT1: Sorry, falsch verstanden ich Ihre Frage! Cocos2d bietet keine Werkzeuge zum Animieren/Morphen von einer Form, die durch einen Pfad definiert wird, zu einer anderen Form, die durch einen anderen Pfad definiert ist. Sie müssen entweder Ihre eigene benutzerdefinierte Klasse erstellen, die von CCNode erbt, oder eine andere Bibliothek als Cocos2d verwenden. Wenn Sie Ihre eigene CCNode Unterklasse tun erstellen, dann die vorhandenen Cocos2d Aktion/Animations-Tools können von Nutzen sein ...


EDIT2: Wenn Sie verstehen, wie, um eine Unterklasse dann ist hier eine Verbindung mit der sehr Grundlagen, welche Methoden wollen Sie in einer Unterklasse von CCNode außer Kraft zu setzen: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update

auch der cocos2d Programmführer sein kann hilfreich im allgemeinen: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

Wenn Sie nicht verstehen, wie man eine Unterklasse (zB einen neuen class erben von einem bestehenden class), dann würde ich Ihnen empfehlen, diesen Teil der objektorientierten Programmierung mit einem einfacheren Problem zu erlernen!


Ich glaube, die beste Wahl in CCSequence zu suchen ist (dies ist eine Art von CCAction ist) und es Sachen mit einer Reihe von CCMoveTo oder CCMoveBy Aktionen.

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

+0

aber ich werde eine lose viel Speicher, denn mit Ihrer Idee werde ich vielleicht 30 Aktionen tun und es wird ein CCSequence von 30 Aktionen sein. –

+0

Wenn Sie sich Gedanken über Speicher machen, können Sie ihn in mehrere Sequenzen aufteilen und mit CCCallFunc verknüpfen. –

+0

ok. aber ich möchte kein Bild auf einem Pfad verschieben. Ich habe einen Pfad Boxpath und ich mag es, seine Form zu runden Pfad zu ändern. –