当应用程序处于后台状态时无法暂停游戏
Cannot Pause Game when app is in the Background State
这可能是一个非常基本的问题,但我制作了一个简单的游戏,用户可以在其中避开迎面而来的障碍物。当我按下主页按钮时,我的应用程序仍在后台 运行,因此应用程序音乐继续播放,我的所有 NSTIMERS 继续,最终导致游戏角色撞到障碍物)。
可以告诉我如何在按下主页按钮或切换到另一个应用程序(游戏处于后台状态)时暂停游戏,然后在该应用程序再次进入前台时继续游戏吗?
我不知道您是如何编写代码的,但是当用户将应用程序置于后台时,您可以在 AppDelegate.m 中使用此方法来了解确切的时间:
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"going to background");
}
在该方法中,您可以将音乐设置为停止并设置所有 NSTIMERS。你可以使用这个:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NSLog(@"Back from Foreground");
}
知道用户什么时候从后台回到前台。
很简单...
使用此代码:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(pauseGameSoUserDoesNotDieForNoReason:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
将其放入包含您的计时器和方法的 .m 文件的设置方法中。
把它也放在 .m 文件中
-(void)pauseGameSoUserDoesNotDieForNoReason:(NSNotification*)theNotification{
}
最后一个方法:
-(void)pauseGameSoUserDoesNotDieForNoReason:(NSNotification*)theNotification
是你放置暂停代码的地方。
注:
这段代码可以放在几个.m文件中。
这可能是一个非常基本的问题,但我制作了一个简单的游戏,用户可以在其中避开迎面而来的障碍物。当我按下主页按钮时,我的应用程序仍在后台 运行,因此应用程序音乐继续播放,我的所有 NSTIMERS 继续,最终导致游戏角色撞到障碍物)。
可以告诉我如何在按下主页按钮或切换到另一个应用程序(游戏处于后台状态)时暂停游戏,然后在该应用程序再次进入前台时继续游戏吗?
我不知道您是如何编写代码的,但是当用户将应用程序置于后台时,您可以在 AppDelegate.m 中使用此方法来了解确切的时间:
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"going to background");
}
在该方法中,您可以将音乐设置为停止并设置所有 NSTIMERS。你可以使用这个:
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
NSLog(@"Back from Foreground");
}
知道用户什么时候从后台回到前台。
很简单...
使用此代码:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(pauseGameSoUserDoesNotDieForNoReason:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
将其放入包含您的计时器和方法的 .m 文件的设置方法中。
把它也放在 .m 文件中
-(void)pauseGameSoUserDoesNotDieForNoReason:(NSNotification*)theNotification{
}
最后一个方法:
-(void)pauseGameSoUserDoesNotDieForNoReason:(NSNotification*)theNotification
是你放置暂停代码的地方。
注:
这段代码可以放在几个.m文件中。