关闭SKScene后,内存仍然很高 [英] After Closing SKScene, Memory Remains High

查看:104
本文介绍了关闭SKScene后,内存仍然很高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dispatch_once NSObject 来创建数据指针。因此,当主视图控制器出现时,所有游戏资产指针都会生成。为了玩游戏,用户点击 UIViewController 上与特定级别相对应的 UIButton 。我叫它LevelSelectionController。当游戏结束时,用户将点击标签( SKLabel )。并且将删除所有操作和节点。

I use a dispatch_once NSObject to create data pointers. So all game asset pointers are made when the main viewcontroller appears. In order to play a game, the user taps a UIButton corresponding to a particular level on a UIViewController. Let me call it LevelSelectionController. When the game is over, the user will tap a label (SKLabel). And all actions and nodes will be removed.

[self removeAllActions];
[self removeAllChildren];
[self removeFromParent];

此外,特定级别的 SKScene 子类委托返回任务用户到LevelSelectionController到viewcontroller呈现游戏 SKView ,如下所示。

Moreover, an SKScene subclass for a particular level delegates the task of returning the user to LevelSelectionController to the viewcontroller presenting game SKView as follows.

- (void)closeScene {
    SKView *spriteView = [[SKView alloc] init];
    [spriteView presentScene:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

我唯一的问题是当用户离开时内存仍然很高游戏场景(SKScene)。游戏需要大量资产。因此,当游戏开始时,内存使用量将跃升至200 MB。当用户返回到原始级别选择视图控制器时,根据活动监视器,游戏模拟器仍然消耗200 MB。当用户进入不同级别时,内存使用量将再跳过10 MB。那么一旦用户离开SKScene,我如何释放上一场比赛的内存?

The only issue that I have is that the memory remains high when the user leaves the game scene (SKScene). The game requires a lot of assets. So when the game starts, the memory usage will jump to 200 MB. When the user returns to the original level selection view controller, the game simulator is still consuming 200 MB according to Activity Monitor. When the user enters a different level, the memory usage will jump by another 10 MB. So how can I release the memory for the last game once the user leaves SKScene?

我正在使用ARC。 Xcode版本是5.1。开发目标是iOS 7.1。

I'm using ARC. The Xcode version is 5.1. The development target is iOS 7.1.

感谢您的帮助。

- 编辑1 -

-- Edit 1 --

我很傻。我知道问题是什么。当我关闭场景时,我正在创建一个新的SKView,然后我将其设置为nil以摆脱当前场景。有用。但这不应该是这样做的方式。相反,我需要在呈现之前将当前SKView设置为变量。当我关闭场景时,我需要将该变量设置为nil。嗯......我没想到。

I'm silly. I know what the problem is. When I close the scene, I'm creating a new SKView, which I then set it to nil to get of out the current scene. It works. But that should not be the way of doing it. Instead, I need to set the current SKView to a variable before presenting it. When I close the scene, I need to set that variable to nil. Hmm... I wasn't thinking.

- 编辑2 -
当前场景显示为nil时几乎没有变化。从removeFromSuperview中删除它并没有太大作用。

-- Edit 2 -- There's little change when the current scene is presented with nil. Removing it from removeFromSuperview doesn't do much.

推荐答案

一些人在SO上注意到SKScene被取消分配当包含SKView从其超级视图中删除时。

It has been noticed by a few people on SO that an SKScene gets deallocated when the containing SKView is removed from it's superview.

看看这些问题及其答案:

Have a look at these questions and their answers:

在SpriteKit过渡到另一个SKScene之后取消分配SKScene

iOS 7 Sprite Kit释放内存

另外,尝试修改closeScene方法,如下所示:

Also, try modifying the closeScene method like so:

- (void)closeScene {
    SKView *spriteView = (SKView*)self.view;
    [spriteView presentScene:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

这篇关于关闭SKScene后,内存仍然很高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆