简体   繁体   中英

cocos2d ccArray removing object is slow

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM