【发布时间】:2014-11-02 07:49:21
【问题描述】:
我有一款游戏在运行时以 60fps 的速度运行,并且在各个方面都或多或少有效率,但问题是当我进入后台时。我通过两种方式暂停,第一种方法在您触摸游戏中的暂停按钮时运行:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint location3 = [touch locationInNode:self.pauseLayer];
SKSpriteNode *touchedNode3 = (SKSpriteNode *)[self.pauseLayer nodeAtPoint:location3];
if (touchedNode3.physicsBody.categoryBitMask == pausa)
{
if (_gameIsPaused == NO)
{
[self.world setPaused:YES];
[self.backWorld setPaused:YES];
[self setPaused:YES];
_gameIsPaused = YES;
[self.pauseLayer setPaused:NO];
[self showPauseMenu];
}
else
{
[self.world setPaused:NO];
[self.backWorld setPaused:NO];
[self setPaused:NO];
_gameIsPaused = NO;
[self hidePauseMenu];
[self.pauseLayer setPaused: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.
[[AVAudioSession sharedInstance] setActive:NO error:nil];
SKView* view = [self getSKViewSubview];
if (view) {
view.paused = YES;
}
}
问题是在后台运行时,我的应用程序仍在消耗内存,有时当我打开另一个应用程序时,我的游戏崩溃了。在这张图片中,您可以看到我的应用程序在后台运行时的消耗情况:
我的问题是如果有办法防止崩溃,我应该在离开游戏之前手动释放内存吗? 我尝试过两种方式进入后台,启用暂停菜单和禁用暂停菜单。有时它会崩溃,看起来像是随机的。因此内存消耗总是相同的。最好的做法是释放除点、玩家位置、时间、当前世界等重要内容之外的所有内容并摧毁其他所有内容吗?在这种情况下,我该怎么做?摧毁一切并保持分数,积分等?
【问题讨论】:
-
你最好的办法是重现和分析当你的应用程序在后台分别从中恢复时发生的崩溃。不过,您不必担心应用程序的后台内存使用情况,iOS 会处理这些问题。我相信它甚至可以拍摄内存快照并将其存储在磁盘上以供以后恢复,而不是将内容保存在“真实”内存中。此外,如果您发现后台模式存在问题,您可以简单地为您的应用关闭后台模式。
标签: ios objective-c sprite-kit