0

私はiPhone用のCocos2dプログラミングを学び始めたばかりで、Cocos2dでの模倣の撮影について質問があります。私はスプライトを持っています。それを別の場所にドラッグしてドロップするとき、シェルという名前の別の小さなスプライトを使用して、一方向に永続的に撮影を開始したいと思います。したがって、-ccTouchesEndedで2ステップのループを永遠に繰り返す必要があります。

1.シェルがスプライトから離れ始めます(射撃の模倣)(CCMoveTo);

2.シェルが停止すると(移動範囲が制限されます)、シェルが消えるか、非表示になります(removeChild:、visible:または何?)

そして、それは永遠に繰り返される必要があります(CCRepeatForever actionWithAction [CCSequenceactions:];

したがって、これら2つのアクションの永遠のサイクルを設定するための支援が必要です。

ありがとう、アレックス。

4

1 に答える 1

1

私はあなたが何を求めているのか本当にわかりません。私が理解していることであなたを助けようとします。

シェルを削除する場合は、非表示に設定するだけです。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;
}

うまくいけば、これはあなたの問題のすべてではないにしてもほとんどを説明します。さらにサポートが必要な場合はお知らせください。

于 2012-06-17T22:03:55.883 に答える