Ios 从计划中的数组中删除适当的CCSprite

Ios 从计划中的数组中删除适当的CCSprite,ios,objective-c,cocos2d-iphone,Ios,Objective C,Cocos2d Iphone,在我解释我的问题之前,我对我的项目如何运作做了一个非常不简洁的说明- 我的问题是-当我想检查伤害的时间到了,我想从数组中删除该对象!我曾尝试在checkForDamage中删除它,但当使用ccTime调用它时,它只会删除每个对象(当我使用removeObjectAtIndex:0删除第一个对象时)。我不能把它放在stopCheckDamage中,因为在检查第一颗的伤害时,玩家可能已经放下了另一颗炸弹 当用户被击中时,checkForDamage工作正常,Ibreak它并调用stopCheckD

在我解释我的问题之前,我对我的项目如何运作做了一个非常不简洁的说明-

我的问题是-当我想检查伤害的时间到了,我想从数组中删除该对象!我曾尝试在checkForDamage中删除它,但当使用
ccTime
调用它时,它只会删除每个对象(当我使用
removeObjectAtIndex:0
删除第一个对象时)。我不能把它放在stopCheckDamage中,因为在检查第一颗的伤害时,玩家可能已经放下了另一颗炸弹

当用户被击中时,
checkForDamage
工作正常,I
break它并调用stopCheckDamage。我的问题是当用户没有被击中时,因为那时不存在的精灵留在数组中,只是把东西搞砸了。我一直在考虑我所知道的每一种方法,如果玩家没有被击中,我似乎找不到一种方法在延迟三秒钟后删除一个特定的对象

我还为相关代码制作了一个粘贴箱,您可以找到它

这只是一个想法

您有一个包含所有对象的数组。你只需要知道删除哪一个。 那么,为什么不给每个对象一个添加到数组中的
标记呢。当您要删除该对象时,请测试其标记并将其删除

//Say your array has 10  objects in it, 
//There will be 10 objects each with a tag 1-10.
//When you want to delete an object, 
编辑

//Before you add each object to the array, use a `for` loop

for (int i = 0; i < theMaxNumberOfTagsYouWant; i++)

{
self.myObject.tag = i;
[self.myArray addObject:self.myObject];
//This will loop thru as many times as you want, specify using the 
//maxNumberOfTagsYouWant variable. and it will give it a tag, which'll be the value of `i` 
//which gets increased for each object you insert into the array, Then when you want to     
//remove the object, use the below code to remove the object using it's tag.
}

-(void)deleteObjectFromArray{
[self.myArray removeObjectAtIndex:myObject.tag];
}
//将每个对象添加到数组之前,请使用'for'循环
对于(int i=0;i
希望这能有所帮助。:)

这只是一个想法

您有一个包含所有对象的数组。你只需要知道删除哪一个。 那么,为什么不给每个对象一个添加到数组中的
标记呢。当您要删除该对象时,请测试其标记并将其删除

//Say your array has 10  objects in it, 
//There will be 10 objects each with a tag 1-10.
//When you want to delete an object, 
编辑

//Before you add each object to the array, use a `for` loop

for (int i = 0; i < theMaxNumberOfTagsYouWant; i++)

{
self.myObject.tag = i;
[self.myArray addObject:self.myObject];
//This will loop thru as many times as you want, specify using the 
//maxNumberOfTagsYouWant variable. and it will give it a tag, which'll be the value of `i` 
//which gets increased for each object you insert into the array, Then when you want to     
//remove the object, use the below code to remove the object using it's tag.
}

-(void)deleteObjectFromArray{
[self.myArray removeObjectAtIndex:myObject.tag];
}
//将每个对象添加到数组之前,请使用'for'循环
对于(int i=0;i

希望这能有所帮助。:)

AW4视图我的糟糕图片吓跑了所有人:(啊,你隐藏了代码,做得很好。)我看到里面有很多重复,你有没有可能先清理一下?还有格式方面。@learncos2D改进了代码,可能使其更高效,但至少去掉了可能让您畏缩的if块。多少解决了这个问题(我想),看看@aw4视图我那张糟糕的照片吓跑了所有人:(啊,你把代码藏起来了,做得很好。)我看到里面有很多重复,你有没有可能先清理一下?还有格式方面。@learncos2D改进了代码,可能使其更高效,但至少去掉了可能让您畏缩的if块。有点解决了这个问题(我想),看看@我曾想过使用标签,但我还没有找到一种方法。我找不到一种方法将其应用到我的代码中。你看过我贴的代码样本了吗?如果可能的话,看看你是否能找到一种方法来实现它!:)我曾考虑过使用标签,但我还没有找到一种方法。我找不到一种方法将其应用到我的代码中。你看过我贴的代码样本了吗?如果可能的话,看看你是否能找到一种方法来实现它!:)