我在设定精灵表格动画时遇到一些问题。我之前获得了此代码,并且可以正常工作。我有2帧,并且我的循环中的变量只允许达到0和1的值,因为帧名分别为stickman0-568.png和stickman1-568.png。我的帧效果不是很好,所以我制作了一个新的精灵表,其中包含3个帧,stickman0-568.png,stickman1-568.png和stickman2-568.png。我的.list文件称为镰刀568.plist,.png精灵表为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]];
See this
关于ios - 在Cocos2d中动画 Sprite 表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22775812/