0

I want to do a very simple thing but it's not working. I want to add some CCParticleSystemQuad in an NSMutableArray and delete them. Here is what I do :

int cpt = 0;
NSMutableArray *myArray = [[NSMutableArray alloc] init];
for (cpt = 0; cpt < 10; cpt++) {
    part = [CCParticleSystemQuad particleWithFile:@"whiteExplosion.plist"];
    [myArray addObject:part];
}

NSLog(@"state of myArray : %@", myArray);

int cont = 0
for (cont = 0; cont < 10; cont++) {
    [myArray removeLastObject:cont];
}

NSLog(@"state of myArray : %@", myArray);

When I NSLog the first time I have this :

state of myArray : (
    "<CCParticleSystemQuad = 0x91ee380 | Tag = -1>",
    "<CCParticleSystemQuad = 0x84aca20 | Tag = -1>",
    "<CCParticleSystemQuad = 0x125136c0 | Tag = -1>",
    "<CCParticleSystemQuad = 0x125b0fc0 | Tag = -1>",
    "<CCParticleSystemQuad = 0x1250d480 | Tag = -1>",
    "<CCParticleSystemQuad = 0x1250fa50 | Tag = -1>",
    "<CCParticleSystemQuad = 0x9108840 | Tag = -1>",
    "<CCParticleSystemQuad = 0x9152b70 | Tag = -1>",
    "<CCParticleSystemQuad = 0x914fb80 | Tag = -1>",
    "<CCParticleSystemQuad = 0x9135470 | Tag = -1>"
)

The second time I have this :

state of myArray : (
)

So, as you can see my CCParticleSystemQuad have been removed. But, when I check in Instruments (allocations) they are still living (I have 20 still living [CCParticleSystemQuad allocMemory]) and still using memory for nothing. What am I missing ? BTW I use ARC. I tried with (NSString *) object and it works fine... Thx.

Niknolty
  • 93
  • 9

2 Answers2

0
[myArray removeLastObject:cont];

You're trying to delete an int from the array. You wanted to use removeLastObject (no params) or removeObjectAtIndex:

Your problem may be that you're not calling removeChild:particle

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
0

You must remove it from its parent not only from array.

 [part removeFromParentAndCleanup:YES];

//to remove object from array

for (int i = 0; i < [myArray count]; i++) {
        [myArray removeObjectAtIndex:i];
}

//remove only last object

        [myArray removeObjectAtIndex:([myArray count]-1)];
Guru
  • 21,652
  • 10
  • 63
  • 102
  • It does not work still have my objects living. Why do I have to call removeChild if I did not call addChild ? So, if I understand well, I always have to use [self addChild:part] and [self removeChild:part] when I want to add and remove a CCParticleSystemQuad from memory? What is this working with NSString * object? Cause, I did not have to addChild my NSString to remove it from memory... – Niknolty Jun 02 '13 at 08:49
  • why do u create cocos2d particle object if not added to any layer/node ? I thought u added..so suggested to remove. Ok LearnCocos2d already mentioned your mistake about adding int in to array..Happy coding. – Guru Jun 02 '13 at 09:11
  • Thx for you advice. But, why is it a mistake to stock it in an array? – Niknolty Jun 02 '13 at 18:41
  • I mean if it is not displayed in screen then why you create particle system? you told not used addChild, it means not displayed anywhere in screen :) – Guru Jun 02 '13 at 18:47
  • yeah, in fact, I create a lot of particle in a preload scene, because if I create them in my game scene the FPS dip down... When the user touches the screen a particle appears and I do not know how many times the users will tap on the screen. It could be 20, 100, 220 etc... – Niknolty Jun 02 '13 at 19:13
  • Thank you again, I close this post – Niknolty Jun 02 '13 at 19:47