2014-10-10 59 views
0

有没有更有效的方法来清理我的CCNodes?我正在调用这个函数(和其他人喜欢它的不同的游戏对象),在一个计时器。有没有更有效的方法来清理我的CCNodes?

- (void)pulseBullets:(NSMutableArray *)bs targets:(NSArray *)targets { 
    for (Bullet *b in bs) { 
     for (QuantumPilot *p in targets) { 
      if (p.active) { 
       [p processBullet:b]; 
       if (!p.active) { 
        [self processKill:p]; 
       } 
      } 
     } 
    } 

    NSMutableArray *bulletsToErase = [NSMutableArray array]; 
    for (Bullet *b in bs) { 
     [b pulse]; 
     if ([self bulletOutOfBounds:b]) { 
      [bulletsToErase addObject:b]; 
     } 
    } 

    for (Bullet *b in bulletsToErase) { 
     [b removeFromParentAndCleanup:YES]; 
    } 

    [bs removeObjectsInArray:bulletsToErase]; 
} 
+2

“高效”的含义究竟是什么?较少的代码?内存使用量较少?更快的代码?更容易维护?这部分代码您认为效率低下,所有这一切? – LearnCocos2D 2014-10-11 09:26:24

+0

所有这些将是“更好”。更快,更少的内存使用。 – quantumpotato 2014-10-13 23:12:48

+0

更好地解决一个已知的性能问题,可以衡量并归因于这些特定的代码行? – YvesLeBorg 2014-10-15 11:14:51

回答

0

好吧,但我没有对性能做任何“声明”,您将不得不为自己衡量这一点。如果以相反顺序迭代可变数组,迭代过程中删除对象是安全的,因为迭代器不会因删除而失效。所以,你可以得到子弹摆脱altogther删除阵列,像这样:

for (Bullet *b in [bs reverseObjectEnumerator]) { // *** do not change iteration order *** 
    for (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]; 
    } 
} 

这是简单的,但混淆改变迭代过程中的数组内容的固有风险。你打电话询问它是否“更清洁”。另外,反转迭代器的'成本'可能高于你保存的成本,因为我说你必须测量它。

+0

我喜欢它。谢谢 – quantumpotato 2014-10-16 20:35:26

相关问题