Populate NSMutableArray with CCSprites via for loop
Hey guys, using obj-c and the cocos2d framework; How would I populate an NSMutableArray with CCSprite's with a for-loop to reduce code. I have +100 sprites/images that need to go into this array, so a for-loop is necessary.
This is my current code:
_backgrounds开发者_StackOverflow社区 = [[NSMutableArray alloc]initWithCapacity:31];
for (int i = 31; i > 1; i--){
[_backgrounds addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"background_%d.png", i]]];
}
Im looking for something along the lines of this: (Although this code doesnt work), for (int i = 31; i > 1; i--){ [backgrounds addObject:[CCSprite spriteWithFile:@"background%d.png", i]]; }
Thanks in advanced, Oliver.
Chances are your shared CCSpriteFrameCache does not contain any values. You must populate it prior to accessing anything from it.
精彩评论