2011-08-23 54 views
1

我已經使用CCSpriteBatchNode和CCSprite創建了一個動畫精靈。我使用plist來獲取框架。這是我把它放在init()中的代碼。Box2D帶有動畫精靈圖表plist的主體

//================== making animating sprite 
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"framelist.plist"]; 
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode 
             batchNodeWithFile:@"frames.png"]; 
    [self addChild:spriteSheet]; 

    NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
    for(int i = 1; i <= 2; ++i) { 
     [walkAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"frame%d.png", i]]]; 
    } 

    CCAnimation *walkAnim = [CCAnimation 
          animationWithFrames:walkAnimFrames delay:0.1f]; 
    //_frameSprite is CC Sprite 

    _frameSprite = [CCSprite spriteWithBatchNode:spriteSheet 
             rect:CGRectMake(0,0,48,48)]; 
    _frameSprite.position = ccp(winSize.width + 60, winSize.height/2); 
    _flyAction = [CCRepeatForever actionWithAction: 
        [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]; 
    [_frameSprite runAction:_flyAction]; 
    [spriteSheet addChild:_frameSprite]; 

一旦子畫面是準備和運行在屏幕上我創建b2BodyDef和如下所示分配b2Body(即frameBodyDef,frameBody)我的精靈。

b2BodyDef frameBodyDef; 
    frameBodyDef.type = b2_staticBody; 
    frameBodyDef.position.Set(160/PTM_RATIO, 200/PTM_RATIO); 
    frameBodyDef.userData = _frameSprite; 
    frameBody = _world->CreateBody(&frameBodyDef); 

產生身體,當構建之後就跑,在線

frameBody = _world->CreateBody(&frameBodyDef); 

程序崩潰說不好的訪問。

請在此幫助我,爲什麼動畫精靈不能添加到身體?

謝謝。

+1

是正確初始化變量_world? – iforce2d

回答

1

這是我想出來的解決方案。

如果您從plist製作精靈表並希望您的動畫表添加到主體中,請確保先將精靈對象添加到主體,然後將精靈添加到表中。

這裏是正確的代碼

//================== making animating sprite 
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"framelist.plist"]; 
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode 
            batchNodeWithFile:@"frames.png"]; 
[self addChild:spriteSheet]; 

NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
for(int i = 1; i <= 2; ++i) { 
    [walkAnimFrames addObject: 
    [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
     [NSString stringWithFormat:@"frame%d.png", i]]]; 
} 

CCAnimation *walkAnim = [CCAnimation 
         animationWithFrames:walkAnimFrames delay:0.1f]; 
//_frameSprite is CC Sprite 

_frameSprite = [CCSprite spriteWithBatchNode:spriteSheet 
            rect:CGRectMake(0,0,48,48)]; 
_frameSprite.position = ccp(winSize.width + 60, winSize.height/2); 
_flyAction = [CCRepeatForever actionWithAction: 
       [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]; 
[_frameSprite runAction:_flyAction]; 

b2BodyDef frameBodyDef; 
frameBodyDef.type = b2_staticBody; 
frameBodyDef.position.Set(160/PTM_RATIO, 200/PTM_RATIO); 
frameBodyDef.userData = _frameSprite; //================first add the sprite to body 
frameBody = _world->CreateBody(&frameBodyDef); 

[spriteSheet addChild:_frameSprite]; //======second add sprite to the sheet