【问题标题】:stop/start NSTimer when app goes to background当应用程序进入后台时停止/启动 NSTimer
【发布时间】:2015-07-08 20:17:07
【问题描述】:

正如标题所暗示的,我试图在我的游戏进入后台时停止我的 NSTimer,并在游戏重新开始时重新启动它。中断显然可能是接听电话等。

我在应用程序委托中使用以下代码在应用程序中断时停止并重新启动场景,但它不会停止计时器。

- (void)applicationWillResignActive:(UIApplication *)application {

    SKView *view = (SKView *)self.window.rootViewController.view;
    view.scene.paused = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {

    SKView *view = (SKView *)self.window.rootViewController.view;
    view.scene.paused = NO;
}

在我放置计时器的场景中,我已将观察者放置在-(id)initWithSize:(CGSize)size 方法中。以下是观察者:

[[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(goBackground)
         name:UIApplicationDidEnterBackgroundNotification
         object:nil];

[[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(backBackground)
         name:UIApplicationDidBecomeActiveNotification object:nil];

和选择器方法:

- (void) goBackground {
    [timer invalidate], timer = nil;
}

- (void) backBackground {
    if([timer isValid]) {
        [timer invalidate];
    }
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];

}

我也试图按如下方式释放观察者:

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

上面的代码被触发了,我在上面的所有方法上都放置了一个 NSLog。但发生的情况是,它适用于场景 1,这是第 1 级的地方,当玩家赢得第 1 级并进入第 2 级并且我再次重新创建应用程序中断场景时,它会在游戏开始前返回并运行几秒钟崩溃。

显然我的代码不起作用,我已经尝试了所有我知道如何解决这个问题的方法,但无济于事。我的计时器只是显示关卡中还剩多少时间,与输赢情况无关。(这可能是一些不需要知道的额外信息)。

当我的游戏进入后台时,我是否可以停止并重新启动计时器?我真的很感激任何帮助。

我不知道是否需要定时器代码,但如下:

- (void) gameTimerLabel {
    gameTimerLabel = [SKLabelNode labelNodeWithFontNamed:@"ArialRoundedMTBold"];
   // gameTimerLabel.text = [NSString stringWithFormat:@"%d", _lives];
    gameTimerLabel.fontSize = 20.0;
    gameTimerLabel.fontColor = [SKColor colorWithRed:135.0/255 green:207.0/255 blue:232.0/255 alpha:0.6];
    gameTimerLabel.position = gameTimerLabel.position = CGPointMake(270, 282);
    [self addChild:gameTimerLabel];
}

- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ){
        secondsLeft -- ;
        //hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        gameTimerLabel.text = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
    }
    else{
        secondsLeft = 70;
    }
}

-(void)countdownTimer{

    secondsLeft = hours = minutes = seconds = 0;
    if([timer isValid]) {
        [timer invalidate];
    }
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];

}

它基本上只是一个倒数计时器,没什么特别的。

编辑:

Ben-G,是对的。几乎没有办法在后台/前台管理 NSTimer,所以我使用了 SKAction 方法并完成了工作。对于那些感兴趣的人,这里是工作代码:

@implementation MyScene {
SKLabelNode *gameTimerLabel;
int countDownInt;
    int secondsLeft;
}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
secondsLeft = 70;
[self gameTimerLabel];
}
    return self;
}

- (void) gameTimerLabel {
    gameTimerLabel = [SKLabelNode labelNodeWithFontNamed:@"ArialRoundedMTBold"];
    gameTimerLabel.fontSize = 20.0;
    gameTimerLabel.fontColor = [SKColor colorWithRed:135.0/255 green:207.0/255 blue:232.0/255 alpha:0.6];
    gameTimerLabel.position = gameTimerLabel.position = CGPointMake(270, 282);
    [self addChild:gameTimerLabel];
    countDownInt = secondsLeft;
    SKAction *updateLabel = [SKAction runBlock:^{
        gameTimerLabel.text = [NSString stringWithFormat:@" %02d", countDownInt];
        --countDownInt;
    }];
    SKAction *wait = [SKAction waitForDuration:1.0];
    SKAction *updateLabelAndWait = [SKAction sequence:@[updateLabel, wait]];
    [self runAction:[SKAction repeatAction:updateLabelAndWait count:secondsLeft] completion:^{
        gameTimerLabel.text = @"Time's Up";
    }];
}

仅此而已。再次感谢 Ben-G 为我指明了正确的方向。

【问题讨论】:

  • 停止 NSTimer : [autoTimer invalidate]; autoTimer = nil;
  • 如果您使用SKAction 而不是NSTimer,当您移入/移出后台时,倒数计时器将自动暂停/恢复。

标签: ios objective-c sprite-kit nstimer skscene


【解决方案1】:

即使它不能准确回答您的问题,我也会参考这个问题的公认答案:SpriteKit - Creating a timer

一般来说,在使用游戏引擎时,您希望避免使用NSTimer。您的定时操作不会与游戏循环集成,并且您的计时器不会在游戏暂停时自动停止。

改为与update 方法集成或使用SKAction

【讨论】:

  • 似乎是一种合理的方法。今晚回家后我会试试 skaction,然后告诉你。
  • 好的,我想通了。好方法。我正在用我现在使用的代码更新我的问题,以便其他人可以有一个工作代码。谢谢你的回答。
最近更新 更多