主页按钮按下在SpriteKit SKView中引起EXC_BAD_ACCESS code=1错误

6

SpriteKit被设计为在按下主屏幕按钮时清理并暂停所有计时器。

然而,我们发现,如果您在显示活动SKView时单击主屏幕按钮,则应用程序会崩溃。即使该视图已经被用户暂停。

奇怪的是,如果您双击主屏幕按钮并移至多任务视图,一切都正常。

后续说明:模拟器在这两种情况下都可以正常工作,没有崩溃。

还有其他人遇到SpriteKit的这个问题吗?

您找到了原因/解决方案吗?


另外,您可能想在开发者论坛中提问。 - Andrew
1
我有完全相同的问题。很高兴知道这不是我的错 :) - Lior Pollak
@harrym17,您可以尝试通过Storyboard / Nib实例化对象,而不是通过编程方式实例化。这对我们解决了该问题。 - MobileVet
@harrym17 在IB中放置一个UIView,然后将类更改为SKView。 - MobileVet
这是Xcode的默认模板(我从中开始)。 - Lior Pollak
显示剩余2条评论
4个回答

10

我遇到了相同的问题,我在应用程序转入后台之前手动暂停了 ViewController 中的根 SKView 来解决它:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view (already setup as SKView via storyboard)
    SKView * skView = (SKView *)self.view; 

    // Create and configure the scene.
    SKScene *scene = [Menu sceneWithSize:CGSizeMake(skView.bounds.size.height, skView.bounds.size.width)];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterBackground)
    name:UIApplicationWillResignActiveNotification
    object:NULL];
}

- (void)appWillEnterBackground
{
    SKView *skView = (SKView *)self.view;
    skView.paused = YES;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterForeground)
    name:UIApplicationWillEnterForegroundNotification
    object:NULL];
}

- (void)appWillEnterForeground
{
    SKView * skView = (SKView *)self.view;
    skView.paused = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterBackground)
    name:UIApplicationWillResignActiveNotification
    object:NULL];
}

这绝对是人们更常遇到的问题... 所以我接受了它。 - MobileVet
1
Tanzolone的回答很好,但是,如果您下拉通知中心,则应用程序将暂停,但是当您再次上滑通知中心时,它不会恢复。我刚遇到过这个问题,并想到其他人也可能会遇到类似问题,这就是我所做的:找到“-(void)AppWillEnterBackground”,将“name:UIApplicationWillEnterBackground”更改为“name:UIApplicationDidBecomeActiveNotification”。 - Joseph Bergman

0

问题可能是由音频引起的,如果是这样,您可以在此处检查答案https://dev59.com/1GMk5IYBdhLWcg3wxAvy#19283721

我在一个不使用音频的游戏中解决了这个问题。解决方案是在进入后台时暂停SKView:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    SKView *view = (SKView *)self.window.rootViewController.view;
    if (view) {
        view.paused = YES;
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    SKView *view = (SKView *)self.window.rootViewController.view;
    if (view) {
        view.paused = NO;
    }
}

0
  1. 运行Xcode的分析器(项目 > 分析)以确定您的代码中是否存在内存管理问题。

  2. 在Instruments应用程序的分配工具中运行此场景(项目 > 配置文件),该工具可以报告检测到的任何内存误用。


0

我们在使用UICollectionView时遇到了类似的不一致行为。在那种情况下,通过Storyboard实例化对象(而不是以编程方式)解决了问题。

出于直觉,我今天早上尝试了这个方法,结果成功了!

因此,看起来Storyboard在创建SKView时进行了一些我们不知道的魔法操作,使它们能够在按Home按钮时被正确终止。虽然令人沮丧,但至少现在它可以正常工作了。


此外,仅当您在故事板中实例化视图时,为SKView设置FPS才有效。 - MobileVet
更多信息,这可能是由于我们调用了'init'而不是'initWithCoder'或'initWithFrame'造成的,这些看起来也能正常工作。 - MobileVet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接