Question

I am new in cocos2d. I have a problem when we create new sprite object. It's not remove onto the display. Sprite object are not delete when we add new lives.(add heart sprite).

//Here I create a live which in heart shape.

    -(id) init {
        if( (self=[super init]) ) {

     hearthArray = [[NSMutableArray alloc] init];
            lives = 4;

            for(NSInteger ilive = 0; ilive<lives; ilive++){
                CCSprite *hearth = [CCSprite spriteWithFile:@"hearth.png"];
                hearth.position = ccp( ((ilive+1)*50), winSize.height - 50);
                [hearthArray insertObject:hearth atIndex:ilive];
                [self addChild:hearth];
            }

           return self;
    }

//Below code into remove the heart.(decrease lives).

- (void) addMonster:(ccTime)dt {

        //select a random monster from the _monsters Array
        int selectedMonster = arc4random() % [_monsters count];

        Monster *monster = [_monsters objectAtIndex:selectedMonster];
        int m = [monster movement];

        CCSprite *spriteMonster = [[CCSprite alloc] initWithFile:[monster monsterSprite]];
        spriteMonster.tag = [monster tag];

        CGSize winSize = [CCDirector sharedDirector].winSize;
        int minX = spriteMonster.contentSize.width / 2;
        int maxX = winSize.width - spriteMonster.contentSize.width/2;
        int rangeX = maxX - minX;
        int actualY = (arc4random() % rangeX) + minX;

        //BLOCK 2 - Determine speed of the monster
        int minDuration = [monster minVelocity];
        int maxDuration = [monster maxVelocity];
        int rangeDuration = maxDuration - minDuration;
        int actualDuration = (arc4random() % rangeDuration) + minDuration;

    if(m == 1){

       spriteMonster.position = ccp( actualY,winSize.height + spriteMonster.contentSize.height/2);
            [self addChild:spriteMonster];

            //BLOCK 4 - Create the actions
            CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp( actualY,-spriteMonster.contentSize.height/2)];

            CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
                [_monstersOnScreen removeObject:node];
                [node removeFromParentAndCleanup:YES];

                // Remove lifes
                lives--;

               // [[hearthArray lastObject] removeFromParentAndCleanup:YES];

                [self removeChild:[hearthArray lastObject] cleanup:YES];
                 [hearthArray removeLastObject];

                NSLog(@"m=1 when array : %@",hearthArray);

                if(lives == 0)
                    [[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
            }];

            [spriteMonster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

            [_monstersOnScreen addObject:spriteMonster];
        }

    }

//Below into Add new lives using for loop.when touch particular object.

-(void)increaseLivesWhentouchCoin{

      NSLog(@"lives is get when add live : %i",lives);

    NSLog(@"hearthArray when toch coin: %@",hearthArray);

     lives = lives+1;
    NSLog(@"lives+1 : %i",lives);

    for(NSInteger i = 0; i<lives; i++){
        hearth = [CCSprite spriteWithFile:@"hearth.png"];
        CGSize winSize = [CCDirector sharedDirector].winSize;
        hearth.position = ccp( ((i+1)*50), winSize.height-50);
        [hearthArray insertObject:hearth atIndex:i];
        [self addChild:hearth];
    }
    NSLog(@"hearthArray out for loop: %@",hearthArray);
}

Please help me.Thanks in advance.

Was it helpful?

Solution

Your increaseliveswhentouch coin method should be like this..

-(void)increaseLivesWhentouchCoin{
    CCSprite *hearth = [CCSprite spriteWithFile:@"hearth.png"];
    CGSize winSize = [CCDirector sharedDirector].winSize;
    hearth.position = ccp( ((lives+1)*50), winSize.height-50);
    [hearthArray insertObject:hearth atIndex:lives];
    [self addChild:hearth];
    lives++;
}

You have to add just one heart object .There is no need to create all objects again,if you want to create remove all the objects first..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top