SpriteKit被设计为在按下主屏幕按钮时清理并暂停所有计时器。
然而,我们发现,如果您在显示活动SKView时单击主屏幕按钮,则应用程序会崩溃。即使该视图已经被用户暂停。
奇怪的是,如果您双击主屏幕按钮并移至多任务视图,一切都正常。
后续说明:模拟器在这两种情况下都可以正常工作,没有崩溃。
还有其他人遇到SpriteKit的这个问题吗?
您找到了原因/解决方案吗?
SpriteKit被设计为在按下主屏幕按钮时清理并暂停所有计时器。
然而,我们发现,如果您在显示活动SKView时单击主屏幕按钮,则应用程序会崩溃。即使该视图已经被用户暂停。
奇怪的是,如果您双击主屏幕按钮并移至多任务视图,一切都正常。
后续说明:模拟器在这两种情况下都可以正常工作,没有崩溃。
还有其他人遇到SpriteKit的这个问题吗?
您找到了原因/解决方案吗?
我遇到了相同的问题,我在应用程序转入后台之前手动暂停了 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];
}
问题可能是由音频引起的,如果是这样,您可以在此处检查答案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;
}
}
运行Xcode的分析器(项目 > 分析)以确定您的代码中是否存在内存管理问题。
在Instruments应用程序的分配工具中运行此场景(项目 > 配置文件),该工具可以报告检测到的任何内存误用。
我们在使用UICollectionView时遇到了类似的不一致行为。在那种情况下,通过Storyboard实例化对象(而不是以编程方式)解决了问题。
出于直觉,我今天早上尝试了这个方法,结果成功了!
因此,看起来Storyboard在创建SKView时进行了一些我们不知道的魔法操作,使它们能够在按Home按钮时被正确终止。虽然令人沮丧,但至少现在它可以正常工作了。