Objective c 有没有更有效的方法来清理我的CCNodes?
有没有更有效的方法来清理我的CCNodes?我在定时器上调用这个函数(以及其他类似于不同游戏对象的函数)Objective c 有没有更有效的方法来清理我的CCNodes?,objective-c,cocos2d-iphone,nsmutablearray,Objective C,Cocos2d Iphone,Nsmutablearray,有没有更有效的方法来清理我的CCNodes?我在定时器上调用这个函数(以及其他类似于不同游戏对象的函数) - (void)pulseBullets:(NSMutableArray *)bs targets:(NSArray *)targets { for (Bullet *b in bs) { for (QuantumPilot *p in targets) { if (p.active) { [p processB
- (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];
}
好的,但我没有对绩效做出“声明”,你必须自己衡量。如果以相反顺序迭代可变数组,则在迭代期间删除对象是安全的,因为迭代器不会因删除而失效。所以你可以去掉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];
}
}
这更简单,但混淆了在迭代过程中更改数组内容的固有风险。你打电话询问它是否“干净”。此外,可能反转迭代器的“成本”高于您所节省的成本,正如我所说,您必须对其进行测量。“高效”的确切含义是什么?更少的代码?更少的内存使用?更快的代码?更容易维护?你认为这个代码的哪一部分效率低,所有这些都是“更好”的。更快,更少的内存使用。更好的方法是“解决一个已知的性能问题,它可以被测量并归因于这些特定的代码行?”在我看来,代码效率很低。我每次都会创建一个新数组,从中删除对象,然后销毁该数组。有没有更有效的解决方案?