暂停SpriteKit中的计时器

时间:2014-08-29 18:20:26

标签: ios objective-c iphone timer sprite-kit

嘿,伙计们在这里自学新手。所以我试图在SpriteKit中暂停一个游戏,虽然暂停有点起作用但它并没有暂停我的计时器。现在我知道你不能在SpriteKit中使用NSTimer,因为它在暂停状态下效果不佳所以我使用SKScene update:方法并使用传入的currentTime参数来跟踪时间。但是,当我暂停游戏时,计时器不会暂停。谁在那里为此工作?下面是计时器和暂停的代码。

-(void)update:(NSTimeInterval)currentTime
{
    if (self.startGame){
        self.startTime = currentTime;
        self.startGame = NO;
    }
int countDownInt = 10.0 - (int)(currentTime - self.startTime);

if (self.gameStartTimer) {
    if (countDownInt>0){
        self.timerLabel.text = [NSString stringWithFormat:@"%i", countDownInt];
    }else if (countDownInt == 0) {
        self.timerLabel.text = [NSString stringWithFormat:@"%@", @"TIME"];
        LoseConditionScene *loseScene = [LoseConditionScene sceneWithSize:self.frame.size];
        SKTransition *transition = [SKTransition fadeWithDuration:1.0];
        [self.view presentScene:loseScene transition:transition];
    }
}

}

-(void)pauseGame {
    _isGamePaused = YES; //Set pause flag to true
    self.scene.view.paused = YES; //Pause scene and physics simulation

-(void)unpauseGame {
    _isGamePaused = NO; //Set pause flag to false
    self.scene.view.paused = NO;

-(void)registerAppTransitionObservers
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:NULL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:NULL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:NULL];

}

-(void)applicationWillResignActive
{
    if (_isGamePaused) { //Pause the game is necessary
        [self pauseGame];
    }
}
-(void)applicationDidEnterBackground
{
    self.scene.view.paused = YES;
}

-(void)applicationWillEnterForeground
{
    self.scene.view.paused = NO; //Unpause the SKView
    if (_isGamePaused) {
        self.paused = YES;
    }
}

0 个答案:

没有答案