2014-04-01 125 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