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];
   }

在更新方法中......

    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,然后添加第六个,1 将变为 6,其位置将是您设置的任何位置。

于 2013-03-03T18:18:15.807 回答