我真的不明白你在问什么。我会尽力帮助你了解我所理解的。
当您想移除贝壳时,只需将它们设置为不可见即可。一直调用addChild:和removeChild:会导致您的设备哭泣。
相反,在您的游戏开始时,根据您的需要创建一个固定数量(可能是 15 - 20)并将它们存储在一个数组中。将它们全部设置为不可见。然后,每次您需要一个外壳时,获取一个未使用的外壳,将其设置为可见,然后对其应用操作。当您不再需要它时(当它停止时),只需将其设置为不可见。
如果您能详细说明一下,那么我很乐意尝试回答您的其余问题:)
编辑:
在您的 .h 文件中,创建一个名为 shells 的 NSMutableArray。初始化它。创建一个 int (bulletIndex = 0) 然后在您的应用程序开始时调用:
- (void)createBullets {
for (int i = 0; i < 15; i++) {
CCSprite *shell = [CCSprite spriteWithFile:@"shell.png"]; //Create a sprite
shell.visible = NO; //Set it invisible for now
[self addChild:shell]; //Add it to the scene
[shells addObject:shell]; //Add it to our array
}
}
现在你的 touchesEnded: 方法:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self schedule:@selector(shootingTimer:) interval:1.0f]; //This schedules a timer that fires the shootingTimer: method every second. Change to fit your app
}
我们上面安排的方法:
- (void)shootingTimer:(ccTime)dt {
CCSprite *shell = (CCSprite*)[shells objectAtIndex:bulletIndex]; //Get the sprite from the array of sprites we created earlier.
shell.visible = YES; //Set it visible because we made it invisible earlier
//Do whatever you want to with a shell. (Run your action)
bulletIndex++; //Increment the index so we get a new shell every time this method is called
if (bulletIndex > shells.count - 1) bulletIndex = 0; //We don't want to go out of our arrays (shells) bounds so we reset bulletIndex to zero if we go over
}
然后,如果你想在 touchesBegan 上关闭它:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self unschedule:@selector(shootingTimer:)];
}
最后,就你的 shell 的操作而言,你会想要做这样的事情:
id shellMove = [CCMoveTo actionWithDuration: yourDuration position: yourPosition]; //Move it to wherever you want
id shellEnd = [CCCallFuncN actionWithTarget: self selector:@selector(endShell:)]; //This will call our custom method after it has finished moving
id seq = [CCSequence actions:shellMove, shellEnd, nil]; //Create a sequence of our actions
[shell runAction:seq]; //Run the sequence action
endShell 方法可能如下所示:
- (void)endShell:(id)sender {
CCSprite *sprite = (CCSprite*)sender;
sprite.visible = NO;
}
希望这可以解释您的大部分问题(如果不是全部的话)。如果您需要进一步的帮助,请告诉我。