2012-04-17 62 views
0

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

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

谢谢您的回答

回答

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.

+0

感谢YvesLeBorg的回答很好的介绍和链接;)我更好地理解它是如何工作的,但是:我们可以在'CCSprite'实例添加到批次节点,但我们无法传递给它'CCAnimation'实例?两者都包含“帧”,所以都应该使用相同的技术来优化渲染性能?感谢 – Paul 2012-04-18 00:07:41

+0

关于性能,阅读安德鲁的答案在这里http://stackoverflow.com/questions/10234570/suitability-of-using-core-animation-on-ios-vs-using-cocos2d-and-opengl-es – YvesLeBorg 2012-04-20 11:19:04