Jems Leo
Jems Leo

Reputation: 39

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
what is the reason for this problem ??

Upvotes: 1

Views: 112

Answers (1)

Brandon Lassiter
Brandon Lassiter

Reputation: 306

I agree with the previously mentioned statement from 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. I.e. 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.

Upvotes: 0

Related Questions