Box2D机身带有动画精灵表plist

时间:2011-08-23 15:30:03

标签: cocos2d-iphone box2d sprite ccsprite

我使用CCSpriteBatchNode和CCSprite创建了一个动画精灵。我使用plist来获取帧。这是我在init()中添加的代码。

//================== 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 个答案:

答案 0 :(得分: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
相关问题