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;
}
}