我尝试删除具有延迟时间的目标,我这样编码
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
targetsToRemove = [[NSMutableArray array] init];
for (CCSprite *target in _targets) {// here _targets is NSMutableArray
if (CGRectContainsPoint(target.boundingBox, location)) {
[targetsToRemove addObject:target];
}
}
[self scheduleOnce:@selector(delayToDelete) delay:0.3];
}
延迟删除代码是
-(void)delayToDelete
{
for (CCSprite *target in targetsToRemove) { //it will crash at this line, when I run
if (target.tag == 1) {
CCLOG(@"do something");
}
else {
CCLOG(@"do nothing");
}
}
}
如果我不使用'[self scheduleOnce:@selector(delayToDelete)delay:0.3];',只需使用[self delayToDelete],它会运行良好,那么这段代码有什么问题?感谢
答案 0 :(得分:0)
将target
添加到targetsToRemove
后,_target
中对象的引用将添加到targetsToRemove
。因此,当_target
被解除分配时,您添加的对象targetsToRemove
尝试[targetsToRemove addObject:[target Copy]];
如果有效,那么_target
将被取消分配。