Friday 15 January 2010

objective c - Is there a more efficient way to clean up my CCNodes? -



objective c - Is there a more efficient way to clean up my CCNodes? -

is there more efficient way clean ccnodes? i'm calling function (and others different game objects), on timer.

- (void)pulsebullets:(nsmutablearray *)bs targets:(nsarray *)targets { (bullet *b in bs) { (quantumpilot *p in targets) { if (p.active) { [p processbullet:b]; if (!p.active) { [self processkill:p]; } } } } nsmutablearray *bulletstoerase = [nsmutablearray array]; (bullet *b in bs) { [b pulse]; if ([self bulletoutofbounds:b]) { [bulletstoerase addobject:b]; } } (bullet *b in bulletstoerase) { [b removefromparentandcleanup:yes]; } [bs removeobjectsinarray:bulletstoerase]; }

ok, create no 'statement' on performance, have measure yourself. if iterate mutable array in reverse order, safe delete object during iteration, because iterator not invalidated deletion. rid altogther of bullets erase array :

for (bullet *b in [bs reverseobjectenumerator]) { // *** not alter iteration order *** (quantumpilot *p in targets) { if (p.active) { [p processbullet:b]; if (!p.active) { [self processkill:p]; } } } [b pulse]; if ([self bulletoutofbounds:b]) { [b removefromparentandcleanup:yes]; [bs removeobject:b]; } }

this simpler , obfuscates inherent risk of altering array content during iteration. create phone call whether 'cleaner'. also, maybe 'cost' of reversing iterator higher save, said have measure it.

objective-c cocos2d-iphone nsmutablearray

No comments:

Post a Comment