我在为 sprite 表设置动画时遇到了一些问题。我之前得到了这段代码并且它有效。我有 2 个帧,我的循环中的变量只允许达到 0 和 1 的值,因为帧名称是 stickman0-568.png 和 stickman1-568.png。我的帧不太好,所以我制作了一个包含 3 个帧的新 Sprite 表,stickman0-568.png、stickman1-568.png 和 stickman2-568.png。我的 .list 文件名为 sickman-568.plist,.png Sprite 表为 stickman-568.png。我改变了循环,所以我只能达到 2(0,1,2) 的值。当我现在运行它时,它崩溃了,尽管它在我只有 2 帧之前工作。谁能给我一些可能出了什么问题的建议?这是我的代码,您可以看到我做了什么:
-(id) init
{
if( (self=[super init]) )
{
backGroundDesert = [CCSprite spriteWithFile:@"DesertMap.png"];
backGroundDesert.position = ccp(backGroundDesert.textureRect.size.width/2, backGroundDesert.textureRect.size.height/2);
[self addChild:backGroundDesert];
[backGroundDesert runAction:[CCMoveBy actionWithDuration:100 position:ccp(-1400,0)]];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"stickman-568.plist"];
CCSprite *mainGuy = [CCSprite spriteWithSpriteFrameName:@"stickman0-568.png"];
mainGuy.position = ccp(40,backGroundDesert.textureRect.size.height/2);
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"stickman-568.png"];
[batchNode addChild:mainGuy];
[self addChild:batchNode];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 0; i < 3; i++)
{
CCSpriteFrame *frames = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"stickman%02d-568.png", i]];
[animFrames addObject:frames];
}
CCAnimation *cape = [CCAnimation animationWithSpriteFrames:animFrames delay:0.2f];
[mainGuy runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:cape restoreOriginalFrame:NO]]];
}
return self;
}
-(void)dealloc
{
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[super dealloc];
}
最佳答案
当您运行代码时,for 循环会创建如下名称的图像
stickman00-568.png
stickman01-568.png
stickman02-568.png
那不是您源代码中的图像,因此仅替换您的 for 循环代码中的以下行。它工作正常。
如下更改代码。
CCSpriteFrame *frames = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"stickman%d-568.png", i]];
关于ios - Cocos2d 中的动画 Sprite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775812/