14

我试图在代码中到处搜索:解释文档在进入后台时会做什么,或者是否有时会暂停,但无济于事 - 有人可以指导我在进入后台时建议做什么启用精灵套件的游戏?

我应该打电话scene.paused = YES,还是我如何确认后台没有绘图,这样我就可以避免被 iOS 终止,这不允许我这样做?

谢谢!

4

6 回答 6

40

SpriteKit 的文档确实还没有那么好,可能是因为它太新了,很少有开发人员实现它。SpriteKit应该在后台运行时暂停动画,但根据我的经验(和harrym17 的),它会导致内存访问错误并完全退出应用程序而不是后台运行。

根据 Apple 开发者论坛的说法,这似乎是 SpriteKit 中的一个已知错误(它在后台运行时并不总是暂停动画,因为它应该)根据 harrym17 的评论导致内存访问错误。这是一个对我有用的简短修复 - 我的应用程序在后台运行时一直崩溃,并且自从添加此代码后一切正常。

(感谢Keith Murray在 Apple 论坛上的代码;据我所知,他没有在 SO 上发布它)。

AppDelegate.h中,确保导入 SpriteKit:

#import <SpriteKit/SpriteKit.h>

AppDelegate.m中,编辑您的applicationWillResignActive方法:

- (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.

     // pause sprite kit
     SKView *view = (SKView *)self.window.rootViewController.view;
     view.paused = YES;
}

这对你的applicationDidBecomeActive方法:

- (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.

     // resume sprite kit
     SKView *view = (SKView *)self.window.rootViewController.view;
     view.paused = NO;
}

显然,在通过 播放音频时在后台运行时会出现其他问题AVAudioSession此线程中提到了一种解决方法。

于 2013-10-11T06:40:29.233 回答
14

正如这里LearnCocos2D所说:

问题是 AVAudioSession 在应用程序进入后台时无法激活。

修复非常简单,也适用于 ObjectAL => 在应用程序处于后台时将 AVAudioSession 设置为非活动状态,并在应用程序进入前台时重新激活音频会话。

带有此修复程序的简化 AppDelegate 如下所示:

#import <AVFoundation/AVFoundation.h>

...

- (void)applicationWillResignActive:(UIApplication *)application
{
    // prevent audio crash
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // prevent audio crash
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // resume audio
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}

PS:此修复将包含在 Kobold Kit v7.0.3 中。

于 2013-10-12T12:09:04.127 回答
10

我想知道为什么这个看似简单的解决方案会导致我在应用程序启动时崩溃,但这是因为我运行了 iAd。所以,要记住的一件事:@MassivePenguin 的答案有效,但如果你有 iAd,你将不得不SKView通过subviews或者它(显然)崩溃。

-(SKView*)getSKViewSubview{
    for (UIView* s in self.window.rootViewController.view.subviews) {
        if ([s isKindOfClass:[SKView class]]) {
            return (SKView*)s;
        }
    }
    return nil;
}

- (void)applicationWillResignActive:(UIApplication *)application {    
    SKView* view = [self getSKViewSubview];

    if (view) {
        view.paused = YES;
    }

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    SKView* view = [self getSKViewSubview];

    if (view) {
        view.paused = NO;
    }

}
于 2014-02-22T08:39:44.917 回答
1

对我来说,没有一个提供解决方案。我确实找到了另一种方法。

// In the AppDelegate
- (void)applicationWillResignActive:(UIApplication *)application
{
    if ([self.window.rootViewController isKindOfClass:[GameViewController class]]) {
        GameViewController *vc = (GameViewController*)self.window.rootViewController;
        [vc pauseGame];
        [vc.view addObserver:vc
                  forKeyPath:@"paused"
                     options:NSKeyValueObservingOptionNew
                     context:nil];
    }
}

// In the GameViewController
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    SKView *skView = (SKView*)self.view;
    MainGame *mainGame = (MainGame*)skView.scene;
    if ([keyPath isEqualToString:@"paused"] &&
        [[change objectForKey:NSKeyValueChangeNewKey] boolValue] == NO &&
        [mainGame isGameInProgress]) {
        [self.view removeObserver:self forKeyPath:@"paused" context:nil];
        skView.paused = YES;
    }
}
于 2014-09-21T16:31:35.003 回答
0

iPad 似乎可以后台运行,但 iPhone 在后台运行精灵套件游戏时肯定会崩溃。您必须手动执行此操作,自动魔术部分不是那么自动。

于 2014-01-14T04:24:58.590 回答
-4

Sprite Kit 移动到后台时会自动暂停其动画计时器——您无需担心您的应用会因此而被杀死。

于 2013-09-25T23:18:52.983 回答