简体   繁体   English

精灵会在一段时间后消失

[英]Sprites are disappear after some time

Here some code for Add sprite for Enemies..... 这里有一些为敌人添加精灵的代码.....

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

And in Update method ........ 并在更新方法中........

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

All Sprites are Move from right to left in screen 屏幕上所有精灵都从右移到左
when Sprite crosses the middle of the screen it automatically disappear 当Sprite越过屏幕中间时,它会自动消失
what is the reason for this problem ?? 这个问题的原因是什么?

I agree with the previously mentioned statement from LearnCocos2D. 我同意LearnCocos2D的上述声明。 It is also possible that you are adding multiple objects and reaching the end of the capacity in the line 也有可能添加多个对象并达到行中容量的末尾

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

If you try to spawn more objects than whatever number you have specified for kNumAsteroids it will remove the oldest object and use the new one in its place. 如果您尝试生成的物体数量多于为kNumAsteroids指定的数量,它将删除最旧的物体并在其位置使用新的物体。 Ie if kNumAsteroids is 5, you have 5 on the screen and then add a sixth, 1 will become 6 and its position will be whatever you set it to. 即,如果kNumAsteroids为5,则屏幕上有5,然后加上第六,则1将变为6,其位置将与您设置的位置相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM