Search code examples
arrayscocos2d-iphoneccsprite

Cocos2d: How can I dynamically change size of array of CCSprite objects?


For example: I made this array to keep all my CCSprites:

CCSprite *Enemy[20];

I figured it would be okay, since I would have no more than 20 enemies, but now I want to be able to increase the size of it, once it's filled with objects, to be 40.


Solution

  • NSMutableArray *mySprites = [[NSMutableArray alloc] init];
    
    
    // array is mutable so you can add to it
    [mySprites addObject: newSprite];
    
    // objective c syntax has improved, you can now access elements with the following syntax
    CCSprite *mySprite = mySprites[0];