我尝试删除一个带有延迟时间的目标,我的代码如下
-(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 delayToDelete code:@selector(DelayToDelete) delay:0.3;',只要使用self scheduleOnce:@selector(Selector)delay:0.3;,它就会运行良好,那么这段代码有什么问题呢?谢谢
发布于 2014-03-21 06:33:17
将target添加到targetsToRemove时,会将对_target中对象的引用添加到targetsToRemove。因此,当_target被释放时,添加的targetsToRemove对象也会被释放
试试[targetsToRemove addObject:[target Copy]];
如果这起作用,那么_target正在被释放。
https://stackoverflow.com/questions/22529471
复制相似问题