1

ここで、敵にスプライトを追加するためのいくつかのコード.....

_robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];
    for (int i = 0; i < kNumAstroids; ++i) {
        CCSprite *asteroid = [CCSprite spriteWithSpriteFrameName:@"robber.png"];
        asteroid.visible = NO;
        [_batchNode addChild:asteroid];
        [_robbers addObject:asteroid];
   }

そしてUpdateメソッドで.......

    double curTime = CACurrentMediaTime();
if (curTime > _nextRunemanSpawn) {
    float randSecs = [self randomValueBetween:0.20 andValue:1.0];
    _nextRunemanSpawn = randSecs + curTime;

    float randY = [self randomValueBetween:80 andValue:80];
    float randDuration = [self randomValueBetween:4.5 andValue:4.5];
    float randDuration1 = [self randomValueBetween:1.0 andValue:1.0];

    CCSprite *asteroid = [_robbers objectAtIndex:_nextRobber];
    _nextRobber++;

    if (_nextRobber >= _robbers.count) {
        _nextRobber = 1;
    }
    [asteroid stopAllActions];
    asteroid.position = ccp(winSize.width +asteroid.contentSize.width / 2 , randY);
    asteroid.visible = YES;

    [asteroid runAction:[CCSequence actions:[CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width-asteroid.contentSize.width, 0)],
                         [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],nil]];


すべてのスプライトは、スプライトが画面の中央を横切ると画面の右から左に移動し、自動的に消えます
。この問題の理由は何ですか??

4

1 に答える 1

0

LearnCocos2D の前述の声明に同意します。複数のオブジェクトを追加していて、ラインの容量の終わりに達している可能性もあります

_robbers = [[CCArray alloc] initWithCapacity:kNumAstroids];

kNumAsteroids に指定した数よりも多くのオブジェクトをスポーンしようとすると、最も古いオブジェクトが削除され、代わりに新しいオブジェクトが使用されます。つまり、kNumAsteroids が 5 の場合、画面上に 5 があり、6 番目を追加すると、1 は 6 になり、その位置は設定したものになります。

于 2013-03-03T18:18:15.807 に答える