views:

148

answers:

1

I'm making a particle system for my game, which basically is smoke coming out from rockets in my game. All particles are in a ccArray called smoke.

ccArray *smoke = ccArrayNew(0);

I have a class called Smoke, which is a subclass of CCSprite, with the addition of an ivar called __opacity.

When I add a new smoke to the ccArray I do like this:

ccArrayAppendObject(smoke, [Smoke spriteWithFile: @"smoke.png"]);
[smoke->arr[smoke->num - 1] setupWithTouch: touch andOpacity: 255.0f];
[self addChild: smoke->arr[smoke->num - 1]];

Which doesn't lag at all, and is fast,

And this is how I handle the smoke every frame:

if(smoke->num > 0)
{
    for(NSUInteger i = 0; i < smoke->num; i++)
    {
        Smoke *s = smoke->arr[i];

        s.__opacity = s.__opacity - 255.0f * delta;

        [s setOpacity: s.__opacity];

        if(s.__opacity <= 0.0f)
        {
            [self removeChild: s cleanup: YES];
            ccArrayFastRemoveObjectAtIndex(smoke, i);
        }
    }
}

When opacity is less than 0, we remove the smoke from the scene, and then

remove it from the array -- which is the part that slows the game down, removing it from the Array. It goes from 60 FPS to like 15-20 FPS, when there's like, 60 smoke particles on the scene.

Any ideas how I can speed this up?

Also, reason I'm using ccArray instead of NSMutableArray is because I read ccArray is faster.

+1  A: 

removing object from the middle or the beginning of an array (any array) will recreate the array, and the operation is very slow (alloc+copy of members), if you have datastruct with many removes that not at the end you probably should use a linked-list

here some implementation i found in the internet (haven't tested it but it looks decent) http://www.cocoadev.com/index.pl?DoublyLinkedList

Guy Ephraim
Or an `NSSet` if you don't need ordered access.
jshier
NSSet is a very good solution if you don't need ordered as Apple didn't provide implementation to linked-list
Guy Ephraim