gpt4 book ai didi

animation - 从 Cocos2d 1.0.1 升级到 2.0 后,Sprite Sheet 动画失败

转载 作者:行者123 更新时间:2023-12-04 05:51:07 25 4
gpt4 key购买 nike

我根据我在网上找到的一些教程创建了一个简单的 2 帧 Sprite 动画。我拍摄了我的 2 张图片,用 plist 和 png 文件创建了一个 Sprite 表,并将它们合并到我的代码中,如下所示。此设置在 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 有悖常理。如果在接下来的几天内没有人能够提供更好的解决方案或“真实”答案,我会将其发布为答案并接受它以结束问题。

最佳答案

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

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

关于animation - 从 Cocos2d 1.0.1 升级到 2.0 后,Sprite Sheet 动画失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10030974/

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