2012-01-19 100 views
6

Cocos2d可以創建一個移動動畫,該動畫將在特定路徑上進行嗎?Cocos2d在路徑上移動動畫

例如,如果我需要一個對象在弧形或整圓上移動,該怎麼辦?

問候!

回答

9

當然,你可以使用這樣做:

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]; 

可以使用ccBezier移動任何節點曲線:

現在動畫部分:

[[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]; 

參考鏈接:http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d

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