0
votes

I am trying to add a few seconds to countdown timer each time when enemy and player node contacts. Also when the player reach to some certain point I want player to pass to another level. I am sending part of the code . Could anybody help me to solve this problem. By the way because I am new to programming my code is a little hard to read . Sorry for that. Thanks

        -(void)update:(CFTimeInterval)currentTime{

   if (startGamePlay){
    startTime = currentTime;
    startGamePlay = NO;
}

 countDownInt = 10.0 - (int)(currentTime-startTime);

 if(countDownInt > 0 ){  //if counting down to 0 show counter
    countDown.text = [NSString stringWithFormat:@"%i", countDownInt];
}


  else if (countDownInt == 0){
        countDown.text =@"0";
        Level2 *level2  = [Level2 sceneWithSize:self.frame.size];
        SKTransition *transition = [SKTransition fadeWithDuration:0.5];
        [self.view presentScene:level2  transition:transition];
    }
}



    - (void) didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;

if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
    firstBody = contact.bodyA;
    secondBody = contact.bodyB;
} else {
    firstBody = contact.bodyB;
    secondBody = contact.bodyA;
}


if ( firstBody.categoryBitMask == CollisionCategoryBrick && secondBody.categoryBitMask == CollisionCategoryShark ) {
    NSLog(@"BRICK");

  [contact.bodyB.node removeFromParent];

} else if ( firstBody.categoryBitMask == CollisionCategoryWaterBall && secondBody.categoryBitMask == CollisionCategoryShark ) {
    NSLog(@"BALL");
    countDownInt = [countDown.text intValue];
    [contact.bodyA.node removeFromParent];
    [self addPoints:PointsPerHit];

   if (![ self childNodeWithName:@"WATERBALLTL"]) {
        [self addWaterBallTopLeft];
    }
    if (![ self childNodeWithName:@"WATERBALLTR"]) {
        [self addWaterBallTopRight];
    }
    if (![ self childNodeWithName:@"WATERBALLBL"]) {
        [self addWaterBallBottomLeft];
    }
    if (![ self childNodeWithName:@"WATERBALLBR"]) {
        [self addWaterBallBottomRight];
    }
 }
  NSLog(@"%lu", (unsigned long)[self.children count]);
}





 #import "HudNode.h"

  @implementation HudNode

 {
    SKLabelNode *countDown;
    BOOL startGamePlay;
    NSTimeInterval startTime;
 }

  + (instancetype) hudAtPosition:(CGPoint)position inFrame:(CGRect)frame {
  HudNode *hud = [self node];
  hud.position = position;
  hud.zPosition = 10;
  hud.name = @"HUD";



   SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura-CondensedExtraBold"];
     scoreLabel.name = @"Score";
    scoreLabel.text = @"0";
scoreLabel.fontSize = 24;
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
scoreLabel.position = CGPointMake(frame.size.width-20, -10);
scoreLabel.zPosition = 12;
[hud addChild:scoreLabel];
        SKLabelNode *countDown = [SKLabelNode labelNodeWithFontNamed:@"Futura-Medium"];
        countDown.fontSize = 50;
        countDown.position = CGPointMake(30, -10);
        countDown.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter; //good for     positioning with other sprites
        countDown.fontColor = [SKColor whiteColor ];
        countDown.name = @"countDown";
        countDown.zPosition = 100;
        [hud addChild:countDown];


return  hud;
 }

   - (void) addPoints:(NSInteger)points {
self.score += points;

SKLabelNode *scoreLabel = (SKLabelNode *) [self childNodeWithName:@"Score"];
scoreLabel.text = [NSString stringWithFormat:@"Score:%li", (long)self.score];

}
1

1 Answers

0
votes

Create a boolean flag that the update method checks to see if it should add seconds.

Add the following variable and set self.addSeconds = NO; initially:

@property (nonatomic) BOOL addSeconds;

Set self.addSeconds =YES; in the method where your player hits your enemy:

Add the following to the very beginning of the Update:

if(self.addSeconds == YES)
{
    CountDownInt+=  5 //Change this to add as many seconds as you want
    self.addSeconds=NO;
}

This should do the trick.