在我解释我的问题之前,我对我的项目如何运作做了这个非常不简洁的说明 -
我的问题是 - 当我要检查损坏的时间到了时,我想从数组中删除该对象!我曾尝试在 checkForDamage 中删除它,但由于它是用 ccTime
调用的,它只会删除每个对象(当我使用 removeObjectAtIndex:0
删除第一个对象时) .我不能将它放在 stopCheckDamage
中,因为在检查第一个炸弹的损坏时,玩家还不如放下另一个炸弹。
当用户被击中时,checkForDamage
工作正常,我中断;
它并调用 stopCheckDamage。我的问题是当用户没有被击中时,因为不存在的 Sprite 会留在数组中并且只会把东西弄乱。我一直在考虑我所知道的每一种方法,但我似乎无法找到一种方法来在延迟三秒后删除特定对象,如果玩家没有被击中的话。
相关代码我也做了一个pastebin,你可以找here
最佳答案
这只是一个想法,
您有一个包含所有对象的数组。您只需要知道要删除哪一个。
那么,为什么不给每个对象一个被添加到数组中的 tag
。当您去删除该对象时,测试它的标签并将其删除。
//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];
}
希望对您有所帮助。 :)
关于ios - 从计划中的数组中删除适当的 CCSprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16744109/