2012-02-15 254 views
0

我正在使用cocos2d和box2d phisycs開發應用程序。我想讓我的精靈在移動時變成動畫。我在Zwoptex中創建了* .plist和* .png文件,並將它們添加到我的項目中。現在,我試圖創建一個精靈:精靈動畫片

 [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"SquirrelAnimation.plist"]; 

     node = [CCSpriteBatchNode batchNodeWithFile:@"SquirrelAnimation.png" capacity:100]; 
     spriteTexture = [node texture]; 

     b2BodyDef bodyDef; 
     bodyDef.type = bodyType; 
     CGSize size = [CCDirector sharedDirector].winSize; 
     CGPoint point = ccp(size.width/2, size.height/2); 
     bodyDef.position.Set(point.x/PTM_RATIO, point.y/PTM_RATIO); 
     body = world->CreateBody(&bodyDef); 

     sprite = [PhysicsSprite spriteWithTexture:spriteTexture]; 
     [sprite setPhysicsBody:body]; 
     [node addChild:sprite]; 

但是,這段代碼使得一個sprite的所有幀到節點。我究竟做錯了什麼?

回答

2

提取幀像這樣...

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spritesheet.plist"]; 
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"spritesheet.png"]; 


    [self addChild:spriteSheet]; 

    NSMutableArray *frames = [[[NSMutableArray alloc]init]retain]; 


    for(int i = 1; i <= numberFrames; i++) { 
      [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@0%d.png", file_name, i]]]; 
     } 


    // Animation object with 0.04 seconds between each frame (~30fps) 
     CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.04f]; 

     if(self.sprite){ 
      // Animate the sprite 
      [self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]]; 
     } 
+0

我使用Box2D的,所以我的看法b2World更新的每次迭代更新。看起來'runAction'在這裏不起作用。我應該嘗試通過手動代碼更改每次迭代的精靈嗎?或有其他解決方案? – SentineL 2012-02-15 05:50:51

+0

它只是例如...你得到數組中的幀,然後你可以根據你使用它,即你使用CCAnimation或只是for循環..滿足您的要求.... – 2012-02-15 05:54:14