現在、CCSpriteをシーン(アニメーション化されたゴースト)に追加し、ランダムに生成されたポイントから別のランダムに生成されたポイントに移動し、フェードアウトしてからシーンから削除するゲームに取り組んでいます。プレイヤーがゴーストをタップすると、スコアに1が追加されます。
すべてがうまく機能していますが、条件が満たされるまで(具体的には、スコアが50に達したとき)、このアクションを繰り返してほしいと思います。私はこれを行うための最良の方法を理解することはできません。ループを実行しようとするたびに、ループが無限に繰り返され、ゲームがクラッシュします。
そのようなループを達成するための最良の方法は何ですか?スプライトの配列を作成するという話を聞いたことがありますが、シーン上で一度にゴーストが繰り返されるのは1回だけです。これを達成する他の方法はありますか?
理想的には、私が達成したいのは、アクションを実行し、アクションが終了するのを待ってから、スコアが50になるまでアクションを再度実行することです。
これが私のコードです:
-(void) ghostSpawner {
CGPoint endPoint = [self randomPointGenerator];
[self birthGhost];
CCSprite * Ghost = (CCSprite *) [self getChildByTag:2];
//...then fade him in
CCFiniteTimeAction *ghostBegin = [CCSequence actions:[CCDelayTime actionWithDuration:1],[CCTintTo actionWithDuration:0 red:0 green:0 blue:0],[CCTintTo actionWithDuration:.5 red:255 green:255 blue:255], nil];
//...move him over a given amount of time, then fade him out and DIE
CCFiniteTimeAction *ghostEnd = [CCSequence actions:[CCMoveTo actionWithDuration:2 position:endPoint],[CCTintTo actionWithDuration:1 red:0 green:0 blue:0],[CCCallFunc actionWithTarget:self selector:@selector(killGhost)], nil] ;
[Ghost runAction:[CCRepeatForever actionWithAction:[CCSequence actions:ghostBegin,ghostEnd, nil]]];
}
スプライトを生成するには:
-(void) birthGhost {
CGPoint spawnPoint = [self randomPointGenerator];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ghostidle.plist"];
CCSprite * Ghost = [CCSprite spriteWithSpriteFrameName:@"Ghost0001.png"];
[Ghost setColor:ccc3(0,0,0)];
Ghost.anchorPoint = ccp(.5, .5);
Ghost.position = spawnPoint;
Ghost.anchorPoint = ccp(0.5f,0.5f);
Ghost.scale = .4;
Ghost.tag = 2;
[Ghost runAction:ghostIdleAnimation()];
[self addChild:Ghost z:0];
}
'ghostSpawner'関数は、シーンの'init'で呼び出されます
[self ghostSpawner];
これで十分だと思います。これは私の最初の本当のiPhoneプロジェクトの1つなので、どんなフィードバックも素晴らしいでしょう。前もって感謝します!