[英]scheduleOnce crash in cocos2d-iphone
I try to delete a target with delay time, and I code like this 我尝试删除具有延迟时间的目标,并且我这样编码
-(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];
}
the delay delete code is 延迟删除代码是
-(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");
}
}
}
if I don't use '[self scheduleOnce:@selector(delayToDelete) delay:0.3];', just use [self delayToDelete], it will run well, so what the problem with this code? 如果我不使用'[self scheduleOnce:@selector(delayToDelete)delay:0.3];',只需使用[self delayToDelete],它将运行良好,那么这段代码有什么问题呢? Thanks
谢谢
When you add target
to targetsToRemove
, a reference to an object in _target
is being added to targetsToRemove
. 当您将
target
添加到targetsToRemove
,对_target
的对象的引用将被添加到targetsToRemove
。 So when _target
is deallocated so is the object you have added targetsToRemove
因此,当
_target
被释放时,您添加的对象也是如此targetsToRemove
Try [targetsToRemove addObject:[target Copy]];
尝试
[targetsToRemove addObject:[target Copy]];
If that works then _target
is being deallocated. 如果
_target
,则_target
被释放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.