为什么CCAnimation不使用CCSpriteBatchNode来打印其图像?

时间:2012-04-17 21:37:36

标签: iphone cocos2d-iphone ccsprite

为什么CCSpriteBatchNode未明确与CCAnimation一起使用?相反,我们使用以下内容:(而不是将每个图像添加到batchNode并让batchNode打印这些图像,代码只使用spriteFrameByName):

CCSpriteBatchNode *chapter2SpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"scene1atlas.png"];
CCSprite *vikingSprite = [CCSprite spriteWithSpriteFrameName:@"sv_anim_1.png"];
[chapter2SpriteBatchNode addChild:vikingSprite];


// Animation example with a CCSpriteBatchNode
CCAnimation *exampleAnim = [CCAnimation animation];
    [exampleAnim addFrame:
    [[CCSpriteFrameCache sharedSpriteFrameCache] 
    spriteFrameByName:@"sv_anim_2.png"]];

感谢您的回答

1 个答案:

答案 0 :(得分:0)

batchNode只是一个纹理绘制工件......不知道纹理包含哪些帧,也不知道它是否在单个纹理中嵌入多个“逻辑”文件。您必须创建该关联,通常通过将spriteFrame添加到spriteFrameCache,每个spriteFrame提供有关batchNode的“片段”的元数据。以下是我的一个游戏中的示例:

-(void) setupIdleAnimation{

    [self setupAnimations];
    NSString* animationName = @"Idle";
    NSString* framesFileName = [self getPlistFileNameForAction:animationName];
    CCSpriteBatchNode *bn = [CCSpriteBatchNode batchNodeWithFile:[self getTextureFileNameForAction:animationName]];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:framesFileName texture:bn.texture];

// create array of frames for the animation

    self.idleBatchNode=bn;

    NSMutableArray *animFrames = [NSMutableArray array];
    for(NSUInteger i = 1; i <= 8; ++i) {

        NSString *sfn = [self getFrameNameForAnimation:animationName
                                    andFrameNumber:i];

        CCSpriteFrame *sf = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:sfn];
        if(sf) {
            [animFrames insertObject:sf atIndex:i-1];
        } else {
            CCLOGERROR(@"%@<setupIdleAnimation> : *** Sprite frame named [%@] not found in cache, bailing out.",self.class,sfn);
            return;
        }
    }


    CCAnimation *anim=[CCAnimation animationWithFrames:walkAnimFrames delay:ANIM_FRAME_DELAY];
    [animFrames removeAllObjects];
    anim.name=animationName;

    self.idleAction = [CCRepeatForever 
                   actionWithAction:[CCAnimate actionWithAnimation:anim 
                                              restoreOriginalFrame:NO]] ;

    self.idleSprite = [CCSprite spriteWithSpriteFrameName:[self getFrameNameForAnimation:animationName
                                                                      andFrameNumber:1]];  
    self.idleSprite.visible=NO;
    [self.idleBatchNode addChild:self.idleSprite];
} 

所以我在.plist中准备了描述每个帧在纹理中的位置的数据,并将这些定义添加到帧缓存中。每个说的batchNode是一个可以优化渲染性能的容器。在上面的例子中,非常适合单独使用纹理嵌入16个字符类的空闲精灵,这些类通常在视图中并且同时空闲。

您可以在this tutorial.

中找到一个好的介绍