objective c - Removing Sprites From NSMutable Array -


i working on cocos2d project , want work array of sprites.

i create array:

nsmutablearray *ssprites; 

then add stuff in init method:

ccsprite *obssprite = [ccsprite spritewithfile:@"/users/desktop/programs/physics test/physics test/resources/icon-small@2x.png"];               obssprite.position=ccp(position,5);              [self addchild: obssprite];              [ssprites addobject: obssprite]; 

then later want remove sprites:

for( int i=0; i<[ssprites count];i++) {     ccsprite *spr = (ccsprite *) [ssprites objectatindex:i];         if(yes) {    //this test, actual program uses actual condition              [spr removefromparentandcleanup: yes];             [ssprites removeobjectatindex:i];         }   } 

but reason sprites staying on screen. how should fix code remove sprites?

as joe mentioned in comment looping through array of sprites , removing them in loop itsels. so, first time remove object @ index 0 , increment i 1, "points" object @ index 2 in original array (the 1 @ index 1 before @ index 0). seems me removing every second object?

regardless of issue, never idea delete elements array looping through. need array @ all? if used ccnode's children array (from code seems every sprite added parent , own array of sprites).


you can see in action simple code

nsmutablearray *ssprites = [nsmutablearray arraywithobjects:@"obj1", @"obj2", @"obj3", @"obj4", @"obj5", nil]; for(int = 0; < [ssprites count]; i++)  {     nsstring *spr = (nsstring *) [ssprites objectatindex:i];      nslog(@"removing %@", spr);      [ssprites removeobjectatindex:i]; } 

i suggest change code this

nsmutablearray *spritestoremove = [[nsmutablearray alloc] init];  (ccsprite *spr in ssprites) {     if (yes) // condition goes here     {         // remove sprite parent here.         [spr removefromparentandcleanup:yes];         // however, leave "spr" in ssprites array - "mark" deletion.         [spritestoremove addobject:spr];     } }  [ssprites removeobjectsinarray:spritestoremove]; 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -