ios - 如何在不浪费应用程序委托(delegate)时间的情况下停止/开始游戏?

标签 ios objective-c iphone sprite-kit appdelegate

当游戏由于按下主页按钮、接听电话等而进入后台时,我试图停止/开始游戏。我不需要在场景中有一个暂停按钮,所以我没有'实现任何暂停方法,但当游戏因接到电话而中断时,我喜欢停止并从剩下的地方开始游戏。 (我希望这个解释是有道理的)。目前我在我的 app delegate 中使用以下代码,但它没有做我想要的,我想知道是否有一种方法可以在 app delegate 中停止和启动游戏,这样当游戏进入后台时,所有操作都会停止并且它会在游戏重新开始时恢复。这是我的应用委托(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 的地方添加通知观察器,例如 View Controller :

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

关于ios - 如何在不浪费应用程序委托(delegate)时间的情况下停止/开始游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101091/

相关文章:

iphone - 无法对存储在 NSMutableArray 中的字符串使用 ComponentsSeparatedByString

ios - 下载数据 (JSON) 不起作用 : unexpectedly found nil while unwrapping an Optional value

objective-c - 使用 NSPredicate 检测 NOT CONTAINS

iphone - 回收单元格时的 UITableView 单元格布局

ios - 自动布局或自动调整大小

iphone - 这是内存泄漏吗?

iphone - 寻找优秀的教程来帮助 ASP.NET 人员学习 iPhone 开发

iphone - NSString 比较不起作用

ios - 我如何处理对单个按钮的多次单击操作以进行 api 调用并从 api 获取真实响应

ios - iOS 应用程序中的所有方法通常都在一个线程中吗? (用于防止竞争条件)