2014-04-01 126 views
0

我在动画我的精灵图片时遇到了一些问题。我之前得到了这个代码,它工作。我有2帧,我的循环中的变量只允许达到0和1的值,因为帧名是stickman0-568.png和stickman1-568.png。我的框架不太好,所以我制作了一个新的精灵画面,其中有3个框架,stickman0-568.png,stickman1-568.png和stickman2-568.png。我的.list文件叫做sickman-568.plist,而.png精灵表是stickman-568.png。我改变了循环,所以我只能达到2(0,1,2)的值。当我现在运行它时,它会崩溃,尽管它以前只有2帧时才工作。任何人都可以请给我一些关于可能出错的建议吗?这里是我的代码,所以你可以看到我做了什么:在Cocos2d中动画雪碧图片

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

回答

1

当您运行循环的代码中创建图像,如下名

stickman00-568.png 
stickman01-568.png 
stickman02-568.png 

,是不是在你的源图像,所以只有跌破取代在for循环的代码行。它工作正常。

更改代码如下。

CCSpriteFrame *frames = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"stickman%d-568.png", i]]; 

See this

+0

感谢您的建议,但这不是问题。我意识到我的.plist文件的元数据部分中的我的textureFileName与我的代码不匹配。谢谢,不过。 – FreeFire