私はあなたが何を求めているのか本当にわかりません。私が理解していることであなたを助けようとします。
シェルを削除する場合は、非表示に設定するだけです。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:)];
}
最後に、シェルのアクションに関する限り、次のようなことをしたいと思うでしょう。
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;
}
うまくいけば、これはあなたの問題のすべてではないにしてもほとんどを説明します。さらにサポートが必要な場合はお知らせください。