开发者

Populate NSMutableArray with CCSprites via for loop

开发者 https://www.devze.com 2023-02-11 18:07 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消