1

我使用 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);

说不好的访问。

请帮我解决这个问题,为什么不能将动画精灵添加到身体中???

谢谢你。

4

1 回答 1

1

这是我想出来的解决方案。

如果您从 plist 制作 sprite 表并希望将动画表添加到 body 中,请确保首先将 sprite 对象添加到 body 然后将 sprite 添加到表中。

这是正确的代码

//================== 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
于 2011-08-24T12:13:21.527 回答