Search code examples
objective-ccocos2d-iphone2d-games

If Else satement working but not updating cocos2d


i have tested my code by changing the starting lives vaule, the problem is it doesn't remove them as the statement becomes valid, How do i fix this? I have tried placing it in my .m file but it doesn't seem to work properly anywhere, any ideas on where it would go? I would post the .m but it is about 500 lines so it is a bit big so i just pasted the relevant bit of it. also i am a 15 year old, and i am fairly new to cocos2d development The Code

  - (void) addMonster {
  CCSprite * monster = [CCSprite spriteWithFile:@"startH.png"];

// Determine where to spawn the monster along the Y axis
CGSize winSize = [CCDirector sharedDirector].winSize;
int minY = monster.contentSize.height / 2;
int maxY = winSize.height - monster.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(winSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster];


// Determine speed of the monster}
if (Strategyscore < 10) {
    int minDuration = 5.0;
    int maxDuration = 10.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;
    eate the actions
    CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
                                                position:ccp(-monster.contentSize.width/2, actualY)];
    CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
        [node removeFromParentAndCleanup:YES];
        [_monsters removeObject:node];
        Life--;

        CCSprite *Life3 = [CCSprite spriteWithFile:@"heart.png"];
        Life3.position = ccp(210,200);
        CCSprite *Life2 = [CCSprite spriteWithFile:@"heart.png"];
        Life2.position = ccp(220,200);
        CCSprite *Life1 = [CCSprite spriteWithFile:@"heart.png"];
        Life1.position = ccp(230,200);
        [self addChild:Life3];
        [self addChild:Life2];
        [self addChild:Life1];
        if(Life == 2) {
            [self removeChild:Life3];
        }
        else if(Life == 1) {
            [self removeChild:Life2];
            [self removeChild:Life3];
        }
        else if(Life <= 0) {
            [self removeChild:Life1];
            [self removeChild:Life2];
            [self removeChild:Life3];


    // Cr [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
        }
    }];
    [monster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
    //collision stuff
    monster.tag = 1;
    [_monsters addObject:monster];
}

Also the .h file

int StrategyBullet;
int Strategyscore;
int high;
int Life;

CCLabelTTF *highlabel;
CCLabelTTF *StrategyBulletLabel;
CCLabelTTF *StrategyscoreLabel;
@interface Strategy: CCLayer
{
NSMutableArray * _monsters;
NSMutableArray * _projectiles;
int _monstersDestroyed;

}


+(CCScene *) scene;

@end

Solution

  • Every time you add a new monster, you add a new set of sprites Life1,Life2, and Life3, superimposed on the previous ones. You probably want to have a single set of life hearts.

    in .h

    CCSprite *Life1,*Life2,*Life3;
    

    in .m, init method

        Life3 = [CCSprite spriteWithFile:@"heart.png"];
        Life3.position = ccp(210,200);
        Life2 = [CCSprite spriteWithFile:@"heart.png"];
        Life2.position = ccp(220,200);
        Life1 = [CCSprite spriteWithFile:@"heart.png"];
        Life1.position = ccp(230,200);
    
        [self addChild:Life1];
        [self addChild:Life2];
        [self addChild:Life3];
    

    and in your actionMoveDone call block, dont remove them, just make them not visible

    CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
    
        [node removeFromParentAndCleanup:YES];
        [_monsters removeObject:node];
        Life--;
    
    
        if(Life == 2) {
            Life3.visible=NO;
        }
        else if(Life == 1) {
            Life3.visible=NO;
            Life2.visible=NO;
        }
        else if(Life <= 0) {
            Life3.visible=NO;
            Life2.visible=NO;
            Life1.visible=NO;
        }
    }];
    

    for starters. I just made this as 'like your coding style' as possible, but eventually you will find different patterns to do this as you game becomes more complex. Read about normal iOS code and naming conventions, it will help you and also make your code samples more palatable for the people trying to help you here.