添加第二个计时器iOS游戏精灵套件

时间:2014-10-02 02:33:34

标签: ios sprite-kit nstimer

每次敌人和玩家节点联系时,我都会尝试向倒数计时器添加几秒钟。      此外,当玩家达到某个特定点时,我希望玩家转到另一个等级。我发送部分代码。任何人都可以帮我解决这个问题。顺便说一句,因为我是编程新手,我的代码有点难以阅读。对不起。 谢谢

        -(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 个答案:

答案 0 :(得分:0)

创建一个布尔标志,update方法检查它是否应该添加秒。

添加以下变量并最初设置self.addSeconds = NO;

@property (nonatomic) BOOL addSeconds;

在玩家击中敌人的方法中设置self.addSeconds =YES;

将以下内容添加到更新的开头:

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

这应该可以解决问题。