1

アニメーションを最適化していないときはうまくいきましたが、アニメーションを最適化するとプログラムがクラッシュしました。Xcode ログには次のように書かれていました。

-[CCSpriteBatchNode addChild:z:tag:]、/Users/hanpengbo/Documents/Xcode/cocos2d_helloWorld/cocos2d_helloWorld/libs/coco‌ s2d/CCSpriteBatchNode.m:183 でのアサーションの失敗

CCSpriteBatchNode.m:183 には、

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

ここに私のコードがあります

// 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];

更新: ここに修正されたコードがあり、うまく機能します

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

1 に答える 1