如何在不浪费应用程序委托时间的情况下停止/开始游戏?
how to stop/ start the game without the loss of time from app delegate?
我试图在游戏进入后台时停止/开始游戏,因为按下主页按钮、接到 phone 电话等。我不需要暂停按钮在场景中,所以我没有实现任何暂停方法,但是当游戏因收到 phone 调用而中断时,我喜欢停止并从剩下的位置开始游戏。 (我希望这个解释是有道理的)。目前我在我的 app delegate 中使用以下代码,但它没有做我想要的,我想知道是否有一种方法可以在 app delegate 中停止和启动游戏,这样当游戏进入后台时,所有操作都会停止并且它会在游戏重新开始时恢复。这是我的应用委托:
#import "AppDelegate.h"
#import <SpriteKit/SpriteKit.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (SKView *)getGameView {
NSArray *viewControllers = self.window.rootViewController.childViewControllers;
for (UIViewController *vc in viewControllers) {
if ([vc.view isKindOfClass:[SKView class]]) {
SKView *view = (SKView *)vc.view;
return view;
}
}
return nil;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
SKView *view = [self getGameView];
if (view) {
view.paused = YES; //or NO
}
}
- (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.
}
- (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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
SKView *view = [self getGameView];
if (view) {
view.paused = NO; //or NO
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
您可以将通知观察者直接添加到您可以直接访问 SKView
的地方,例如视图控制器:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseGame) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeGame) name:UIApplicationDidBecomeActiveNotification object:nil];
问题出在上面的代码中,我从来没有注意到。经过一番努力后,我发现我试图访问的视图是直到。所以我将应用程序委托代码更改为以下内容,效果非常好。
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
SKView *view = (SKView *)self.window.rootViewController.view;
view.scene.paused = YES;
}
- (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.
}
- (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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
SKView *view = (SKView *)self.window.rootViewController.view;
view.scene.paused = NO;
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
基本上我回到了允许应用程序委托管理我项目中的每个场景。我在上面的问题中写的代码不起作用。
我试图在游戏进入后台时停止/开始游戏,因为按下主页按钮、接到 phone 电话等。我不需要暂停按钮在场景中,所以我没有实现任何暂停方法,但是当游戏因收到 phone 调用而中断时,我喜欢停止并从剩下的位置开始游戏。 (我希望这个解释是有道理的)。目前我在我的 app delegate 中使用以下代码,但它没有做我想要的,我想知道是否有一种方法可以在 app delegate 中停止和启动游戏,这样当游戏进入后台时,所有操作都会停止并且它会在游戏重新开始时恢复。这是我的应用委托:
#import "AppDelegate.h"
#import <SpriteKit/SpriteKit.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (SKView *)getGameView {
NSArray *viewControllers = self.window.rootViewController.childViewControllers;
for (UIViewController *vc in viewControllers) {
if ([vc.view isKindOfClass:[SKView class]]) {
SKView *view = (SKView *)vc.view;
return view;
}
}
return nil;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
SKView *view = [self getGameView];
if (view) {
view.paused = YES; //or NO
}
}
- (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.
}
- (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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
SKView *view = [self getGameView];
if (view) {
view.paused = NO; //or NO
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
您可以将通知观察者直接添加到您可以直接访问 SKView
的地方,例如视图控制器:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseGame) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeGame) name:UIApplicationDidBecomeActiveNotification object:nil];
问题出在上面的代码中,我从来没有注意到。经过一番努力后,我发现我试图访问的视图是直到。所以我将应用程序委托代码更改为以下内容,效果非常好。
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
SKView *view = (SKView *)self.window.rootViewController.view;
view.scene.paused = YES;
}
- (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.
}
- (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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
SKView *view = (SKView *)self.window.rootViewController.view;
view.scene.paused = NO;
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
基本上我回到了允许应用程序委托管理我项目中的每个场景。我在上面的问题中写的代码不起作用。