gpt4 book ai didi

ios - cocos2d动画优化失败

转载 作者:行者123 更新时间:2023-11-29 13:19:00 27 4
gpt4 key购买 nike

当我不优化动画时,它运行良好。但是当我优化我的动画时,程序崩溃了。Xcode 日志说:

Assertion failure in -[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];

最佳答案

为此,您的 Icon.png 纹理应该位于 birdAtlas.png 纹理中,并在 .plist 中进行适当的声明。 Batchnodes 1) 是用一个纹理创建的(并且只有一个),并且 2) 只接受来自相同纹理的子 Sprite 。

... 而且,我不知道你的意图,但通常你会有

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

在那种情况下,我认为批处理节点添加会起作用。

...并且,不确定使用批处理节点是否会对动画产生任何影响,如果纹理仅包含该动画的动画帧。帧一次显示一个,所以我认为您不会从批量绘制调用中受益。

关于ios - cocos2d动画优化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14854955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com