2

我根据在网上找到的一些教程创建了一个简单的 2 帧精灵动画。我拍了两张图片,用 plist 和 png 文件创建了一个精灵表,并将它们合并到我的代码中,如下所示。此设置在 Cocos2d V 1.0.1 中运行良好。我刚刚将我的项目升级到 2.0rc0a,现在我的应用程序在它应该从第一帧切换到第二帧的地方崩溃并出现以下错误:'CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteBatchNode'

我查看了这个SO question,但我不确定这是否与我做错的事情相同,并且因为我对 Cocos2d 还是很陌生,我不确定如何正确调整我的代码。这是我在注释中没有看到的 2.0 更改的内容,我应该报告的错误,还是我的编码不正确?我仍然有一个 1.0.1 项目的副本,其代码相同,并且动画可以正常工作。

//CHAMELEON
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"front page/mainchameleon.plist"];
mainChameleon = [CCSpriteBatchNode batchNodeWithFile:@"front page/mainchameleon.png"];
[self addChild:mainChameleon z:7];
NSMutableArray *chameleonFrames = [NSMutableArray array];
for (int i = 1; i <= 2; ++i) 
{
    [chameleonFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"chameleon%d.png", i]]];
}

CCAnimation *mouthAnim = [CCAnimation animationWithSpriteFrames:chameleonFrames delay:0.3f];
chameleon = [CCSprite spriteWithSpriteFrameName:@"chameleon1.png"];
CCAnimate *chameleonAction = [CCAnimate actionWithAnimation:mouthAnim];
CCDelayTime *chameleonDelay = [CCDelayTime actionWithDuration:10];
CCRepeatForever *chameleonRepeat = [CCRepeatForever actionWithAction:[CCSequence actions:chameleonDelay, chameleonAction, chameleonDelay, nil]]; 
[chameleon runAction:chameleonRepeat];
[mainChameleon addChild:chameleon];

如果我注释掉,chameleon = [CCSprite spriteWithSpriteFrameName:@"chameleon1.png"];则应用程序不会崩溃,但变色龙根本不会出现,正如当前编写代码的方式所预期的那样。

或者,如果我注释掉,[chameleon runAction:chameleonRepeat];那么变色龙就会出现,显示帧 chameleon1.png,但显然没有通过动画。

好的,所以为了让我更加困惑,因为我肯定遗漏了一些东西,我尝试将代码的底部部分更改为此,动画从第 1 帧转到第 2 帧,然后无限期地停留在第 2 帧。但是,如果我使延迟时间超过 1.0,我会收到与之前相同的错误。chameleonDelay如果我在没有重复永远语句的情况下在操作之前重新包含,我也会遇到同样的崩溃。如果应用程序必须等待超过 1 秒才能执行切换,它似乎会崩溃。我需要的是让第 1 帧坐一会儿(10 秒),然后切换到第 2 帧 0.3 秒,然后切换回第 1 帧并再次坐一会儿。

尝试的代码#2:

CCAnimation *mouthAnim = [CCAnimation animationWithSpriteFrames:chameleonFrames delay:0.3f]; //<--- maxes out at 1.0. Anything more causes crash
chameleon = [CCSprite spriteWithSpriteFrameName:@"chameleon1.png"];
CCAnimate *chameleonAction = [CCAnimate actionWithAnimation:mouthAnim];     
[chameleon runAction:chameleonAction];
[mainChameleon addChild:chameleon];

YvesLeBorg 建议使用 restoreOriginalFrame 语句,但在 2.0 版中已弃用。我尝试使用

CCAnimation *mouthAnim = [CCAnimation animationWithAnimationFrames:chameleonFrames delayPerUnit:0.3f loops:5];

并得到错误'-[CCSpriteFrame delayUnits]: unrecognized selector sent to instance'。我不确定为什么这不起作用或从这里尝试什么。

编辑:所以现在它正在工作......但没有像我想要的那样有效地编码:

新代码:

//CHAMELEON
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"front page/mainchameleon.plist"];
mainChameleon = [CCSpriteBatchNode batchNodeWithFile:@"front page/mainchameleon.png"];
[self addChild:mainChameleon z:7];
NSMutableArray *chameleonFrames = [NSMutableArray array];

//Frame 1 - closed mouth
[chameleonFrames addObject:[CCSpriteFrame frameWithTexture:mainChameleon.texture rect:CGRectMake(0, 124, 149, 122)]];
//Frame 2 - Open Mouth
[chameleonFrames addObject:[CCSpriteFrame frameWithTexture:mainChameleon.texture rect:CGRectMake(0, 0, 149, 122)]];
//Frame 1 - closed mouth
[chameleonFrames addObject:[CCSpriteFrame frameWithTexture:mainChameleon.texture rect:CGRectMake(0, 124, 149, 122)]];

CCAnimation *mouthAnim = [CCAnimation animationWithSpriteFrames:chameleonFrames delay:0.9f];
    chameleon = [CCSprite spriteWithTexture:mainChameleon.texture rect:CGRectMake(0,124,149,122)];
CCAnimate *chameleonAction = [CCAnimate actionWithAnimation:mouthAnim];
CCDelayTime *chameleonDelay = [CCDelayTime actionWithDuration:10];
CCRepeatForever *chameleonRepeat = [CCRepeatForever actionWithAction:[CCSequence actions:chameleonDelay, chameleonAction, nil]]; 
[chameleon runAction:chameleonRepeat];
[mainChameleon addChild:chameleon];

我真的很喜欢我在 1.0.1 中的做法,因为如果我有 2 帧或 100 帧,我只需要对 if 语句做一个小的调整。这种方式需要对每个单独的帧进行编码,这似乎与使用 plist 有悖常理。如果在接下来的几天内没有人能够提供更好的解决方案或“真实”答案,我会将其发布为答案并接受它以结束问题。

4

2 回答 2

3

这是我最终得到的代码,它可以正常工作。不知道为什么我必须进行这些更改,但确实如此。(当我开始一个新项目来解决这个问题时,一些名称已经改变,但我的原始代码和这之间的差异是显而易见的)。对于其他找到此线程的人,我的原始代码基于Ray Wenderlich 的精灵表教程

CCSpriteBatchNode *chameleonBN = [CCSpriteBatchNode batchNodeWithFile:@"chameleonimages.png"];
    [self addChild:chameleonBN];

//ADDED the texture part to resolve: 'CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteBatchNode'   
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"chameleonplist.plist" texture:chameleonBN.texture];

NSMutableArray *chameleonframes = [NSMutableArray array];

for (int i = 1; i <= 2 ; i++) 
{
    [chameleonframes addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"chameleon%d.png", i]]];
}

CCAnimation *mouthAnim = [CCAnimation animationWithSpriteFrames:chameleonframes delay:0.9f];

//ADDED this sprite frame to resolve texture id assert error
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:chameleonBN.texture rect:CGRectMake(0, 0, 149, 122)];
CCSprite *chameleon = [CCSprite spriteWithSpriteFrame:frame];
chameleon.position = ccp(512,384);
CCAnimate *chameleonAnimate = [CCAnimate actionWithAnimation:mouthAnim];

CCDelayTime *chameleonDelay = [CCDelayTime actionWithDuration:10];
CCDelayTime *chameleonDelay2 = [CCDelayTime actionWithDuration:0.1];//Had to use this to ge tthe mouth to close. Using restore original frame doesn't work for me.
CCRepeatForever *chameleonRepeat = [CCRepeatForever actionWithAction:[CCSequence actions:chameleonDelay, chameleonAnimate, chameleonDelay2, nil]]; 
[chameleon runAction:chameleonRepeat];
[chameleonBN addChild:chameleon];
于 2012-04-17T14:31:39.087 回答
0

不确定这是否是 2.0 问题,但我注意到您在同一序列中使用了两次 chameleonDelay 对象。这真的是你想要完成的吗?

延迟,行动,延迟,延迟,行动,延迟,延迟,行动......等。

试试这个看看它是否有效:

CCRepeatForever *chameleonRepeat = [CCRepeatForever actionWithAction:[CCSequence actions:chameleonDelay, chameleonAction, nil]];
于 2012-04-05T17:14:06.377 回答