scheduleOnce在cocos2d-iphone崩溃

时间:2014-03-20 10:05:08

标签: ios objective-c cocos2d-iphone

我尝试删除具有延迟时间的目标,我这样编码

-(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],它会运行良好,那么这段代码有什么问题?感谢

1 个答案:

答案 0 :(得分:0)

target添加到targetsToRemove后,_target中对象的引用将添加到targetsToRemove。因此,当_target被解除分配时,您添加的对象targetsToRemove

尝试[targetsToRemove addObject:[target Copy]];

如果有效,那么_target将被取消分配。