Han Pengbo
Han Pengbo

Reputation: 1436

optimize animation in cocos2d failed

When I didn't optimize animation,it worked well.But when I optimized my animation,the program crashed.And Xcode log said:

Assertion failure in -[CCSpriteBatchNode addChild:z:tag:], /Users/hanpengbo/Documents/Xcode/cocos2d_helloWorld/cocos2d_helloWorld/libs/coco‌​s2d/CCSpriteBatchNode.m:183

in CCSpriteBatchNode.m:183,there is

NSAssert( child.texture.name == textureAtlas_.texture.name, @"CCSprite is not using the same texture id");

here is my code

// cache
    CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache addSpriteFramesWithFile:@"birdAtlas.plist"];

    // frame array
    NSMutableArray *framesArray=[NSMutableArray array];
    for (int i=1; i<10; i++) {
        NSString *frameName=[NSString stringWithFormat:@"bird%d.png", i];
        id frameObject=[cache spriteFrameByName:frameName];
        [framesArray addObject:frameObject];
    }

    // animation object
    id animObject=[CCAnimation animationWithFrames:framesArray delay:0.1];

    // animation action
    id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
    animAction=[CCRepeatForever actionWithAction:animAction];

    // sprite
    cocosGuy = [CCSprite spriteWithFile: @"Icon.png"];//cocosGuy is CCSprite,declared earler
    cocosGuy.position = ccp( 200, 300 );

    // batchNode
    CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"birdAtlas.png"];
    [self addChild:batchNode];
    [batchNode addChild:cocosGuy];

    [cocosGuy runAction:animAction];

UPDATE: here is the corrected code,and it works well

    // batchNode
    CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"birdAtlas.png"];
    [cocosGuy setTexture:[batchNode texture]];
    [self addChild:batchNode];
    [batchNode addChild:cocosGuy];

Upvotes: 1

Views: 400

Answers (1)

YvesLeBorg
YvesLeBorg

Reputation: 9079

For this to work, your Icon.png texture should be in the birdAtlas.png texture, with appropriate declaration in the .plist. Batchnodes 1) are created with one texture (and only one), and 2) only accept as children sprites that are from the SAME texture.

... and , i dont know your intent, but typically you would have

CCSprite *cocosGuy = [CCSprite spriteWithSpriteFrame:[cache spriteFrameByName:@"bird1.png"];

in that case, i think the batch node add will work.

... and , not sure using a batch node will be of any consequence for an animation, if the texture only contains the animation frames for that one animation. Frames are displayed one at a time, so i dont think you will benefit from the batched draw call.

Upvotes: 1

Related Questions