2012-05-15 76 views
0

我對cocos2d更新,準備我的演示遊戲。我正在從右向左移動一個精靈,只用一個圖像,像從左到右移動的鳥圖像。但我想通過各種圖像來動畫這個精靈,以便它看起來像一隻飛翔的小鳥。我不知道該怎麼做。Animate CCSprite

這裏是我的代碼:

CCSprite *target = [CCSprite spriteWithFile:@"Target.png" rect:CGRectMake(0, 0, 27, 40)] 
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)]; 
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)]; 
    [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

欣賞你的答案

先謝謝了!

回答

3

對於特定的精靈動畫你需要精靈表在你的資源中。您可以從我通常使用的Texture PackerZwoptex工具創建Sprite表。

接下來,您可以實現下面的代碼

CCSpriteBatchNode *sheet = [CCSpriteBatchNode batchNodeWithFile:@"drawing1-i3.png"]; // Png File Name 
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"drawing1-i3.plist"]; // Plist File Name 
    [self addChild:sheet]; 

     //Generating the Animation 
    NSMutableArray *arr_anim =[NSMutableArray array]; 
    for(int i=1; i<30; i++) // i< number of frames in the plist File Name 
    { 
     NSString *str_fileNm = [NSString stringWithFormat:@"drawing1%d.png",i]; 
     CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:str_fileNm]; 
     [arr_anim addObject:frame]; 
    } 

    CCSprite *startAnim = [CCSprite spriteWithSpriteFrameName:@"drawing11.png"]; 
    [startAnim setPosition:ccp(150,150)]; 
    [self addChild:startAnim]; 

    //Starting the Animation 
    CCAnimation *animation = [CCAnimation animationWithFrames:arr_anim delay:0.15f]; 
    // id action =[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES]]; 
    id action =[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]; 
    [startAnim runAction:action]; 

我認爲這將幫助您創建的動畫。

+0

精靈表不是動畫的必需條件 – johnbakers

+0

應該將'startAnim'精靈添加爲批處理節點的子節點,這樣它只能使用單個OpenGL調用或「批處理繪製」。 – nrj